Skip to content

Commit c53abef

Browse files
committed
Aligning the resource data structures with the 2024-11-05 schema
- Introducing separate classes for Text and Blob ResourceContents - Custom JSON converter to allow those types to be returned, making it easier to follow the type system against the spec - Updated tests
1 parent ca59b30 commit c53abef

20 files changed

+201
-73
lines changed

src/ModelContextProtocol/AIContentExtensions.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ public static AIContent ToAIContent(this Content content)
3939
}
4040
else if (content is { Type: "resource" } && content.Resource is { } resourceContents)
4141
{
42-
ac = resourceContents.Blob is not null && resourceContents.MimeType is not null ?
43-
new DataContent(Convert.FromBase64String(resourceContents.Blob), resourceContents.MimeType) :
44-
new TextContent(resourceContents.Text);
45-
46-
(ac.AdditionalProperties ??= [])["uri"] = resourceContents.Uri;
42+
ac = resourceContents.ToAIContent();
4743
}
4844
else
4945
{
@@ -62,9 +58,12 @@ public static AIContent ToAIContent(this ResourceContents content)
6258
{
6359
Throw.IfNull(content);
6460

65-
AIContent ac = content.Blob is not null && content.MimeType is not null ?
66-
new DataContent(Convert.FromBase64String(content.Blob), content.MimeType) :
67-
new TextContent(content.Text);
61+
AIContent ac = content switch
62+
{
63+
BlobResourceContents blobResource => new DataContent(Convert.FromBase64String(blobResource.Blob), blobResource.MimeType ?? "application/octet-stream"),
64+
TextResourceContents textResource => new TextContent(textResource.Text),
65+
_ => throw new NotSupportedException($"Resource type '{content.GetType().Name}' is not supported.")
66+
};
6867

6968
(ac.AdditionalProperties ??= [])["uri"] = content.Uri;
7069
ac.RawRepresentation = content;
@@ -79,7 +78,7 @@ public static IList<AIContent> ToAIContents(this IEnumerable<Content> contents)
7978
{
8079
Throw.IfNull(contents);
8180

82-
return contents.Select(ToAIContent).ToList();
81+
return [.. contents.Select(ToAIContent)];
8382
}
8483

8584
/// <summary>Creates a list of <see cref="AIContent"/> from a sequence of <see cref="ResourceContents"/>.</summary>
@@ -89,7 +88,7 @@ public static IList<AIContent> ToAIContents(this IEnumerable<ResourceContents> c
8988
{
9089
Throw.IfNull(contents);
9190

92-
return contents.Select(ToAIContent).ToList();
91+
return [.. contents.Select(ToAIContent)];
9392
}
9493

9594
/// <summary>Extracts the data from a <see cref="DataContent"/> as a Base64 string.</summary>

src/ModelContextProtocol/Client/McpClientExtensions.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,7 @@ internal static (IList<ChatMessage> Messages, ChatOptions? Options) ToChatClient
475475
}
476476
else if (sm.Content is { Type: "resource", Resource: not null })
477477
{
478-
ResourceContents resource = sm.Content.Resource;
479-
480-
if (resource.Text is not null)
481-
{
482-
message.Contents.Add(new TextContent(resource.Text));
483-
}
484-
485-
if (resource.Blob is not null && resource.MimeType is not null)
486-
{
487-
message.Contents.Add(new DataContent(Convert.FromBase64String(resource.Blob), resource.MimeType));
488-
}
478+
message.Contents.Add(sm.Content.Resource.ToAIContent());
489479
}
490480

491481
messages.Add(message);

src/ModelContextProtocol/ModelContextProtocol.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions"/>
17+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" />
1818
<PackageReference Include="Microsoft.Extensions.AI" />
1919
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
2020
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />

src/ModelContextProtocol/Protocol/Types/ResourceContents.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace ModelContextProtocol.Protocol.Types;
4+
5+
/// <summary>
6+
/// Binary contents of a resource.
7+
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
8+
/// </summary>
9+
public class BlobResourceContents : ResourceContents
10+
{
11+
/// <summary>
12+
/// The binary content of the resource.
13+
/// </summary>
14+
[JsonPropertyName("blob")]
15+
public string Blob { get; set; } = default!;
16+
}

0 commit comments

Comments
 (0)