From bf9926366d97770c5a9d92c7d6ac7c43aab5feea Mon Sep 17 00:00:00 2001 From: Kayvan Zahiri Date: Mon, 21 Sep 2026 12:22:44 -0700 Subject: [PATCH 1/2] [MCP] Follow nextCursor when listing tools in MCPClient add_mcp_server called list_tools() once, so tools past a server's first tools/list page were never registered, and allowed_tools could filter down to nothing. Page through with the cursor until nextCursor is empty. --- .../inference/_mcp/mcp_client.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/huggingface_hub/inference/_mcp/mcp_client.py b/src/huggingface_hub/inference/_mcp/mcp_client.py index 7331d13fa61..b8214a774c5 100644 --- a/src/huggingface_hub/inference/_mcp/mcp_client.py +++ b/src/huggingface_hub/inference/_mcp/mcp_client.py @@ -210,17 +210,24 @@ async def add_mcp_server(self, type: ServerType, **params: Any): logger.debug("Initializing session...") await session.initialize() - # List available tools - response = await session.list_tools() - logger.debug("Connected to server with tools:", [tool.name for tool in response.tools]) + # List available tools, following `nextCursor` since servers may paginate tools/list + tools: list[mcp_types.Tool] = [] + cursor: str | None = None + while True: + response = await session.list_tools(cursor=cursor) + tools.extend(response.tools) + cursor = response.nextCursor + if not cursor: + break + logger.debug("Connected to server with tools:", [tool.name for tool in tools]) # Filter tools based on allowed_tools configuration - filtered_tools = response.tools + filtered_tools = tools if allowed_tools is not None: - filtered_tools = [tool for tool in response.tools if tool.name in allowed_tools] + filtered_tools = [tool for tool in tools if tool.name in allowed_tools] logger.debug( - f"Tool filtering applied. Using {len(filtered_tools)} of {len(response.tools)} available tools: {[tool.name for tool in filtered_tools]}" + f"Tool filtering applied. Using {len(filtered_tools)} of {len(tools)} available tools: {[tool.name for tool in filtered_tools]}" ) for tool in filtered_tools: From 8fc922f6551e2af416d257b2ee44787bc5260436 Mon Sep 17 00:00:00 2001 From: Kayvan Zahiri Date: Mon, 21 Sep 2026 16:37:55 -0700 Subject: [PATCH 2/2] [MCP] Keep the first list_tools() call cursor-free and require mcp>=1.9.4 mcp 1.8.x's list_tools() takes no cursor at all, and before 1.9.4 the cursor is sent outside params, so a paginating server returns page one forever. Call list_tools() plainly first so non-paginated servers behave as before, and raise the extra's floor to the first release that paginates correctly. --- setup.py | 2 +- src/huggingface_hub/inference/_mcp/mcp_client.py | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index eecbb10c026..4715d73580d 100644 --- a/setup.py +++ b/setup.py @@ -48,7 +48,7 @@ def get_version() -> str: extras["hf_xet"] = [HF_XET_VERSION] -extras["mcp"] = ["mcp>=1.8.0, <2.0.0"] +extras["mcp"] = ["mcp>=1.9.4, <2.0.0"] # list_tools(cursor=...) only paginates correctly from 1.9.4 extras["testing"] = ( extras["oauth"] diff --git a/src/huggingface_hub/inference/_mcp/mcp_client.py b/src/huggingface_hub/inference/_mcp/mcp_client.py index b8214a774c5..00c0b572fd9 100644 --- a/src/huggingface_hub/inference/_mcp/mcp_client.py +++ b/src/huggingface_hub/inference/_mcp/mcp_client.py @@ -211,14 +211,11 @@ async def add_mcp_server(self, type: ServerType, **params: Any): await session.initialize() # List available tools, following `nextCursor` since servers may paginate tools/list - tools: list[mcp_types.Tool] = [] - cursor: str | None = None - while True: - response = await session.list_tools(cursor=cursor) + response = await session.list_tools() + tools: list[mcp_types.Tool] = list(response.tools) + while response.nextCursor: + response = await session.list_tools(cursor=response.nextCursor) tools.extend(response.tools) - cursor = response.nextCursor - if not cursor: - break logger.debug("Connected to server with tools:", [tool.name for tool in tools]) # Filter tools based on allowed_tools configuration