Update content according to latest spec (#513)

* Update content according to latest spec

* Add Title to a bunch of types

* Add _meta to lots of types

* Address feedback, more cleanup, fix NAOT test failure
This commit is contained in:
Stephen Toub 2025-06-16 19:39:41 -04:00 • committed by GitHub
commit 2bcff315f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace ModelContextProtocol.Protocol;
@ -28,6 +29,7 @@ namespace ModelContextProtocol.Protocol;
[JsonConverter(typeof(Converter))]
public abstract class ResourceContents
{
/// <summary>Prevent external derivations.</summary>
private protected ResourceContents()
{
}
@ -44,6 +46,15 @@ public abstract class ResourceContents
[JsonPropertyName("mimeType")]
public string? MimeType { get; set; }
/// <summary>
/// Gets or sets metadata reserved by MCP for protocol-level metadata.
/// </summary>
/// <remarks>
/// Implementations must not make assumptions about its contents.
/// </remarks>
[JsonPropertyName("_meta")]
public JsonObject? Meta { get; set; }
/// <summary>
/// Provides a <see cref="JsonConverter"/> for <see cref="ResourceContents"/>.
/// </summary>
@ -67,6 +78,7 @@ public abstract class ResourceContents
string? mimeType = null;
string? blob = null;
string? text = null;
JsonObject? meta = null;
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
@ -97,6 +109,10 @@ public abstract class ResourceContents
text = reader.GetString();
break;
case "_meta":
meta = JsonSerializer.Deserialize(ref reader, McpJsonUtilities.JsonContext.Default.JsonObject);
break;
default:
break;
}
@ -108,7 +124,8 @@ public abstract class ResourceContents
{
Uri = uri ?? string.Empty,
MimeType = mimeType,
Blob = blob
Blob = blob,
Meta = meta,
};
}
@ -118,7 +135,8 @@ public abstract class ResourceContents
{
Uri = uri ?? string.Empty,
MimeType = mimeType,
Text = text
Text = text,
Meta = meta,
};
}
@ -148,6 +166,12 @@ public abstract class ResourceContents
writer.WriteString("text", textResource.Text);
}
if (value.Meta is not null)
{
writer.WritePropertyName("_meta");
JsonSerializer.Serialize(writer, value.Meta, McpJsonUtilities.JsonContext.Default.JsonObject);
}
writer.WriteEndObject();
}
}