fix(mcp): generate truthful JSON Schemas from Python annotations - #1176
Open
GautamSharma99 wants to merge 1 commit into
Open
fix(mcp): generate truthful JSON Schemas from Python annotations#1176GautamSharma99 wants to merge 1 commit into
GautamSharma99 wants to merge 1 commit into
Conversation
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
Make SDK MCP tool schemas faithfully represent the Python annotations supplied by tool authors.
Nonein nullable unionsLiteralandEnumconstraints into JSON Schema enumsadditionalPropertiesAnyandobjectas unconstrained JSON valuesProblem
The previous converter silently changed several handler contracts while building the schema exposed through MCP:
str | Nonebecame a non-nullable stringLiteral["fast", "safe"]became an unconstrained stringdict[str, int]became an object with unconstrained valuesNoneClaude and the MCP validation layer consume this generated schema before the Python handler runs. A lossy schema can therefore reject valid inputs, permit values outside the annotation contract, or teach the model to construct the wrong arguments.
Implementation
Nullable unions
Union members are converted without removing
NoneType;Nonenow produces{ "type": "null" }. Requiredness remains controlled independently by TypedDictRequiredandNotRequiredmetadata, so a required nullable key stays required while accepting null.Literals and enums
Literalarguments and PythonEnummember values become JSON Schemaenumvalues. Homogeneous JSON scalar values also receive the matchingtype; mixed scalar enums remain valid heterogeneous enums without an incorrect single type.Containers
dict[str, T]and other string-keyedMappingannotations produceadditionalPropertiesusing the schema forTtuple[T, ...]produces an array item schemaprefixItemsplus exactminItemsandmaxItemsUnsupported annotations
Custom annotations no longer silently fall back to
{ "type": "string" }. Server construction raises a descriptiveTypeErrordirecting the caller to provide an explicit JSON Schema dictionary.Anyandobjectare intentionally supported as unconstrained schemas.Compatibility
Schemas for existing primitives, bare lists and dictionaries,
Annotateddescriptions, and TypedDict required keys are unchanged. The unsupported-type fallback changes intentionally: creating a server with an annotation that cannot be represented now fails early instead of publishing a false schema.Tests
Regression coverage includes:
NoneNotRequirednullable TypedDict keysAnytools/listpreservation through MCP model validationValidation
uv run --extra dev pytest -q— 1301 passed, 5 skippeduv run --extra dev ruff check src tests— passeduv run --extra dev ruff format --check src tests— passeduv run --extra dev mypy src— passedFixes #1166