fix: handle Image helpers in mixed return annotations#2612
Open
pragnyanramtha wants to merge 4 commits into
Open
fix: handle Image helpers in mixed return annotations#2612pragnyanramtha wants to merge 4 commits into
pragnyanramtha wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a structured-output schema generation failure when MCPServer helper wrappers (Image, Audio) appear nested inside mixed return annotations (e.g., tuple[str, Image, AudioContent]). The change ensures these helpers always follow the existing unstructured content conversion path, while protocol content models (e.g., ImageContent, AudioContent, ContentBlock) retain their structured-output behavior.
Changes:
- Added recursive detection of
Image/Audiohelper types inside return annotations to disable structured output schema generation for those tools. - Added unit tests covering helper wrappers in top-level, container, union, and
Annotated[...]return annotations. - Added an integration-style server test validating mixed return annotations with an
Imagehelper produce unstructured content and nooutput_schema.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/mcp/server/mcpserver/utilities/func_metadata.py |
Skips structured-output model/schema creation when return annotations contain Image/Audio helpers (recursively). |
tests/server/mcpserver/test_func_metadata.py |
Adds regression tests ensuring helper wrapper annotations do not generate schemas (including nested/annotated cases). |
tests/server/mcpserver/test_server.py |
Adds a server-level regression test for mixed return annotations including an Image helper. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Fixes #1060.
ImageandAudioare MCPServer helper wrappers, not protocol content Pydantic models. When they appeared inside mixed return annotations such astuple[str, Image, AudioContent], the structured-output schema generator tried to build a Pydantic schema for the helper class and raisedPydanticSchemaGenerationError.This detects helper wrapper types recursively inside return annotations and keeps those tools on the existing unstructured content conversion path, matching the current top-level
Image/Audiobehavior. Protocol content types such asImageContent,AudioContent, andContentBlockkeep their existing structured-output behavior.Validation
uv run --frozen --python 3.12 pytest tests/server/mcpserver/test_server.py::test_tool_mixed_content_with_image_helper_annotation tests/server/mcpserver/test_func_metadata.py::test_unstructured_output_content_helper_annotations tests/server/mcpserver/test_server.py::TestServerTools::test_tool_image_helper tests/server/mcpserver/test_server.py::TestServerTools::test_tool_audio_helper tests/server/mcpserver/test_server.py::TestServerTools::test_tool_mixed_content tests/server/mcpserver/test_func_metadata.py::test_structured_output_generic_types -quv run --frozen --python 3.12 pytest tests/server/mcpserver/test_server.pyuv run --frozen --python 3.12 ruff check src/mcp/server/mcpserver/utilities/func_metadata.py tests/server/mcpserver/test_server.py tests/server/mcpserver/test_func_metadata.pyuv run --frozen --python 3.12 ruff format --check src/mcp/server/mcpserver/utilities/func_metadata.py tests/server/mcpserver/test_server.py tests/server/mcpserver/test_func_metadata.pyuv run --frozen --python 3.12 pyright src/mcp/server/mcpserver/utilities/func_metadata.py tests/server/mcpserver/test_server.py tests/server/mcpserver/test_func_metadata.pygit diff --check