Skip to content

Fix double base64 encoding of image/audio content in sampling messages - #2034

Open
atailakh wants to merge 1 commit into
modelcontextprotocol:mainfrom
atailakh:fix/sampling-image-double-base64
Open

atailakh wants to merge 1 commit into
modelcontextprotocol:mainfrom
atailakh:fix/sampling-image-double-base64

Conversation

@atailakh

@atailakh atailakh commented Nov 22, 2026 •

Copy link
Copy Markdown

Fixes #2033

SingleItemOrListConverter<T>.Write serialized items via GetTypeInfo(typeof(object)), which uses the derived type's default contract and bypasses ContentBlock.Converter. For ImageContentBlock/AudioContentBlock this base64-encoded the already-encoded Data a second time, so images/audio in SamplingMessage.Content and CreateMessageResult.Content were corrupted on the wire.

Items are now serialized via GetTypeInfo(typeof(T)), matching how Read already deserializes them.

Tests

Added tests in SamplingMessageTests asserting that the data field equals the single base64 encoding and round-trips to the original bytes, for a single image, an image in a multi-block message, and CreateMessageResult. They fail before the change and pass after. ModelContextProtocol.Tests (excluding stdio integration tests that need external servers) and ModelContextProtocol.AspNetCore.Tests pass on net10.0.

🤖 Generated with Claude Code

SingleItemOrListConverter<T> serialized each item via GetTypeInfo(typeof(object)),
which resolves the derived type's default contract (e.g. ImageContentBlock) and
bypasses ContentBlock.Converter. The default contract base64-encodes the
ReadOnlyMemory<byte> Data property, which already holds base64 UTF-8 bytes, so
image and audio data in SamplingMessage.Content and CreateMessageResult.Content
were encoded twice on the wire. Serialize items via GetTypeInfo(typeof(T)) instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Image/audio content in SamplingMessage and CreateMessageResult is base64-encoded twice

2 participants