Skip to content

fix: serialize Image and Audio helpers as ImageContent/AudioContent#2659

Open
adityasingh2400 wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
adityasingh2400:fix-image-content-stateless-http-2376
Open

fix: serialize Image and Audio helpers as ImageContent/AudioContent#2659
adityasingh2400 wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
adityasingh2400:fix-image-content-stateless-http-2376

Conversation

@adityasingh2400
Copy link
Copy Markdown

Summary

  • Image and Audio in fastmcp.utilities.types are plain Python classes; handing one to a Pydantic-driven serializer raises PydanticSerializationError: Unable to serialize unknown type. Inside the normal FastMCP tool result flow this is masked because _convert_to_content unwraps the helpers first, but as the reporter on Image/ImageContent serialization fails in stateless HTTP mode #2376 showed it can surface end-to-end when remote MCP clients exercise the stateless HTTP path with json_response=True.
  • Add __get_pydantic_core_schema__ to both helpers so any Pydantic serializer (e.g. model_dump_json on an enclosing model) round-trips them through the existing to_image_content / to_audio_content methods. Wire shape is unchanged, the helper API is unchanged, and _convert_to_content is untouched.
  • Add a regression test that returns an Image from a real FastMCP server over a stateless HTTP transport (stateless_http=True, json_response=True) and asserts the response carries a valid ImageContent payload.

Fixes #2376

Test plan

  • uv run pytest tests/issues/test_2376_image_stateless_http.py -v (3 passed)
  • uv run pytest tests/server/ -v -k "image or stateless" (4 passed)
  • uv run pytest tests/server/fastmcp/ (331 passed, no regressions)
  • uv run ruff format . (no changes)
  • uv run ruff check src/mcp/server/fastmcp/utilities/types.py tests/issues/test_2376_image_stateless_http.py (clean)
  • uv run pyright src/mcp/server/fastmcp/utilities/types.py tests/issues/test_2376_image_stateless_http.py (0 errors)

The Image and Audio helpers in fastmcp.utilities.types are not Pydantic
models, so handing one to a Pydantic-driven serializer (for example,
model_dump_json on an enclosing model) raises PydanticSerializationError
with "Unable to serialize unknown type". Inside the normal FastMCP tool
result flow this never happens because _convert_to_content unwraps the
helpers into ImageContent / AudioContent first, but anywhere the
helpers reach Pydantic directly, serialization fails.

Give both helpers a __get_pydantic_core_schema__ that serializes them
through their existing to_image_content / to_audio_content methods.
The wire shape is unchanged, validation still accepts an Image/Audio
instance, and the regular _convert_to_content path is untouched.

Add a regression test that returns an Image from a FastMCP tool over a
real stateless HTTP transport (json_response=True) and asserts the
response carries a valid ImageContent payload.

Fixes modelcontextprotocol#2376
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.

1 participant