Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(mcp): honor optional server tool capabilities
pei711a377a8e0d358
diff --git a/nanobot/agent/tools/mcp.py b/nanobot/agent/tools/mcp.pyindex f2ff6a3b6..5e8fd1423 100644--- a/nanobot/agent/tools/mcp.py+++ b/nanobot/agent/tools/mcp.py@@ -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_toolsdiff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.pyindex 974f76da4..23545a305 100644--- a/tests/tools/test_mcp_tool.py+++ b/tests/tools/test_mcp_tool.py@@ -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)[email protected]("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# ---------------------------------------------------------------------------