From eba55ecb6fbbd30ee1f4608faaf1dbcfefa6d59d Mon Sep 17 00:00:00 2001 From: Ankit Garg <146389256+AnKiT-GaRG2@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:09:05 +0530 Subject: [PATCH] fix #516: follow tools/list pagination tools/list is paginated by the MCP spec, but discovery stopped after the first page, so every tool past it was lost. Follow nextCursor until the server stops returning one, and stop on a repeated cursor. Signed-off-by: Ankit Garg <146389256+AnKiT-GaRG2@users.noreply.github.com> --- agent_core/core/impl/mcp/server.py | 41 ++++++++++------ tests/test_mcp_tools_list_pagination.py | 65 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 tests/test_mcp_tools_list_pagination.py diff --git a/agent_core/core/impl/mcp/server.py b/agent_core/core/impl/mcp/server.py index 2b3cabf1..02bd6dc0 100644 --- a/agent_core/core/impl/mcp/server.py +++ b/agent_core/core/impl/mcp/server.py @@ -943,25 +943,38 @@ async def _discover_tools(self) -> None: ) return - response = await self._transport.send_request("tools/list", {}) + tools_data: List[Dict[str, Any]] = [] + cursor: Optional[str] = None + seen_cursors = set() - if "error" in response: - error_info = response.get("error", {}) - if isinstance(error_info, dict): - error_msg = error_info.get("message", str(error_info)) - else: - error_msg = str(error_info) - logger.warning( - f"[MCPServer:{self.config.name}] Failed to list tools: {error_msg}" - ) - return + # tools/list is paginated: keep following nextCursor until the server + # stops returning one, otherwise every tool past the first page is lost. + while True: + params: Dict[str, Any] = {"cursor": cursor} if cursor else {} + response = await self._transport.send_request("tools/list", params) + + if "error" in response: + error_info = response.get("error", {}) + if isinstance(error_info, dict): + error_msg = error_info.get("message", str(error_info)) + else: + error_msg = str(error_info) + logger.warning( + f"[MCPServer:{self.config.name}] Failed to list tools: {error_msg}" + ) + return + + result = response.get("result", {}) + tools_data.extend(result.get("tools", [])) - result = response.get("result", {}) - tools_data = result.get("tools", []) + cursor = result.get("nextCursor") + if not cursor or cursor in seen_cursors: + break + seen_cursors.add(cursor) if not tools_data: logger.debug( - f"[MCPServer:{self.config.name}] Server returned empty tools list. Response: {response}" + f"[MCPServer:{self.config.name}] Server returned an empty tools list" ) self._tools = [MCPTool.from_dict(t) for t in tools_data] diff --git a/tests/test_mcp_tools_list_pagination.py b/tests/test_mcp_tools_list_pagination.py new file mode 100644 index 00000000..7435644b --- /dev/null +++ b/tests/test_mcp_tools_list_pagination.py @@ -0,0 +1,65 @@ +"""MCP tool discovery pagination. + +tools/list is paginated by the MCP spec, so every page has to be fetched. +""" + +import asyncio + +from agent_core.core.impl.mcp.config import MCPServerConfig +from agent_core.core.impl.mcp.server import MCPServerConnection + + +class FakeTransport: + is_connected = True + + def __init__(self, pages): + self.pages = pages + self.list_params = [] + + async def send_request(self, method, params=None): + if method == "tools/list": + self.list_params.append(params) + return {"result": self.pages[(params or {}).get("cursor")]} + raise AssertionError(f"unexpected method {method}") + + +def _connection(transport): + conn = MCPServerConnection( + MCPServerConfig(name="fake", transport="stdio", command="fake") + ) + conn._transport = transport + return conn + + +def _tool(name): + return {"name": name, "inputSchema": {}} + + +def test_discover_tools_follows_next_cursor(): + transport = FakeTransport( + pages={ + None: {"tools": [_tool("a")], "nextCursor": "p2"}, + "p2": {"tools": [_tool("b")], "nextCursor": "p3"}, + "p3": {"tools": [_tool("c")]}, + } + ) + conn = _connection(transport) + + asyncio.run(conn._discover_tools()) + + assert [t.name for t in conn.tools] == ["a", "b", "c"] + assert transport.list_params == [{}, {"cursor": "p2"}, {"cursor": "p3"}] + + +def test_discover_tools_stops_on_repeated_cursor(): + transport = FakeTransport( + pages={ + None: {"tools": [_tool("a")], "nextCursor": "loop"}, + "loop": {"tools": [_tool("b")], "nextCursor": "loop"}, + } + ) + conn = _connection(transport) + + asyncio.run(conn._discover_tools()) + + assert [t.name for t in conn.tools] == ["a", "b"]