René's URL Explorer Experiment


Title: Hand TypedDict tool results to pydantic natively by maxisbey · Pull Request #3331 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: Hand TypedDict tool results to pydantic natively by maxisbey · Pull Request #3331 · modelcontextprotocol/python-sdk

X Title: Hand TypedDict tool results to pydantic natively by maxisbey · Pull Request #3331 · modelcontextprotocol/python-sdk

Description: MCPServer now validates and serializes TypedDict tool results through pydantic's own TypedDict support instead of mirroring the TypedDict into a hand-built BaseModel. Fixes #3224 Fixes #3227 Motivation and Context The hand-built mirror in _create_model_from_typeddict was the common root of a few problems with TypedDict return types: Optional keys (NotRequired, total=False) got a None default and were dumped as null, so a tool that left one out produced structuredContent that violated its own outputSchema ({"type": "integer", "default": null}) and the client rejected the call (#3224). This affected every Python version. The field types came from typing.get_type_hints, which leaves NotRequired/Required in place on 3.10 and ReadOnly in place on 3.10–3.12; create_model rejects bare qualifiers, so registration raised PydanticForbiddenQualifier (#3227, plus the unreported ReadOnly variant). The TypedDict's docstring and any Annotated[..., Field(...)] metadata never reached the schema, and constraints weren't enforced. Nested and wrapped TypedDicts (-> list[Person], a TypedDict inside a model) already went through pydantic natively and had none of these issues, so this makes the top-level case consistent with them. What changes: TypedDict returns are handled by a TypeAdapter over the TypedDict itself; _create_model_from_typeddict is gone. pydantic refuses typing.TypedDict below Python 3.12, so on 3.10/3.11 a stdlib TypedDict return type is rebuilt as an equivalent typing_extensions.TypedDict when the validator is built (per-key required/optional derived the way pydantic does it; docstring, __module__/__qualname__, __pydantic_config__ and ReadOnly carried over). output_model stays the class the user declared. Only the top-level class is rebuilt: a stdlib TypedDict nested inside one (or config inherited from a stdlib base) still needs typing_extensions below 3.12 and otherwise falls back to unstructured output with an INFO log; wrapped forms (list[StdTD], StdTD | None, ...) raise pydantic's "use typing_extensions.TypedDict" at registration exactly as they do on main. Delete when 3.11 support is dropped. FuncMetadata derives a private validator (and output_schema, unless one is given) from output_model when it is constructed; func_metadata() constructs it inside the existing "not serializable for structured output" fallback, so unsupported return types still degrade (or raise InvalidSignature with structured_output=True). The fields are read live: code that clears or assigns output_schema/output_model on a registered tool's fn_metadata keeps working, and the validator is rebuilt if output_model is reassigned. FuncMetadata.output_model is the TypedDict class for TypedDict tools (still the model class for everything else); its annotation widens to type[Any] | None. Results are validated with by_name=True as well as by alias, so a return type declaring Field(alias=...) accepts the Python-side keys a tool naturally returns while structuredContent carries the aliases the schema advertises. This supersedes #3225 — thanks @sainikhiljuluri for the thorough reports and the initial fix, and @gingeekrishna for the typing_extensions.get_type_hints pointer. I went with the native route rather than exclude_unset because exclude_unset recurses into nested models (a BaseModel with defaults inside a TypedDict would lose its defaulted fields) and the mirror would still publish default: null and drop metadata. How Has This Been Tested? New/updated unit tests for qualifiers, Annotated metadata, aliases and omitted keys (using stdlib TypedDict so the 3.10/3.11 legs go through the rebuild), hand-built and post-registration-mutated FuncMetadata, an unresolvable key annotation, plus an in-memory Client(server) round trip; the changed tests fail on main. Drove a stdio server with TypedDict tools (both typing and typing_extensions spellings, NotRequired/Required/ReadOnly, nested model with defaults, passthrough CallToolResult) through mcp.Client on 3.14 and on 3.10, and compared against main. Breaking Changes No code changes needed. Observable differences, mostly for TypedDict tools: outputSchema no longer carries "default": null on optional keys; the class docstring becomes description; Annotated[..., Field(...)] descriptions/constraints/aliases and __pydantic_config__/@with_config (e.g. extra='forbid', alias_generator) now appear and are enforced. Omitted optional keys are absent from structuredContent instead of null. A TypedDict pydantic can't build a schema for (un-schema-able value type, unresolvable key annotation) now falls back to unstructured output (or InvalidSignature with structured_output=True) like other unsupported return types, instead of raising from the decorator. One 3.10-only wrinkle: a quoted forward reference inside a builtin generic on a TypedDict key (children: list["Node"]) is something pydantic can't resolve on 3.10, so it takes that fallback too; from __future__ import annotations, List["Node"] or quoting the whole annotation work. For any structured return type, a dict keyed by field names now validates where the type declares aliases (previously only alias keys did). A ReadOnly key triggers pydantic's own UserWarning ("Pydantic will not protect items from any mutation") once at registration. I left that visible rather than filtering pydantic's message in library code; happy to revisit. 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 Not included, possible follow-ups: dataclass/plain-class returns still go through the hand-built model (InitVar fields, slots=True, default_factory have similar rough edges); the synthesized models (wrapped {"result": ...}, dict[str, T], dataclass/plain class) are still constructed outside the fallback try, so an un-schema-able member type there still raises at registration; recursive return types publish a root $ref schema that 2025-11-25 sessions reject (#3337, pre-existing for BaseModel, now also TypedDict); schema is generated in validation mode while content is serialized (#3100). AI Disclaimer

Open Graph Description: MCPServer now validates and serializes TypedDict tool results through pydantic's own TypedDict support instead of mirroring the TypedDict into a hand-built BaseModel. Fixes #3224 Fixes #3227 Mo...

X Description: MCPServer now validates and serializes TypedDict tool results through pydantic's own TypedDict support instead of mirroring the TypedDict into a hand-built BaseModel. Fixes #3224 Fixes #322...

Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/3331

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:6cc775bd-0fc9-48df-df68-79836560203b
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idE8BE:3D4705:9D0360:CBCB5E:6B00E10F
html-safe-nonceba062bc8947691ed4bcf8df9e799a1034f9d56b2cd7555d36d1cc09f3373b368
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOEJFOjNENDcwNTo5RDAzNjA6Q0JDQjVFOjZCMDBFMTBGIiwidmlzaXRvcl9pZCI6IjcyMDQ4MDAzNTIyNzU3MTg0MTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac3a09dabfd72580ff3ec4fa7d3ccbc5037d8bff6027dda1b5edb66b6cadf7f1b8
hovercard-subject-tagpull_request:4304922889
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/3331/files
twitter:imagehttps://avatars.githubusercontent.com/u/224885523?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/224885523?s=400&v=4
og:image:altMCPServer now validates and serializes TypedDict tool results through pydantic's own TypedDict support instead of mirroring the TypedDict into a hand-built BaseModel. Fixes #3224 Fixes #3227 Mo...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git
octolytics-dimension-user_id182288589
octolytics-dimension-user_loginmodelcontextprotocol
octolytics-dimension-repository_id862584018
octolytics-dimension-repository_nwomodelcontextprotocol/python-sdk
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id862584018
octolytics-dimension-repository_network_root_nwomodelcontextprotocol/python-sdk
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release5425dc24e8bd8d6fca9641a12afc984825a43875
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fpull%2F3331%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fpull%2F3331%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files
modelcontextprotocolhttps://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Codehttps://github.com/modelcontextprotocol/python-sdk
Issues308 (308)https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests112 (112)https://github.com/modelcontextprotocol/python-sdk/pulls
Actionshttps://github.com/modelcontextprotocol/python-sdk/actions
Projectshttps://github.com/modelcontextprotocol/python-sdk/projects
Security and quality12 (12)https://github.com/modelcontextprotocol/python-sdk/security
Insightshttps://github.com/modelcontextprotocol/python-sdk/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2Fnew%2Fchoose
maxisbeyhttps://github.com/maxisbey
mainhttps://github.com/modelcontextprotocol/python-sdk/tree/main
fix/typeddict-native-outputhttps://github.com/modelcontextprotocol/python-sdk/tree/fix/typeddict-native-output
Conversation 12 https://github.com/modelcontextprotocol/python-sdk/pull/3331
Commits 2 https://github.com/modelcontextprotocol/python-sdk/pull/3331/commits
Checks 43 https://github.com/modelcontextprotocol/python-sdk/pull/3331/checks
Files changed 4 https://github.com/modelcontextprotocol/python-sdk/pull/3331/files
Hand TypedDict tool results to pydantic natively https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#top
Show all changes 2 commits https://github.com/modelcontextprotocol/python-sdk/pull/3331/files
1e90f40 Hand TypedDict tool results to pydantic natively maxisbey Aug 18, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3331/commits/1e90f40a0bb20e07f6f9ae824b5c90349f970a96
053ffda Derive the output validator on FuncMetadata construction; review fixes maxisbey Aug 19, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3331/commits/053ffdac345b12c14e0175e22a0edec6f8f6413e
Clear filters https://github.com/modelcontextprotocol/python-sdk/pull/3331/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files
structured-output.md https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-6cdb67ce182e70a37d1f810c4d392afc68e9dcd0e7b7904fe608e6e33ce42b92
func_metadata.py https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
test_func_metadata.py https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-441229bc4427c8d5e50cc305a07b43d42f2a292682cf4b0161e59b4f4685e9b8
test_server.py https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-c191383f010475fc22bf69d35f14f16d7e45098b05a1a0f03ad66d6c151d1f34
docs/servers/structured-output.mdhttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-6cdb67ce182e70a37d1f810c4d392afc68e9dcd0e7b7904fe608e6e33ce42b92
View file https://github.com/modelcontextprotocol/python-sdk/blob/053ffdac345b12c14e0175e22a0edec6f8f6413e/docs/servers/structured-output.md
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/modelcontextprotocol/python-sdk/pull/3331/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-6cdb67ce182e70a37d1f810c4d392afc68e9dcd0e7b7904fe608e6e33ce42b92
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-6cdb67ce182e70a37d1f810c4d392afc68e9dcd0e7b7904fe608e6e33ce42b92
src/mcp/server/mcpserver/utilities/func_metadata.pyhttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
View file https://github.com/modelcontextprotocol/python-sdk/blob/053ffdac345b12c14e0175e22a0edec6f8f6413e/src/mcp/server/mcpserver/utilities/func_metadata.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/modelcontextprotocol/python-sdk/pull/3331/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files
https://github.com/modelcontextprotocol/python-sdk/pull/3331/files#diff-5584d2273ac6a270822fe54fb95a27e03d6895afd1216e8bd71d1850e8f09676
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3331/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.