fix: serialize Image and Audio helpers as ImageContent/AudioContent#2659
Open
adityasingh2400 wants to merge 1 commit into
Open
Conversation
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
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
ImageandAudioinfastmcp.utilities.typesare plain Python classes; handing one to a Pydantic-driven serializer raisesPydanticSerializationError: Unable to serialize unknown type. Inside the normal FastMCP tool result flow this is masked because_convert_to_contentunwraps 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 withjson_response=True.__get_pydantic_core_schema__to both helpers so any Pydantic serializer (e.g.model_dump_jsonon an enclosing model) round-trips them through the existingto_image_content/to_audio_contentmethods. Wire shape is unchanged, the helper API is unchanged, and_convert_to_contentis untouched.Imagefrom a realFastMCPserver over a stateless HTTP transport (stateless_http=True,json_response=True) and asserts the response carries a validImageContentpayload.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)