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