mcp fixes
This commit is contained in:
8 files changed
+430
-24
No files matched your search
@@ -547,3 +547,108 @@ async def test_bot_mcp_ghost_mode_command(tmp_path):
|
||||
assert "MCP Коннекторы" in sent
|
||||
agent.chat.assert_not_awaited()
|
||||
await manager.close()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_builtin_mcp_tools_and_cimd_oauth_flow(tmp_path):
|
||||
"""LLM agent can connect & authenticate MCP servers via built-in mcp_connect/mcp_provide_auth tools (including CIMD OAuth like Zenmoney)."""
|
||||
base_url_holder = {"url": ""}
|
||||
valid_tokens: set[str] = set()
|
||||
|
||||
async def handle_prm(request: web.Request) -> web.Response:
|
||||
return web.json_response(
|
||||
{
|
||||
"resource": f"{base_url_holder['url']}/mcp",
|
||||
"authorization_servers": [base_url_holder["url"]],
|
||||
"scopes_supported": ["all"],
|
||||
}
|
||||
)
|
||||
|
||||
async def handle_as(request: web.Request) -> web.Response:
|
||||
u = base_url_holder["url"]
|
||||
return web.json_response(
|
||||
{
|
||||
"issuer": u,
|
||||
"authorization_endpoint": f"{u}/oauth2/authorize/",
|
||||
"token_endpoint": f"{u}/oauth2/token/",
|
||||
"client_id_metadata_document_supported": True,
|
||||
"code_challenge_methods_supported": ["S256"],
|
||||
}
|
||||
)
|
||||
|
||||
async def handle_mcp(request: web.Request) -> web.Response:
|
||||
auth_hdr = request.headers.get("Authorization", "")
|
||||
if auth_hdr not in {f"Bearer {t}" for t in valid_tokens}:
|
||||
return web.Response(
|
||||
status=401,
|
||||
headers={
|
||||
"WWW-Authenticate": f'Bearer resource_metadata="{base_url_holder["url"]}/.well-known/oauth-protected-resource/mcp", scope="all"'
|
||||
},
|
||||
)
|
||||
body = await request.json()
|
||||
req_id = body.get("id")
|
||||
if req_id is None:
|
||||
return web.Response(status=202)
|
||||
if body.get("method") == "initialize":
|
||||
return web.json_response(
|
||||
{"jsonrpc": "2.0", "id": req_id, "result": {"protocolVersion": "2025-03-26", "capabilities": {"tools": {}}, "serverInfo": {"name": "zen-mock"}}}
|
||||
)
|
||||
if body.get("method") == "tools/list":
|
||||
return web.json_response(
|
||||
{"jsonrpc": "2.0", "id": req_id, "result": {"tools": [{"name": "get_balance", "description": "Get balance", "inputSchema": {"type": "object", "properties": {}}}]}}
|
||||
)
|
||||
return web.json_response(
|
||||
{"jsonrpc": "2.0", "id": req_id, "result": {"content": [{"type": "text", "text": "1000 RUB"}], "isError": False}}
|
||||
)
|
||||
|
||||
app = web.Application()
|
||||
app.router.add_get("/.well-known/oauth-protected-resource/mcp", handle_prm)
|
||||
app.router.add_get("/.well-known/oauth-authorization-server", handle_as)
|
||||
app.router.add_route("*", "/mcp", handle_mcp)
|
||||
runner = web.AppRunner(app, access_log=None)
|
||||
await runner.setup()
|
||||
site = web.TCPSite(runner, "127.0.0.1", 0)
|
||||
await site.start()
|
||||
port = int(site._server.sockets[0].getsockname()[1]) # type: ignore[union-attr]
|
||||
base_url = f"http://127.0.0.1:{port}"
|
||||
base_url_holder["url"] = base_url
|
||||
|
||||
try:
|
||||
# Case 1: Without MCP_OAUTH_CLIENT_METADATA_URL -> agent gets clear CIMD/token instructions, then authenticates with token
|
||||
registry = ToolRegistry()
|
||||
settings = Settings(DATA_DIR=tmp_path / "case1", MCP_CONFIG_FILE=tmp_path / "none.json")
|
||||
manager = MCPConnectorManager(settings=settings, registry=registry)
|
||||
assert registry.get("mcp_connect") is not None
|
||||
assert registry.get("mcp_provide_auth") is not None
|
||||
|
||||
res1 = await registry.execute("mcp_connect", {"source": f"zenmoney {base_url}/mcp"})
|
||||
assert "Client ID Metadata Document" in res1
|
||||
assert "mcp_provide_auth" in res1
|
||||
|
||||
valid_tokens.add("zen-personal-tok")
|
||||
res2 = await registry.execute("mcp_provide_auth", {"connector_name": "zenmoney", "credential_or_url": "zen-personal-tok"})
|
||||
assert "авторизован и подключён" in res2
|
||||
assert await registry.execute("mcp_zenmoney_get_balance", {}) == "1000 RUB"
|
||||
|
||||
list_out = await registry.execute("mcp_list_connectors", {})
|
||||
assert "zenmoney" in list_out and "connected" in list_out
|
||||
|
||||
await registry.execute("mcp_remove_connector", {"connector_name": "zenmoney"})
|
||||
assert registry.get("mcp_zenmoney_get_balance") is None
|
||||
await manager.close()
|
||||
|
||||
# Case 2: With MCP_OAUTH_CLIENT_METADATA_URL -> auto-starts OAuth 2.1 PKCE using CIMD URL as client_id!
|
||||
registry2 = ToolRegistry()
|
||||
settings2 = Settings(
|
||||
DATA_DIR=tmp_path / "case2",
|
||||
MCP_CONFIG_FILE=tmp_path / "none.json",
|
||||
MCP_OAUTH_CLIENT_METADATA_URL="https://example.org/homeagent-client.json",
|
||||
)
|
||||
manager2 = MCPConnectorManager(settings=settings2, registry=registry2)
|
||||
res_cimd = await registry2.execute("mcp_connect", {"source": f"zenmoney {base_url}/mcp"})
|
||||
assert "oauth2/authorize/" in res_cimd
|
||||
assert "client_id=https%3A%2F%2Fexample.org%2Fhomeagent-client.json" in res_cimd
|
||||
await manager2.close()
|
||||
finally:
|
||||
await runner.cleanup()
|
||||
|
||||
Reference in new issue
Block a user