π The fast, Pythonic way to build MCP servers and clients.
Block recursive self-invocation in BulkToolCaller (#3433)
Jeremiah Lowin297880bbfa30
diff --git a/src/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.py b/src/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.pyindex 07e82e8aa..71ed25482 100644--- a/src/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.py+++ b/src/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.py@@ -1,6 +1,6 @@from typing import Any-from mcp.types import CallToolResult+from mcp.types import CallToolResult, TextContentfrom pydantic import BaseModel, Fieldfrom fastmcp import FastMCP@@ -53,6 +53,8 @@ class BulkToolCaller(MCPMixin):A class to provide a "bulk tool call" tool for a FastMCP server"""+ _BULK_TOOL_NAMES: frozenset[str] = frozenset({"call_tools_bulk", "call_tool_bulk"})+def register_tools(self,mcp_server: "FastMCP",@@ -122,6 +124,22 @@ class BulkToolCaller(MCPMixin):Helper method to call a tool with the provided arguments."""+ if tool in self._BULK_TOOL_NAMES:+ return CallToolRequestResult(+ tool=tool,+ arguments=arguments,+ isError=True,+ content=[+ TextContent(+ type="text",+ text=(+ "BulkToolCaller cannot call itself. "+ "The tools 'call_tools_bulk' and 'call_tool_bulk' are disallowed."+ ),+ )+ ],+ )+async with Client(self.connection) as client:result = await client.call_tool_mcp(name=tool, arguments=arguments)diff --git a/tests/contrib/test_bulk_tool_caller.py b/tests/contrib/test_bulk_tool_caller.pyindex eb641550b..36b98bb7d 100644--- a/tests/contrib/test_bulk_tool_caller.py+++ b/tests/contrib/test_bulk_tool_caller.py@@ -286,3 +286,64 @@ async def test_call_tools_bulk_error_continues(bulk_caller_live: BulkToolCaller)),])+++async def test_call_tools_bulk_blocks_self_invocation(bulk_caller_live: BulkToolCaller):+ """Test call_tools_bulk blocks recursive calls to bulk tools."""+ tool_calls = [+ CallToolRequest(tool="call_tools_bulk", arguments={"tool_calls": []}),+ CallToolRequest(tool=ECHO_TOOL_NAME, arguments={"arg1": "success_value"}),+ ]++ results = await bulk_caller_live.call_tools_bulk(tool_calls, continue_on_error=True)++ assert results == snapshot(+ [+ CallToolRequestResult(+ content=[+ TextContent(+ type="text",+ text=(+ "BulkToolCaller cannot call itself. "+ "The tools 'call_tools_bulk' and 'call_tool_bulk' are disallowed."+ ),+ )+ ],+ isError=True,+ tool="call_tools_bulk",+ arguments={"tool_calls": []},+ ),+ CallToolRequestResult(+ content=[TextContent(type="text", text="success_value")],+ tool="echo_tool",+ arguments={"arg1": "success_value"},+ ),+ ]+ )+++async def test_call_tool_bulk_blocks_self_invocation(bulk_caller_live: BulkToolCaller):+ """Test call_tool_bulk blocks recursive calls to bulk tools."""++ results = await bulk_caller_live.call_tool_bulk(+ "call_tool_bulk", [{"arg1": "value1"}], continue_on_error=False+ )++ assert results == snapshot(+ [+ CallToolRequestResult(+ content=[+ TextContent(+ type="text",+ text=(+ "BulkToolCaller cannot call itself. "+ "The tools 'call_tools_bulk' and 'call_tool_bulk' are disallowed."+ ),+ )+ ],+ isError=True,+ tool="call_tool_bulk",+ arguments={"arg1": "value1"},+ )+ ]+ )