-
Notifications
You must be signed in to change notification settings - Fork 824
MCP 6/18/25: Add output schema to tools #901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds output schema support to FastMCP tools by automatically generating a JSON schema from tool return type annotations, updating tool registration, test cases, and related type conversions. Key changes include:
- Adding an “output_schema” field to the Tool class and propagating it through tool registration.
- Implementing schema generation for various return types (native, Pydantic models, dataclasses, and FastMCP special types).
- Updating tests and documentation to validate and demonstrate the new output schema functionality.
Reviewed Changes
Copilot reviewed 16 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
tests/utilities/test_types.py | Added tests for the new replace_type function. |
tests/tools/test_tool.py | Updated tool tests to check for the new output_schema field. |
tests/server/test_server_interactions.py | Added tests verifying output schema on tool registration via Client. |
tests/server/openapi/test_openapi.py | Updated OpenAPI schema tests to include output schema fields. |
src/fastmcp/utilities/types.py | Introduced and implemented the replace_type helper. |
src/fastmcp/tools/tool_transform.py | Updated schema merging to use input_schema instead of parameters. |
src/fastmcp/tools/tool_manager.py | Updated return type from MCPContent to ContentBlock. |
src/fastmcp/tools/tool.py | Integrated output_schema in tool conversion and from_function. |
src/fastmcp/server/*.py | Updated type hints for content types from MCPContent to ContentBlock |
docs/servers/tools.mdx | Documented output schema generation and provided JSON example. |
Comments suppressed due to low confidence (1)
src/fastmcp/tools/tool.py:305
- Using the union operator (list | tuple) in isinstance may cause a runtime error. Replace it with a tuple of types: isinstance(result, (list, tuple)).
"""Convert a result to a sequence of content objects."""
modelcontextprotocol/python-sdk#993 seems like a blocker to get this into the low-level server |
- Tools can return StructuredOutput() for explicit structured data - Tools with output_schema automatically return structured data - Client hydrates structured responses into typed objects - Maintains backward compatibility with unstructured-only tools
- TransformedTool.run() now returns ToolResult instead of list[ContentBlock] - forward() and forward_raw() return ToolResult from parent tools - Transform functions can return ToolResult for full control or any value for auto-wrapping - Maintains backward compatibility with existing transform functions 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Summary
This PR introduces two complementary features for FastMCP tools: structured outputs (JSON tool results) and output schemas (validation/declaration of result format), plus automatic client-side deserialization. FastMCP clients now receive richer, automatically deserialized tool outputs instead of raw content blocks.
Closes #743
Implements a clean DX for modelcontextprotocol/modelcontextprotocol#371
Key Features
Automatic Output Schema Generation + Structured Outputs
FastMCP automatically:
Manual Control Options
Client-Side Deserialization (Breaking Change)
Breaking Change
FastMCP clients now receive
CallToolResult
objects fromcall_tool()
instead of rawlist[ContentBlock]
. This provides automatic deserialization of structured outputs while maintaining access to original content through.content
.Technical Implementation
json_schema_to_type()
utility for client-side validationToolResult
objectsImage
,Audio
,File
typesOther Changes
This maintains natural, Pythonic DX while enabling richer tool interactions and full user control when needed.