fix(mcp): honor optional server tool capabilities

This commit is contained in:
pei711 authored and Xubin Ren committed 2026-10-18 21:18:54 +08:00
1 parent 68e9b1cc3a
commit a377a8e0d3
2 files changed
+45 -14

No files matched your search

+12 -10
View File
@@ -1147,19 +1147,21 @@ async def connect_mcp_servers(
read = _filter_malformed_mcp_progress_notifications(read, name)
session = await server_stack.enter_async_context(ClientSession(read, write))
await session.initialize()
initialization = await session.initialize()
# Finish discovery before registering tools so a failed page leaves no partial set.
page = await session.list_tools()
tool_defs = list(page.tools)
seen_cursors: set[str] = set()
while page.nextCursor is not None:
cursor = page.nextCursor
if cursor in seen_cursors:
raise ValueError("MCP tools/list returned a repeated pagination cursor")
seen_cursors.add(cursor)
page = await session.list_tools(params=types.PaginatedRequestParams(cursor=cursor))
tool_defs: list[MCPToolDefinition] = []
if initialization.capabilities.tools is not None:
page = await session.list_tools()
tool_defs.extend(page.tools)
seen_cursors: set[str] = set()
while page.nextCursor is not None:
cursor = page.nextCursor
if cursor in seen_cursors:
raise ValueError("MCP tools/list returned a repeated pagination cursor")
seen_cursors.add(cursor)
page = await session.list_tools(params=types.PaginatedRequestParams(cursor=cursor))
tool_defs.extend(page.tools)
enabled_tools = set(cfg.enabled_tools)
allow_all_tools = "*" in enabled_tools
+33 -4
View File
@@ -782,8 +782,8 @@ def _make_tool_def(name: str) -> SimpleNamespace:
def _make_fake_session(tool_names: list[str]) -> SimpleNamespace:
async def initialize() -> None:
return None
async def initialize() -> SimpleNamespace:
return SimpleNamespace(capabilities=SimpleNamespace(tools=SimpleNamespace()))
async def list_tools() -> SimpleNamespace:
return SimpleNamespace(tools=[_make_tool_def(name) for name in tool_names], nextCursor=None)
@@ -1917,8 +1917,8 @@ def _make_fake_session_with_capabilities(
resource_names: list[str] | None = None,
prompt_names: list[str] | None = None,
) -> SimpleNamespace:
async def initialize() -> None:
return None
async def initialize() -> SimpleNamespace:
return SimpleNamespace(capabilities=SimpleNamespace(tools=SimpleNamespace()))
async def list_tools() -> SimpleNamespace:
return SimpleNamespace(tools=[_make_tool_def(name) for name in tool_names], nextCursor=None)
@@ -2027,6 +2027,35 @@ async def test_discovery_failure_logs_do_not_expose_credentials(
mcp_mod.logger.remove(sink)
@pytest.mark.asyncio
@pytest.mark.parametrize("catalog", ["resources", "prompts"])
async def test_connect_accepts_servers_without_tools_capability(
fake_mcp_runtime: dict[str, object | None], catalog: str,
) -> None:
from mcp.shared.exceptions import McpError
session = _make_fake_session_with_capabilities(
tool_names=[],
resource_names=["document"] if catalog == "resources" else [],
prompt_names=["review"] if catalog == "prompts" else [],
)
session.initialize = AsyncMock(
return_value=SimpleNamespace(capabilities=SimpleNamespace(tools=None)),
)
session.list_tools = AsyncMock(side_effect=McpError(code=-32601, message="Method not found"))
fake_mcp_runtime["session"] = session
registry = ToolRegistry()
stacks = await connect_mcp_servers({"test": MCPServerConfig(command="fake")}, registry)
try:
assert set(stacks) == {"test"}
expected = "mcp_test_resource_document" if catalog == "resources" else "mcp_test_prompt_review"
assert registry.tool_names == [expected]
session.list_tools.assert_not_awaited()
finally:
for stack in stacks.values():
await stack.aclose()
# ---------------------------------------------------------------------------
# _sanitize_name tests
# ---------------------------------------------------------------------------