π The fast, Pythonic way to build MCP servers and clients.
client: decode empty structured content into CallToolResult.data (#5291)
nate nowackac4f582ea9d5
diff --git a/docs/clients/tools.mdx b/docs/clients/tools.mdxindex e4e2e9fdd..41a1f8bb5 100644--- a/docs/clients/tools.mdx+++ b/docs/clients/tools.mdx@@ -118,7 +118,7 @@ async with client:</ResponseField></Card>-For tools without output schemas or when deserialization fails, `.data` will be `None`. Fall back to content blocks in that case:+Structured content without an output schema is returned as a dictionary in `.data`, including an empty object (`{}`). When structured content is absent or deserialization fails, `.data` is `None`. Fall back to content blocks in that case:```pythonasync with client:diff --git a/fastmcp_slim/fastmcp/client/mixins/tools.py b/fastmcp_slim/fastmcp/client/mixins/tools.pyindex e4f6ca14a..1a7ed104a 100644--- a/fastmcp_slim/fastmcp/client/mixins/tools.py+++ b/fastmcp_slim/fastmcp/client/mixins/tools.py@@ -371,7 +371,7 @@ async def _parse_call_tool_result(else:msg = f"Tool '{name}' returned an error"raise ToolError(msg)- elif result.structured_content and not result.is_error:+ elif result.structured_content is not None and not result.is_error:try:raw_fastmcp_meta = (result.meta or {}).get("fastmcp")fastmcp_meta = (diff --git a/tests/client/client/test_client.py b/tests/client/client/test_client.pyindex 800988cd0..8e3655306 100644--- a/tests/client/client/test_client.py+++ b/tests/client/client/test_client.py@@ -9,7 +9,7 @@ import anyioimport pytestfrom mcp import ClientSession, MCPErrorfrom mcp_types import TextContent-from pydantic import AnyUrl+from pydantic import AnyUrl, BaseModelimport fastmcpfrom fastmcp.client import Client@@ -18,6 +18,7 @@ from fastmcp.client.transports import (FastMCPTransport,)from fastmcp.server.server import FastMCP+from fastmcp.tools import ToolResultfrom tests.conftest import user_meta@@ -868,6 +869,50 @@ async def test_client_does_not_unwrap_dict_result():assert user_meta(result.meta) is None+async def test_client_decodes_empty_structured_content():+ """An empty object is a valid structured result, not a missing one (issue #5287)."""+ server = FastMCP()++ class Empty(BaseModel):+ pass++ @server.tool+ def empty_dict() -> dict[str, str]:+ return {}++ @server.tool+ def empty_model() -> Empty:+ return Empty()++ client = Client(transport=FastMCPTransport(server))+ async with client:+ dict_result = await client.call_tool("empty_dict", {})+ assert dict_result.structured_content == {}+ assert dict_result.data == {}+ assert type(dict_result.data) is dict++ model_result = await client.call_tool("empty_model", {})+ assert model_result.structured_content == {}+ assert model_result.data == {}+ assert type(model_result.data) is dict++[email protected]("structured_content", [{}, {"value": "present"}, None])+async def test_client_structured_content_without_output_schema(structured_content):+ server = FastMCP()++ @server.tool(output_schema=None)+ def raw_result() -> ToolResult:+ return ToolResult(content=[], structured_content=structured_content)++ async with Client(server) as client:+ result = await client.call_tool("raw_result")++ assert result.structured_content == structured_content+ assert result.data == structured_content+ assert type(result.data) is type(structured_content)++async def test_client_list_dict_return_type():"""list[dict] return type should produce list of dicts, not Root() objects (issue #3867)."""server = FastMCP()