MCP tool discovery ignores nextCursor and omits tools after the first page
Problem
MCPClient.connection_server() calls ClientSession.list_tools() once and treats that response as the complete tool list. If the server returns nextCursor, tools on later pages are missing from client.tools and therefore from the tools registered by MCPManager.init_config_async().
The MCP tools/list protocol supports pagination. The Python SDK returns one page per call.
Reproduction
Tested against source main at 31a4d36d123688581a9e9744427272b33ce940e0, Python 3.12.14 on Windows, MCP SDK 1.30.0. No model credentials or hosted service are needed.
Save this as paged_server.py:
import asyncio
from mcp.server.lowlevel import Server
from mcp.server.stdio import stdio_server
from mcp.types import ListToolsRequest, ListToolsResult, Tool
server = Server('pagination-repro')
@server.list_tools()
async def list_tools(request: ListToolsRequest) -> ListToolsResult:
cursor = request.params.cursor if request.params else None
if cursor is None:
return ListToolsResult(
tools=[Tool(name='first', inputSchema={'type': 'object'})],
nextCursor='page-2',
)
if cursor == 'page-2':
return ListToolsResult(
tools=[Tool(name='second', inputSchema={'type': 'object'})],
)
raise ValueError(f'Unexpected cursor: {cursor}')
async def main():
async with stdio_server() as (reader, writer):
await server.run(reader, writer, server.create_initialization_options())
asyncio.run(main())Save this alongside it as repro.py and run python repro.py:
import asyncio
import sys
from pathlib import Path
from qwen_agent.tools.mcp_manager import MCPClient
async def main():
client = MCPClient()
try:
await client.connection_server('paged', {
'command': sys.executable,
'args': [str(Path(__file__).with_name('paged_server.py'))],
})
print('Discovered tools:', [tool.name for tool in client.tools])
page2 = await client.session.list_tools(cursor='page-2')
print('Direct SDK second page:', [tool.name for tool in page2.tools])
finally:
await client.cleanup()
asyncio.run(main())Observed on the base commit:
Discovered tools: ['first']
Direct SDK second page: ['second']Expected discovery: ['first', 'second'].
Proposed fix
Follow nextCursor in the same initialized session, accumulate the pages in order, and assign self.tools only after discovery completes. Continue across empty intermediate pages when a cursor is present, and propagate a later-page failure instead of exposing a partial list.
I have prepared a focused fix and regression tests for the shared discovery path across stdio, SSE and streamable HTTP. On the base code, the new tests report 9 failures and 6 passes; with the fix, all 15 pass. The real local stdio reproduction also discovers both tools with the fix. I will submit the associated PR.
AI assistance
This report, implementation and local validation were prepared with Codex under the authorization of icearia0219. No live model or manual end-to-end validation was performed.
Source: QwenLM/Qwen-Agent