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): discover all resource and prompt pages
pei7116670dd72da75
diff --git a/nanobot/agent/tools/mcp.py b/nanobot/agent/tools/mcp.pyindex 5e8fd1423..84d8b4fe8 100644--- a/nanobot/agent/tools/mcp.py+++ b/nanobot/agent/tools/mcp.py@@ -1216,7 +1216,18 @@ async def connect_mcp_servers(if register_extras:try:resources_result = await session.list_resources()- for resource in resources_result.resources:+ resources = list(resources_result.resources)+ resource_cursors: set[str] = set()+ while resources_result.nextCursor is not None:+ cursor = resources_result.nextCursor+ if cursor in resource_cursors:+ raise ValueError("MCP resources/list returned a repeated pagination cursor")+ resource_cursors.add(cursor)+ resources_result = await session.list_resources(+ params=types.PaginatedRequestParams(cursor=cursor)+ )+ resources.extend(resources_result.resources)+ for resource in resources:wrapper = MCPResourceWrapper(session, name, resource, resource_timeout=cfg.tool_timeout)@@ -1234,7 +1245,18 @@ async def connect_mcp_servers(try:prompts_result = await session.list_prompts()- for prompt in prompts_result.prompts:+ prompts = list(prompts_result.prompts)+ prompt_cursors: set[str] = set()+ while prompts_result.nextCursor is not None:+ cursor = prompts_result.nextCursor+ if cursor in prompt_cursors:+ raise ValueError("MCP prompts/list returned a repeated pagination cursor")+ prompt_cursors.add(cursor)+ prompts_result = await session.list_prompts(+ params=types.PaginatedRequestParams(cursor=cursor)+ )+ prompts.extend(prompts_result.prompts)+ for prompt in prompts:wrapper = MCPPromptWrapper(session, name, prompt, prompt_timeout=cfg.tool_timeout)diff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.pyindex 23545a305..1dc974d7b 100644--- a/tests/tools/test_mcp_tool.py+++ b/tests/tools/test_mcp_tool.py@@ -1933,7 +1933,7 @@ def _make_fake_session_with_capabilities(description=f"{rname} resource",))- return SimpleNamespace(resources=resources)+ return SimpleNamespace(resources=resources, nextCursor=None)async def list_prompts() -> SimpleNamespace:prompts = []@@ -1945,7 +1945,7 @@ def _make_fake_session_with_capabilities(arguments=None,))- return SimpleNamespace(prompts=prompts)+ return SimpleNamespace(prompts=prompts, nextCursor=None)return SimpleNamespace(initialize=initialize,@@ -1955,6 +1955,80 @@ def _make_fake_session_with_capabilities()[email protected]("catalog", ["resources", "prompts"])+async def test_connect_registers_all_resource_and_prompt_pages(+ fake_mcp_runtime: dict[str, object | None], catalog: str,+) -> None:+ session = _make_fake_session_with_capabilities(["tool_a"], ["res_b"], ["prompt_c"])+ definition = (+ SimpleNamespace(name="second", uri="file:///second", description="second resource")+ if catalog == "resources"+ else SimpleNamespace(name="second", description="second prompt", arguments=None)+ )+ pages = {+ None: SimpleNamespace(**{catalog: []}, nextCursor=""),+ "": SimpleNamespace(**{catalog: []}, nextCursor="opaque:+/="),+ "opaque:+/=": SimpleNamespace(**{catalog: [definition]}, nextCursor=None),+ }+ cursors: list[str | None] = []++ async def list_pages(*, params: SimpleNamespace | None = None) -> SimpleNamespace:+ cursor = params.cursor if params is not None else None+ cursors.append(cursor)+ return pages[cursor]++ setattr(session, f"list_{catalog}", list_pages)+ fake_mcp_runtime["session"] = session+ registry = ToolRegistry()+ stacks = await connect_mcp_servers({"test": MCPServerConfig(command="fake")}, registry)+ try:+ assert set(stacks) == {"test"}+ assert cursors == [None, "", "opaque:+/="]+ kind = "resource" if catalog == "resources" else "prompt"+ other = "mcp_test_prompt_prompt_c" if catalog == "resources" else "mcp_test_resource_res_b"+ assert set(registry.tool_names) == {"mcp_test_tool_a", other, f"mcp_test_{kind}_second"}+ finally:+ for stack in stacks.values():+ await stack.aclose()++[email protected]("catalog", ["resources", "prompts"])[email protected]("failure", ["request-error", "repeated-cursor"])+async def test_connect_failed_catalog_pagination_preserves_other_capabilities(+ fake_mcp_runtime: dict[str, object | None], catalog: str, failure: str,+) -> None:+ session = _make_fake_session_with_capabilities(["tool_a"], ["res_b"], ["prompt_c"])+ first_page = await getattr(session, f"list_{catalog}")()+ first_page.nextCursor = "next"+ cursors: list[str | None] = []++ async def list_pages(*, params: SimpleNamespace | None = None) -> SimpleNamespace:+ cursor = params.cursor if params is not None else None+ cursors.append(cursor)+ if cursor is None:+ return first_page+ if failure == "request-error":+ raise RuntimeError("second page failed")+ if len(cursors) > 2:+ raise AssertionError("A repeated cursor must not be requested again")+ return SimpleNamespace(**{catalog: []}, nextCursor="next")++ setattr(session, f"list_{catalog}", list_pages)+ fake_mcp_runtime["session"] = session+ registry = ToolRegistry()+ stacks = await connect_mcp_servers({"test": MCPServerConfig(command="fake")}, registry)+ try:+ assert set(stacks) == {"test"}+ assert cursors == [None, "next"]+ other = "mcp_test_prompt_prompt_c" if catalog == "resources" else "mcp_test_resource_res_b"+ assert set(registry.tool_names) == {"mcp_test_tool_a", other}+ finally:+ for stack in stacks.values():+ await stack.aclose()++@pytest.mark.asyncioasync def test_connect_registers_resources_and_prompts(fake_mcp_runtime: dict[str, object | None],