Title: feat: Add structured output support for tool functions by bhosmer-ant · Pull Request #993 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: feat: Add structured output support for tool functions by bhosmer-ant · Pull Request #993 · modelcontextprotocol/python-sdk
X Title: feat: Add structured output support for tool functions by bhosmer-ant · Pull Request #993 · modelcontextprotocol/python-sdk
Description: Adds support for structured output in FastMCP and Client, allowing tool functions to return well-typed, validated data that clients can easily process and validate. Key Changes FastMCP: Adds structured tool support to FastMCP's @mcp.tool() decorator. By default, functions with structured-output-compatible return types become structured tools, other functions become unstructured tools. This behavior can be overridden by passing structured_output=False to the decorator. (structured_output=True will force FastMCP to attempt to create a structured tool, which may be useful in certain edge cases.) To convert a function into a structured tool, the function return type annotation is converted into Pydantic model (see "Supported return types" below) the tool's outputSchema is generated from this model structuredContent results are validated on the server before sending, via the lowlevel server's structured output support Client: adds validation of structuredContent results against tool outputSchema Tests: lots Docs: added to README.md Examples: added examples/fastmcp/weather_structured.py showing various return type options Supported return types @mcp.tool() supports the following function return types for structured output: Pydantic models: Used directly to validate function results and generate outputSchema dict[str, *], TypedDict, NamedTuple, classes with typed fields: Converted to Pydantic models Primitives, lists, generics: Wrapped in {\"result\": value} Note on backward compatibility In keeping with the MCP specification regarding backward compatibility, structured tools will provide both structured results and backwards-compatible unstructured results. In order to provide backward compatibility with previous versions of FastMCP in particular, we do not use the generic JSON serialization provided by the lowlevel server for this purpose. Instead we use the existing FastMCP unstructured results pipeline, which performs several ad hoc conversions of function results. Future versions of FastMCP will instead use the generic lowlevel server pipeline. Motivation and Context Support structured output as defined in spec rev 2025-06-18 How Has This Been Tested? New tests added Breaking Changes Protocol defines structured output in a backwards-compatible way SDK API changes should be backwards compatible Types of changes Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) Breaking change (fix or feature that would cause existing functionality to change) Documentation update Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context Many thanks to @davemssavage, who prototyped an initial version of this functionality.
Open Graph Description: Adds support for structured output in FastMCP and Client, allowing tool functions to return well-typed, validated data that clients can easily process and validate. Key Changes FastMCP: Adds struct...
X Description: Adds support for structured output in FastMCP and Client, allowing tool functions to return well-typed, validated data that clients can easily process and validate. Key Changes FastMCP: Adds struct...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/993
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:643d5786-cf1e-2488-4da2-3e66f2e99c7f |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C7D4:3EDB51:9BB4E3:CFAB9E:6A5B5CA5 |
| html-safe-nonce | 7fcf6e10aaee8a0c153f489a6ac6bfbacfb9d896933a602ca815f46c3f90e4f7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDN0Q0OjNFREI1MTo5QkI0RTM6Q0ZBQjlFOjZBNUI1Q0E1IiwidmlzaXRvcl9pZCI6IjI2MDA1MDM3MTM0MDExMDE0NzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d997797e7d7cff9e3d8444c367aca766a0a6505853f3592c98bc415ef720a670 |
| hovercard-subject-tag | pull_request:2607825582 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/993/files |
| twitter:image | https://avatars.githubusercontent.com/u/177685734?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/177685734?s=400&v=4 |
| og:image:alt | Adds support for structured output in FastMCP and Client, allowing tool functions to return well-typed, validated data that clients can easily process and validate. Key Changes FastMCP: Adds struct... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width