Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/ModelContextProtocol.Core/AIContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,6 @@ public static class AIContentExtensions
return JsonSerializer.SerializeToNode(properties, options.GetTypeInfo(typeof(IReadOnlyDictionary<string, object?>))) as JsonObject;
}

internal static AdditionalPropertiesDictionary ToAdditionalProperties(this JsonObject obj)
{
AdditionalPropertiesDictionary d = [];
foreach (var kvp in obj)
{
d.Add(kvp.Key, kvp.Value);
}

return d;
}

/// <summary>
/// Converts a <see cref="PromptMessage"/> to a <see cref="ChatMessage"/> object.
/// </summary>
Expand Down Expand Up @@ -302,7 +291,18 @@ public static IList<PromptMessage> 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;
Expand Down
72 changes: 72 additions & 0 deletions tests/ModelContextProtocol.Tests/AIContentExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonNode>(aiContent.AdditionalProperties["source"]).GetValue<string>());
}

[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<JsonNode>(Assert.Single(aiContent.AdditionalProperties).Value).GetValue<string>());
}

[Fact]
public void CallToolResult_ToChatMessage_ProducesExpectedAIContent()
{
Expand Down
Loading