Conversation
Contributor
There was a problem hiding this comment.
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."""
8 tasks
Member
Author
|
modelcontextprotocol/python-sdk#993 seems like a blocker to get this into the low-level server |
9 tasks
- 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>
jordicore
pushed a commit
to jordicore/fastmcp
that referenced
this pull request
Jul 2, 2025
MCP 6/18/25: Add output schema to tools
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
CallToolResultobjects 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 validationToolResultobjectsImage,Audio,FiletypesOther Changes
This maintains natural, Pythonic DX while enabling richer tool interactions and full user control when needed.