π The fast, Pythonic way to build MCP servers and clients.
fix(contrib): preserve BulkToolCaller tool prefixes (#5555)
mika99f659bef362
diff --git a/fastmcp_slim/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.py b/fastmcp_slim/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.pyindex ab7c20e71..d96faa208 100644--- a/fastmcp_slim/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.py+++ b/fastmcp_slim/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.py@@ -55,6 +55,10 @@ class BulkToolCaller(MCPMixin):_BULK_TOOL_NAMES: frozenset[str] = frozenset({"call_tools_bulk", "call_tool_bulk"})+ def __init__(self) -> None:+ super().__init__()+ self._registered_bulk_tool_names = set(self._BULK_TOOL_NAMES)+def register_tools(self,mcp_server: "FastMCP",@@ -65,8 +69,14 @@ class BulkToolCaller(MCPMixin):Register the tools provided by this class with the given MCP server."""self.connection = FastMCPTransport(mcp_server)+ if prefix:+ self._registered_bulk_tool_names.update(+ f"{prefix}{separator}{name}" for name in self._BULK_TOOL_NAMES+ )- super().register_tools(mcp_server=mcp_server)+ super().register_tools(+ mcp_server=mcp_server, prefix=prefix, separator=separator+ )@mcp_tool()async def call_tools_bulk(@@ -124,7 +134,7 @@ class BulkToolCaller(MCPMixin):Helper method to call a tool with the provided arguments."""- if tool in self._BULK_TOOL_NAMES:+ if tool in self._registered_bulk_tool_names:return CallToolRequestResult(tool=tool,arguments=arguments,diff --git a/tests/contrib/test_bulk_tool_caller.py b/tests/contrib/test_bulk_tool_caller.pyindex 97ee6a12e..709908917 100644--- a/tests/contrib/test_bulk_tool_caller.py+++ b/tests/contrib/test_bulk_tool_caller.py@@ -4,7 +4,7 @@ import pytestfrom inline_snapshot import snapshotfrom mcp_types import TextContent-from fastmcp import FastMCP+from fastmcp import Client, FastMCPfrom fastmcp.contrib.bulk_tool_caller.bulk_tool_caller import (BulkToolCaller,CallToolRequest,@@ -347,3 +347,61 @@ async def test_call_tool_bulk_blocks_self_invocation(bulk_caller_live: BulkToolC)])+++async def test_register_tools_applies_prefix_and_separator():+ server = FastMCP()+ caller = BulkToolCaller()++ caller.register_tools(server, prefix="one", separator="-")++ async with Client(server) as client:+ tool_names = {tool.name for tool in await client.list_tools()}++ assert tool_names == {"one-call_tools_bulk", "one-call_tool_bulk"}+++async def test_register_all_applies_tool_prefix_and_separator():+ server = FastMCP()+ caller = BulkToolCaller()++ caller.register_all(server, prefix="all", tool_separator="-")++ async with Client(server) as client:+ tool_names = {tool.name for tool in await client.list_tools()}++ assert tool_names == {"all-call_tools_bulk", "all-call_tool_bulk"}+++async def test_multiple_bulk_callers_register_independent_prefixes():+ server = FastMCP()+ first = BulkToolCaller()+ second = BulkToolCaller()++ first.register_tools(server, prefix="first", separator="-")+ second.register_tools(server, prefix="second", separator="-")++ async with Client(server) as client:+ tool_names = {tool.name for tool in await client.list_tools()}++ assert tool_names == {+ "first-call_tools_bulk",+ "first-call_tool_bulk",+ "second-call_tools_bulk",+ "second-call_tool_bulk",+ }+++async def test_prefixed_bulk_tools_and_legacy_names_cannot_call_self():+ caller = BulkToolCaller()+ caller.register_tools(FastMCP(), prefix="prefixed", separator="-")++ for name in (+ "call_tools_bulk",+ "call_tool_bulk",+ "prefixed-call_tools_bulk",+ "prefixed-call_tool_bulk",+ ):+ result = await caller._call_tool(name, {})+ assert result.is_error+ assert result.tool == name