Clarify client handling of embedded binary tool resources (#1762)
Co-authored-by: Luis Rodriguez <25299418+luisangelrod@users.noreply.github.com>
This commit is contained in:
parent
aa07c6fcca
commit
24b122afa4
1 changed files with 120 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using ModelContextProtocol.Protocol;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
|
|
@ -45,4 +46,42 @@ public static class CallToolResultTests
|
|||
Assert.Null(deserialized.IsError);
|
||||
Assert.Null(deserialized.Meta);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void CallToolResult_SerializationRoundTrip_PreservesEmbeddedPdfResource()
|
||||
{
|
||||
byte[] pdfBytes = Encoding.ASCII.GetBytes("%PDF-1.7\n");
|
||||
var original = new CallToolResult
|
||||
{
|
||||
Content =
|
||||
[
|
||||
new EmbeddedResourceBlock
|
||||
{
|
||||
Resource = BlobResourceContents.FromBytes(
|
||||
pdfBytes,
|
||||
"file:///mypdf.pdf",
|
||||
"application/pdf")
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
string json = JsonSerializer.Serialize(original, McpJsonUtilities.DefaultOptions);
|
||||
|
||||
using JsonDocument document = JsonDocument.Parse(json);
|
||||
JsonElement resourceBlock = document.RootElement.GetProperty("content")[0];
|
||||
Assert.Equal("resource", resourceBlock.GetProperty("type").GetString());
|
||||
|
||||
JsonElement resource = resourceBlock.GetProperty("resource");
|
||||
Assert.Equal("file:///mypdf.pdf", resource.GetProperty("uri").GetString());
|
||||
Assert.Equal("application/pdf", resource.GetProperty("mimeType").GetString());
|
||||
Assert.Equal(Convert.ToBase64String(pdfBytes), resource.GetProperty("blob").GetString());
|
||||
|
||||
var deserialized = JsonSerializer.Deserialize<CallToolResult>(json, McpJsonUtilities.DefaultOptions);
|
||||
Assert.NotNull(deserialized);
|
||||
var embeddedResource = Assert.IsType<EmbeddedResourceBlock>(Assert.Single(deserialized.Content));
|
||||
var pdfResource = Assert.IsType<BlobResourceContents>(embeddedResource.Resource);
|
||||
Assert.Equal("file:///mypdf.pdf", pdfResource.Uri);
|
||||
Assert.Equal("application/pdf", pdfResource.MimeType);
|
||||
Assert.Equal(pdfBytes, pdfResource.DecodedData.ToArray());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue