Files
LangBot/tests/unit_tests/api/test_mcp_server.py
huanghuoguoguo ac0b1eaac5 feat: workspace sandbox selection, lightweight validation and limits (#2882)
* feat: configure workspace sandbox backends and E2B templates

* fix: accept user supplied E2B templates without listing API

* feat: separate sandbox test runs and add workspace resource limits

* feat(box): pin workspace backends and validate E2B templates

* fix(box): validate user templates without modifying E2B services

* feat(box): lightweight E2B validation and workspace limit enforcement

* test(box): retain identity cleanup guard for workspace backends

* style(box): satisfy checks on migrated sandbox tests

* fix(box): preserve SDK startup and pin MCP sandbox configuration

* fix(box): retain legacy bound-session execution contract

* fix(mcp): reject untrusted sandbox selection overrides

* test(web): match Workspace settings heading exactly

* test(runner): wait for the managed runtime handshake

* test(runner): align boot fixture with runtime readiness

* fix(box): keep default configuration usable with the published SDK

* fix(box): preserve E2B proxy routing on sandbox reconnect
2026-11-12 21:13:45 +08:00

131 lines
5.5 KiB
Python

from __future__ import annotations
from contextlib import nullcontext
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from mcp.server.fastmcp.exceptions import ToolError
from langbot.pkg.api.mcp import server as mcp_server
from langbot.pkg.api.mcp.server import LangBotMCPServer
def _make_app() -> SimpleNamespace:
app = SimpleNamespace()
app.persistence_mgr = SimpleNamespace(tenant_scope=lambda workspace_uuid: nullcontext())
app.instance_config = SimpleNamespace(data={'system': {'edition': 'community', 'instance_id': 'inst-1'}})
app.ver_mgr = SimpleNamespace(get_current_version=lambda: 'test-version')
app.bot_service = SimpleNamespace(
get_bots=AsyncMock(return_value=[]),
get_bot=AsyncMock(return_value={'uuid': 'bot-1'}),
create_bot=AsyncMock(return_value='bot-1'),
update_bot=AsyncMock(),
delete_bot=AsyncMock(),
list_event_route_statuses=AsyncMock(return_value={'routes': [], 'unmatched_events': [], 'stale_routes': []}),
)
app.pipeline_service = SimpleNamespace(
get_pipelines=AsyncMock(return_value=[]),
get_pipeline=AsyncMock(return_value={}),
create_pipeline=AsyncMock(return_value='pipeline-1'),
update_pipeline=AsyncMock(),
delete_pipeline=AsyncMock(),
)
app.agent_service = SimpleNamespace(
get_agents=AsyncMock(return_value=[]),
get_agent=AsyncMock(return_value={}),
create_agent=AsyncMock(return_value={'uuid': 'agent-1', 'kind': 'agent'}),
update_agent=AsyncMock(),
delete_agent=AsyncMock(),
)
app.llm_model_service = SimpleNamespace(
get_llm_models=AsyncMock(return_value=[]),
get_llm_model=AsyncMock(return_value={}),
)
app.embedding_models_service = SimpleNamespace(get_embedding_models=AsyncMock(return_value=[]))
app.provider_service = SimpleNamespace(get_providers=AsyncMock(return_value=[]))
app.knowledge_service = SimpleNamespace(
get_knowledge_bases=AsyncMock(return_value=[]),
get_knowledge_base=AsyncMock(return_value={}),
retrieve_knowledge_base=AsyncMock(return_value=[]),
)
app.mcp_service = SimpleNamespace(get_mcp_servers=AsyncMock(return_value=[]))
app.skill_service = SimpleNamespace(
list_skills=AsyncMock(return_value=[]),
get_skill=AsyncMock(return_value={}),
)
return app
@pytest.mark.asyncio
async def test_mcp_server_exposes_bot_event_route_tools():
app = _make_app()
server = LangBotMCPServer(app)
tools = await server.mcp.list_tools()
tool_names = {tool.name for tool in tools}
assert 'list_bot_event_route_statuses' in tool_names
assert 'test_bot_event_route' not in tool_names
assert 'list_processors' in tool_names
assert 'list_agents' not in tool_names
assert 'get_workspace_message_language' in tool_names
language_tool = next(tool for tool in tools if tool.name == 'set_workspace_message_language')
assert set(language_tool.inputSchema['properties']['language']['enum']) == {
'en_US',
'zh_Hans',
'zh_Hant',
'ja_JP',
'es_ES',
'ru_RU',
'th_TH',
'vi_VN',
}
@pytest.mark.asyncio
@pytest.mark.parametrize(
'filters', [{}, {'bot_ids': ['bot-1'], 'user_query': 'Alice'}, {'mode': 'real'}, {'execution_statuses': ['failed']}]
)
async def test_monitoring_sessions_accept_only_conversation_filters(monkeypatch, filters):
app = _make_app()
app.monitoring_service = SimpleNamespace(
normalize_page_window=lambda limit, offset: (limit, offset),
get_sessions=AsyncMock(return_value=([], 0)),
)
context = SimpleNamespace(workspace_uuid='workspace-1')
monkeypatch.setattr(mcp_server, 'get_request_context', lambda: context)
monkeypatch.setattr(mcp_server, '_authorized', lambda permission: context)
monkeypatch.setattr(mcp_server, 'get_workspace_language', AsyncMock(return_value='en_US'))
server = LangBotMCPServer(app)
arguments = {'kind': 'sessions', 'filters': filters}
if set(filters) & {'mode', 'execution_statuses'}:
with pytest.raises(ToolError, match='Unsupported monitoring filter'):
await server.mcp.call_tool('get_monitoring_records', arguments)
app.monitoring_service.get_sessions.assert_not_awaited()
else:
await server.mcp.call_tool('get_monitoring_records', arguments)
app.monitoring_service.get_sessions.assert_awaited_once_with(context, limit=100, offset=0, **filters)
@pytest.mark.asyncio
@pytest.mark.parametrize(
'tool,operation,args',
[
('get_workspace_sandbox', 'get', {'refresh': True}),
('set_workspace_sandbox', 'update', {'backend': 'docker', 'e2b': None, 'limits': None}),
('check_workspace_sandbox', 'check', {'e2b': None}),
('test_workspace_sandbox', 'test', {'e2b': None}),
],
)
async def test_sandbox_mcp_uses_application_boundary(monkeypatch, tool, operation, args):
app = _make_app()
context = SimpleNamespace(workspace_uuid='workspace-1')
app.application_api = SimpleNamespace(execute=AsyncMock(return_value={'backend': 'docker'}))
monkeypatch.setattr(mcp_server, 'get_request_context', lambda: context)
monkeypatch.setattr(mcp_server, '_authorized', lambda permission: context)
monkeypatch.setattr(mcp_server, 'get_workspace_language', AsyncMock(return_value='en_US'))
await LangBotMCPServer(app).mcp.call_tool(tool, args)
app.application_api.execute.assert_awaited_once_with('workspace.sandbox.' + operation, context, args)