Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Keep structured content on bulk tool results (#5917)

Asem ahmed78cb2f44d1ee
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.py
index d96faa208..391fcd62a 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
@@ -1,6 +1,6 @@
from typing import Any
-from mcp_types import CallToolResult, TextContent
+from mcp_types import SERVER_INFO_META_KEY, CallToolResult, TextContent
from pydantic import BaseModel, Field
from fastmcp import FastMCP
@@ -39,12 +39,22 @@ class CallToolRequestResult(CallToolResult):
) -> "CallToolRequestResult":
"""
Create a CallToolRequestResult from a CallToolResult.
+
+ Result metadata is kept (including FastMCP's `wrap_result` marker), except
+ the server info entry, which describes the inner connection.
"""
+ meta = {
+ key: value
+ for key, value in (result.meta or {}).items()
+ if key != SERVER_INFO_META_KEY
+ }
return cls(
tool=tool,
arguments=arguments,
is_error=result.is_error,
content=result.content,
+ structured_content=result.structured_content,
+ meta=meta or None,
)
@@ -153,9 +163,6 @@ class BulkToolCaller(MCPMixin):
async with Client(self.connection) as client:
result = await client.call_tool_mcp(name=tool, arguments=arguments)
- return CallToolRequestResult(
- tool=tool,
- arguments=arguments,
- is_error=result.is_error,
- content=result.content,
+ return CallToolRequestResult.from_call_tool_result(
+ result, tool=tool, arguments=arguments
)
diff --git a/tests/contrib/test_bulk_tool_caller.py b/tests/contrib/test_bulk_tool_caller.py
index 709908917..ac2f441c9 100644
--- a/tests/contrib/test_bulk_tool_caller.py
+++ b/tests/contrib/test_bulk_tool_caller.py
@@ -67,6 +67,11 @@ def no_return_tool_result_factory(arg1: str) -> CallToolRequestResult:
)
+async def sum_tool(a: int, b: int) -> dict[str, int]:
+ """A tool that returns structured output."""
+ return {"sum": a + b}
+
+
@pytest.fixture
def live_server_with_tool() -> FastMCP:
"""Fixture to create a FastMCP server instance with the echo_tool registered."""
@@ -74,6 +79,7 @@ def live_server_with_tool() -> FastMCP:
server.add_tool(Tool.from_function(echo_tool))
server.add_tool(Tool.from_function(error_tool))
server.add_tool(Tool.from_function(no_return_tool))
+ server.add_tool(Tool.from_function(sum_tool))
return server
@@ -101,6 +107,8 @@ async def test_call_tool_bulk_single_success(bulk_caller_live: BulkToolCaller):
[
CallToolRequestResult(
content=[TextContent(type="text", text="value1")],
+ structured_content={"result": "value1"},
+ meta={"fastmcp": {"wrap_result": True}},
tool="echo_tool",
arguments={"arg1": "value1"},
)
@@ -118,11 +126,15 @@ async def test_call_tool_bulk_multiple_success(bulk_caller_live: BulkToolCaller)
[
CallToolRequestResult(
content=[TextContent(type="text", text="value1")],
+ structured_content={"result": "value1"},
+ meta={"fastmcp": {"wrap_result": True}},
tool="echo_tool",
arguments={"arg1": "value1"},
),
CallToolRequestResult(
content=[TextContent(type="text", text="value2")],
+ structured_content={"result": "value2"},
+ meta={"fastmcp": {"wrap_result": True}},
tool="echo_tool",
arguments={"arg1": "value2"},
),
@@ -180,6 +192,8 @@ async def test_call_tool_bulk_error_continues(bulk_caller_live: BulkToolCaller):
),
CallToolRequestResult(
content=[TextContent(type="text", text="success_value")],
+ structured_content={"result": "success_value"},
+ meta={"fastmcp": {"wrap_result": True}},
tool="echo_tool",
arguments={"arg1": "success_value"},
),
@@ -197,6 +211,8 @@ async def test_call_tools_bulk_single_success(bulk_caller_live: BulkToolCaller):
[
CallToolRequestResult(
content=[TextContent(type="text", text="value1")],
+ structured_content={"result": "value1"},
+ meta={"fastmcp": {"wrap_result": True}},
tool="echo_tool",
arguments={"arg1": "value1"},
)
@@ -219,6 +235,8 @@ async def test_call_tools_bulk_multiple_success(bulk_caller_live: BulkToolCaller
[
CallToolRequestResult(
content=[TextContent(type="text", text="echo_value")],
+ structured_content={"result": "echo_value"},
+ meta={"fastmcp": {"wrap_result": True}},
tool="echo_tool",
arguments={"arg1": "echo_value"},
),
@@ -281,6 +299,8 @@ async def test_call_tools_bulk_error_continues(bulk_caller_live: BulkToolCaller)
),
CallToolRequestResult(
content=[TextContent(type="text", text="success_value")],
+ structured_content={"result": "success_value"},
+ meta={"fastmcp": {"wrap_result": True}},
tool="echo_tool",
arguments={"arg1": "success_value"},
),
@@ -315,6 +335,8 @@ async def test_call_tools_bulk_blocks_self_invocation(bulk_caller_live: BulkTool
),
CallToolRequestResult(
content=[TextContent(type="text", text="success_value")],
+ structured_content={"result": "success_value"},
+ meta={"fastmcp": {"wrap_result": True}},
tool="echo_tool",
arguments={"arg1": "success_value"},
),
@@ -349,6 +371,42 @@ async def test_call_tool_bulk_blocks_self_invocation(bulk_caller_live: BulkToolC
)
+ ("bulk_tool", "bulk_arguments"),
+ [
+ (
+ "call_tool_bulk",
+ {
+ "tool": "sum_tool",
+ "tool_arguments": [{"a": 1, "b": 2}, {"a": 3, "b": 4}],
+ },
+ ),
+ (
+ "call_tools_bulk",
+ {
+ "tool_calls": [
+ {"tool": "sum_tool", "arguments": {"a": 1, "b": 2}},
+ {"tool": "sum_tool", "arguments": {"a": 3, "b": 4}},
+ ]
+ },
+ ),
+ ],
+)
[email protected]("bulk_caller_live")
+async def test_bulk_results_preserve_structured_content(
+ live_server_with_tool: FastMCP,
+ bulk_tool: str,
+ bulk_arguments: dict[str, Any],
+):
+ async with Client(live_server_with_tool) as client:
+ result = await client.call_tool_mcp(bulk_tool, bulk_arguments)
+
+ assert result.structured_content is not None
+ assert [
+ entry["structured_content"] for entry in result.structured_content["result"]
+ ] == [{"sum": 3}, {"sum": 7}]
+
+
async def test_register_tools_applies_prefix_and_separator():
server = FastMCP()
caller = BulkToolCaller()