fix(mcpserver): preserve Annotated/Field metadata for dict[str, T] return types#2939
Open
anneheartrecord wants to merge 2 commits into
Open
Conversation
…turn types When a tool returns `Annotated[dict[str, T], Field(description="...")]`, the `_try_create_model_and_schema` dict branch was passing the unwrapped `type_expr` (i.e. `dict[str, T]`) to `_create_dict_model` instead of `original_annotation`, so any `Field` description or other Pydantic metadata was dropped from the output schema. Fix by passing `original_annotation` so the `RootModel` picks it up. Fixes modelcontextprotocol#2935
Pre-commit ruff-format check failed because the return type annotation for get_headers() in the test exceeded line-length limits. Wrap it to three lines to satisfy the formatter.
Author
|
Fixed the pre-commit failure — ruff-format wrapped a long return-type annotation in the test file. All other checks were already green. |
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
When a tool declares a
dict[str, T]return type wrapped inAnnotatedwithPydantic
Fieldmetadata (e.g. a description), that metadata was silently droppedfrom the generated output schema.
Root cause
In
_try_create_model_and_schema, thedict[str, T]branch called_create_dict_model(func_name, type_expr)wheretype_expris theAnnotated-stripped type. The existing TODO comment on that line even flaggedthe issue. The fix is to pass
original_annotationinstead, soRootModel[Annotated[dict[str, T], Field(...)]]picks up the metadata.Changes
src/mcp/server/mcpserver/utilities/func_metadata.py: passoriginal_annotationinstead of
type_exprto_create_dict_model; replace TODO comment with aclarifying one
tests/server/mcpserver/test_func_metadata.py: addtest_structured_output_dict_str_preserves_annotated_metadataregression testcovering
Field(description=...)andField(description=..., title=...)Fixes #2935