From 55b98635e47f95fe0778807e97642b0985883049 Mon Sep 17 00:00:00 2001 From: Peder Date: Tue, 1 Dec 2026 19:46:56 +0100 Subject: [PATCH] Keep the resource URI when converting an embedded resource to AIContent 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 #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 --- .../AIContentExtensions.cs | 24 +++---- .../AIContentExtensionsTests.cs | 72 +++++++++++++++++++ 2 files changed, 84 insertions(+), 12 deletions(-) diff --git a/src/ModelContextProtocol.Core/AIContentExtensions.cs b/src/ModelContextProtocol.Core/AIContentExtensions.cs index affb8b20a..e031e23f5 100644 --- a/src/ModelContextProtocol.Core/AIContentExtensions.cs +++ b/src/ModelContextProtocol.Core/AIContentExtensions.cs @@ -150,17 +150,6 @@ public static class AIContentExtensions return JsonSerializer.SerializeToNode(properties, options.GetTypeInfo(typeof(IReadOnlyDictionary))) as JsonObject; } - internal static AdditionalPropertiesDictionary ToAdditionalProperties(this JsonObject obj) - { - AdditionalPropertiesDictionary d = []; - foreach (var kvp in obj) - { - d.Add(kvp.Key, kvp.Value); - } - - return d; - } - /// /// Converts a to a object. /// @@ -302,7 +291,18 @@ public static IList ToPromptMessages(this ChatMessage chatMessage if (ac is not null) { ac.RawRepresentation = content; - ac.AdditionalProperties = content.Meta?.ToAdditionalProperties(); + + // Merge the block's _meta into, rather than replace, any properties the conversion has already set, + // such as the "uri" that converting an embedded resource records. Those describe the content itself, + // so they take precedence over a _meta entry with the same key. + if (content.Meta is { } meta) + { + AdditionalPropertiesDictionary additionalProperties = ac.AdditionalProperties ??= []; + foreach (var kvp in meta) + { + additionalProperties.TryAdd(kvp.Key, kvp.Value); + } + } } return ac; diff --git a/tests/ModelContextProtocol.Tests/AIContentExtensionsTests.cs b/tests/ModelContextProtocol.Tests/AIContentExtensionsTests.cs index 77be2f922..6fb497fea 100644 --- a/tests/ModelContextProtocol.Tests/AIContentExtensionsTests.cs +++ b/tests/ModelContextProtocol.Tests/AIContentExtensionsTests.cs @@ -2,12 +2,84 @@ using ModelContextProtocol.Protocol; using ModelContextProtocol.Tests.Protocol; using System.Text.Json; +using System.Text.Json.Nodes; using System.Text.Json.Serialization; namespace ModelContextProtocol.Tests; public class AIContentExtensionsTests { + private static ResourceContents CreateResourceContents(string kind) => kind switch + { + "blob" => new BlobResourceContents { Uri = "file:///report.pdf", Blob = "cmVwb3J0"u8.ToArray(), MimeType = "application/pdf" }, + _ => new TextResourceContents { Uri = "file:///notes.txt", Text = "notes", MimeType = "text/plain" }, + }; + + [Theory] + [InlineData("blob")] + [InlineData("text")] + public void EmbeddedResourceBlock_ToAIContent_KeepsResourceUri(string kind) + { + var resource = CreateResourceContents(kind); + var aiContent = new EmbeddedResourceBlock { Resource = resource }.ToAIContent(); + + Assert.NotNull(aiContent); + Assert.NotNull(aiContent.AdditionalProperties); + Assert.Equal(resource.Uri, aiContent.AdditionalProperties["uri"]); + } + + [Theory] + [InlineData("blob")] + [InlineData("text")] + public void EmbeddedResourceBlock_WithMeta_ToAIContent_KeepsResourceUriAndMeta(string kind) + { + var resource = CreateResourceContents(kind); + var block = new EmbeddedResourceBlock + { + Resource = resource, + Meta = new JsonObject { ["source"] = "crawler" }, + }; + + var aiContent = block.ToAIContent(); + + Assert.NotNull(aiContent); + Assert.NotNull(aiContent.AdditionalProperties); + Assert.Equal(resource.Uri, aiContent.AdditionalProperties["uri"]); + Assert.Equal("crawler", Assert.IsAssignableFrom(aiContent.AdditionalProperties["source"]).GetValue()); + } + + [Fact] + public void EmbeddedResourceBlock_WithUriKeyInMeta_ToAIContent_ResourceUriTakesPrecedence() + { + var block = new EmbeddedResourceBlock + { + Resource = new BlobResourceContents { Uri = "file:///actual.bin", Blob = "AAEC"u8.ToArray() }, + Meta = new JsonObject { ["uri"] = "file:///from-meta.bin" }, + }; + + var aiContent = block.ToAIContent(); + + Assert.NotNull(aiContent); + Assert.Equal("file:///actual.bin", aiContent.AdditionalProperties?["uri"]); + } + + [Fact] + public void ContentBlock_WithMeta_ToAIContent_CopiesMetaIntoAdditionalProperties() + { + var block = new ImageContentBlock + { + Data = "AAEC"u8.ToArray(), + MimeType = "image/png", + Meta = new JsonObject { ["source"] = "camera" }, + }; + + var aiContent = block.ToAIContent(); + + Assert.NotNull(aiContent); + Assert.NotNull(aiContent.AdditionalProperties); + Assert.Equal("camera", Assert.IsAssignableFrom(Assert.Single(aiContent.AdditionalProperties).Value).GetValue()); + } + [Fact] public void CallToolResult_ToChatMessage_ProducesExpectedAIContent() {