fix(rmcp): flatten Resource variant of PromptMessageContent#843
Open
ynishi wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix(rmcp): flatten Resource variant of PromptMessageContent#843ynishi wants to merge 1 commit intomodelcontextprotocol:mainfrom
ynishi wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
The Resource variant of PromptMessageContent was missing #[serde(flatten)],
causing the embedded resource content block to serialize as a double-nested
shape `{ "type": "resource", "resource": { "resource": {...} } }` instead of
the spec-compliant flat shape `{ "type": "resource", "resource": {uri, mimeType, text} }`.
This caused Zod-based MCP clients (e.g. Claude Code) to reject prompts/get
responses containing embedded resource messages with InvalidUnion errors.
The Image and ResourceLink variants already use #[serde(flatten)] correctly;
only Resource was missing it.
Fix: add #[serde(flatten)] so EmbeddedResource (=Annotated<RawEmbeddedResource>)
fields _meta / annotations / resource are flattened to the content-block level,
matching the MCP spec for prompts embedded resources.
Regression test: test_prompt_message_resource_serialization_is_flat verifies
content.resource.uri is reachable and content.resource.resource is absent.
Schema snapshots regenerated via UPDATE_SCHEMA=1.
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.
Fixes #842
Motivation and Context
PromptMessageContent::Resourcecurrently serializes with a doubly-nestedresourceobject, producing:{ "type": "resource", "resource": { "resource": { "uri": "file:///example.md", "text": "...", "mimeType": "text/markdown" } } }The MCP specification (Prompts — Embedded Resources) defines the wire shape
as a single, flat
resourcefield:{ "type": "resource", "resource": { "uri": "file:///example.md", "text": "...", "mimeType": "text/markdown" } }The other variants of
PromptMessageContent(Text,Image,ResourceLink)already use
#[serde(flatten)]and serialize flat.Resourceis the onlyoutlier: the
resource: EmbeddedResourcefield was missing#[serde(flatten)],so the outer enum tag wrapper plus the inner struct's own
resourcefield bothserialize, yielding the double nesting.
This breaks interop with any MCP client that follows the spec literally — the
inner
{uri, text, mimeType}is unreachable without descending two levels.How Has This Been Tested?
test_prompt_message_resource_serialization_is_flat)that constructs a
PromptMessage::new_resource(...), serializes it, andasserts:
content.type == "resource"content.resource.uri,content.resource.mimeType,content.resource.textare reachable directly (flat shape).
content.resource.resourceis absent (regression guard against thedoubly-nested shape).
UPDATE_SCHEMA=1.cargo test --all-featurespasses locally for thermcpcrate (lib + allintegration tests).
cargo +nightly fmt --all -- --checkandcargo clippy --all-targets --all-features -- -D warningspass.Breaking Changes
Wire format: yes, but in the spec-compliance direction. Any client that
relied on the previous (non-spec) doubly-nested shape would break, but such
clients are already non-conformant against the MCP spec and against the other
content variants in this same enum.
Rust API: none. The variant's field name (
resource) and type(
EmbeddedResource) are unchanged; only the serde representation is corrected.cargo semver-checksshould be unaffected.Types of Changes
Additional Context
Single-line behavioral fix in
crates/rmcp/src/model/prompt.rs: add#[serde(flatten)]to theResourcevariant's inner field, matching thepattern used by
ImageandResourceLinkin the same enum.