Skip to content

Fix NotSupportedException when returning IEnumerable<ContentBlock> #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 5, 2025
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