Skip to content

fix: handle Image helpers in mixed return annotations#2612

Open
pragnyanramtha wants to merge 4 commits into
modelcontextprotocol:mainfrom
pragnyanramtha:fix-1060-image-helper-schema
Open

fix: handle Image helpers in mixed return annotations#2612
pragnyanramtha wants to merge 4 commits into
modelcontextprotocol:mainfrom
pragnyanramtha:fix-1060-image-helper-schema

Conversation

@pragnyanramtha
Copy link
Copy Markdown

Summary

Fixes #1060.

Image and Audio are MCPServer helper wrappers, not protocol content Pydantic models. When they appeared inside mixed return annotations such as tuple[str, Image, AudioContent], the structured-output schema generator tried to build a Pydantic schema for the helper class and raised PydanticSchemaGenerationError.

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/Audio behavior. Protocol content types such as ImageContent, AudioContent, and ContentBlock keep 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 -q
  • uv run --frozen --python 3.12 pytest tests/server/mcpserver/test_server.py
  • uv 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.py
  • uv 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.py
  • uv 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.py
  • git diff --check

Copilot AI review requested due to automatic review settings May 15, 2026 22:49
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/Audio helper 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 Image helper produce unstructured content and no output_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PydanticSchemaGenerationError: Unable to generate pydantic-core schema for Image type

2 participants