Skip to content

Keep the resource URI when converting an embedded resource to AIContent - #2068

Open
PederHP wants to merge 1 commit into
modelcontextprotocol:mainfrom
PederHP:fix/embedded-resource-uri-in-ai-content
Open

PederHP wants to merge 1 commit into
modelcontextprotocol:mainfrom
PederHP:fix/embedded-resource-uri-in-ai-content

Conversation

@PederHP

@PederHP PederHP commented Dec 1, 2026

Copy link
Copy Markdown
Member

ToAIContent(ResourceContents) stores the resource URI in AdditionalProperties["uri"]. ToAIContent(ContentBlock) calls it for an EmbeddedResourceBlock, then replaces AdditionalProperties with the block's _meta:

ac.AdditionalProperties = content.Meta?.ToAdditionalProperties();

So an embedded resource converted this way (as McpClientTool, ToChatMessage and ToAIContents do) loses its URI. If the block has _meta, only the _meta entries are kept. This started with #976, and no test covered the URI.

The fix merges _meta into the properties the conversion already set. If _meta has its own uri key, the resource URI wins. The ToAdditionalProperties helper had no other callers and is removed.

One side effect: ToContentBlock writes AdditionalProperties back as _meta, so a round trip now leaves uri in the block's _meta, as it did before #976. Separately, ToContentBlock has always created blob resources with Uri = "". Both could be fixed by reading the URI from AdditionalProperties["uri"] and keeping it out of _meta. I left that out to keep this PR to the regression, but I'm happy to add it here or in a follow-up.

New tests in AIContentExtensionsTests cover:

  • blob and text resources, with and without _meta;
  • the uri precedence;
  • _meta copying for other content types.

The URI tests fail without the fix.

Written by Claude Opus 5.5 (Claude Code), guided and reviewed by me.

🤖 Generated with Claude Code

ToAIContent(ResourceContents) records the resource's URI in
AdditionalProperties["uri"], but ToAIContent(ContentBlock) then assigned
the block's _meta to AdditionalProperties, replacing that dictionary. An
EmbeddedResourceBlock without _meta therefore lost its URI entirely, and
one with _meta kept only the _meta entries. This has been the case since
modelcontextprotocol#976 started copying block _meta into AdditionalProperties.

Merge the block's _meta into the existing properties instead. When a
_meta entry has the same key as a property the conversion set itself,
the conversion's value wins, since it describes the content directly.

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.

1 participant