Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/ModelContextProtocol.Core/McpJsonUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ internal static bool IsValidMcpToolSchema(JsonElement element)
[JsonSerializable(typeof(AudioContentBlock))]
[JsonSerializable(typeof(EmbeddedResourceBlock))]
[JsonSerializable(typeof(ResourceLinkBlock))]
[JsonSerializable(typeof(IEnumerable<ContentBlock>))]
[JsonSerializable(typeof(PromptReference))]
[JsonSerializable(typeof(ResourceTemplateReference))]
[JsonSerializable(typeof(BlobResourceContents))]
Expand Down
33 changes: 33 additions & 0 deletions tests/ModelContextProtocol.Tests/McpJsonUtilitiesTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using ModelContextProtocol.Protocol;

namespace ModelContextProtocol.Tests;

Expand Down Expand Up @@ -43,6 +44,38 @@ public static void DefaultOptions_UnknownEnumHandling()
}
}

[Fact]
public static void DefaultOptions_CanSerializeIEnumerableOfContentBlock()
{
var options = McpJsonUtilities.DefaultOptions;

// Create an IEnumerable<ContentBlock> with different content types
IEnumerable<ContentBlock> contentBlocks = new List<ContentBlock>
{
new TextContentBlock { Text = "Hello World" },
new TextContentBlock { Text = "Test message" }
};

// Should not throw NotSupportedException
string json = JsonSerializer.Serialize(contentBlocks, options);

Assert.NotNull(json);
Assert.Contains("Hello World", json);
Assert.Contains("Test message", json);
Assert.Contains("\"type\":\"text\"", json);

// Should also be able to deserialize back
var deserialized = JsonSerializer.Deserialize<IEnumerable<ContentBlock>>(json, options);
Assert.NotNull(deserialized);
var deserializedList = deserialized.ToList();
Assert.Equal(2, deserializedList.Count);
Assert.All(deserializedList, cb => Assert.Equal("text", cb.Type));

var textBlocks = deserializedList.Cast<TextContentBlock>().ToArray();
Assert.Equal("Hello World", textBlocks[0].Text);
Assert.Equal("Test message", textBlocks[1].Text);
}

public enum EnumWithoutAnnotation { A = 1, B = 2, C = 3 }

[JsonConverter(typeof(JsonStringEnumConverter<EnumWithAnnotation>))]
Expand Down
Loading