Fix NotSupportedException when returning IEnumerable<ContentBlock> (#675)
This commit is contained in:
parent
5e5b1af29b
commit
e1a2564a4e
2 changed files with 34 additions and 0 deletions
|
|
@ -146,6 +146,7 @@ public static partial class McpJsonUtilities
|
|||
[JsonSerializable(typeof(AudioContentBlock))]
|
||||
[JsonSerializable(typeof(EmbeddedResourceBlock))]
|
||||
[JsonSerializable(typeof(ResourceLinkBlock))]
|
||||
[JsonSerializable(typeof(IEnumerable<ContentBlock>))]
|
||||
[JsonSerializable(typeof(PromptReference))]
|
||||
[JsonSerializable(typeof(ResourceTemplateReference))]
|
||||
[JsonSerializable(typeof(BlobResourceContents))]
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
@ -43,6 +44,38 @@ public static class McpJsonUtilitiesTests
|
|||
}
|
||||
}
|
||||
|
||||
[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>))]
|
||||
|
|
|
|||
Loading…
Reference in a new issue