From 366c86a86436471bc3c59c54073e1c2247ef9412 Mon Sep 17 00:00:00 2001 From: redth Date: Fri, 21 Mar 2025 14:27:13 -0400 Subject: [PATCH] Refactor Resource/ResourceTemplate to subclass Annotated The `Resource` and `ResourceTemplate` types should derive from a base `Annotated` type having the `Annotations` property as per the [spec](https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.ts#L821) This small change makes the C# types match the schema more closely. Also renamed Resources.cs to Resource.cs to reflect the type name. --- .../Protocol/Types/Annotated.cs | 16 ++++++++++++++++ .../Protocol/Types/{Resources.cs => Resource.cs} | 8 +------- .../Protocol/Types/ResourceTemplate.cs | 8 +------- 3 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 src/ModelContextProtocol/Protocol/Types/Annotated.cs rename src/ModelContextProtocol/Protocol/Types/{Resources.cs => Resource.cs} (83%) diff --git a/src/ModelContextProtocol/Protocol/Types/Annotated.cs b/src/ModelContextProtocol/Protocol/Types/Annotated.cs new file mode 100644 index 000000000..2b3458ff4 --- /dev/null +++ b/src/ModelContextProtocol/Protocol/Types/Annotated.cs @@ -0,0 +1,16 @@ +using System.Text.Json.Serialization; + +namespace ModelContextProtocol.Protocol.Types; + +/// +/// Base for objects that include optional annotations for the client. The client can use annotations to inform how objects are used or displayed. +/// See the schema for details +/// +public abstract record Annotated +{ + /// + /// Optional annotations for the resource. + /// + [JsonPropertyName("annotations")] + public Annotations? Annotations { get; init; } +} diff --git a/src/ModelContextProtocol/Protocol/Types/Resources.cs b/src/ModelContextProtocol/Protocol/Types/Resource.cs similarity index 83% rename from src/ModelContextProtocol/Protocol/Types/Resources.cs rename to src/ModelContextProtocol/Protocol/Types/Resource.cs index 7bd3a93c9..fbe44bce7 100644 --- a/src/ModelContextProtocol/Protocol/Types/Resources.cs +++ b/src/ModelContextProtocol/Protocol/Types/Resource.cs @@ -6,7 +6,7 @@ namespace ModelContextProtocol.Protocol.Types; /// Represents a known resource that the server is capable of reading. /// See the schema for details /// -public record Resource +public record Resource : Annotated { /// /// The URI of this resource. @@ -31,10 +31,4 @@ public record Resource /// [JsonPropertyName("mimeType")] public string? MimeType { get; init; } - - /// - /// Optional annotations for the resource. - /// - [JsonPropertyName("annotations")] - public Annotations? Annotations { get; init; } } diff --git a/src/ModelContextProtocol/Protocol/Types/ResourceTemplate.cs b/src/ModelContextProtocol/Protocol/Types/ResourceTemplate.cs index b32822a21..4bf6e60c1 100644 --- a/src/ModelContextProtocol/Protocol/Types/ResourceTemplate.cs +++ b/src/ModelContextProtocol/Protocol/Types/ResourceTemplate.cs @@ -8,7 +8,7 @@ namespace ModelContextProtocol.Protocol.Types; /// Represents a known resource template that the server is capable of reading. /// See the schema for details /// -public record ResourceTemplate +public record ResourceTemplate : Annotated { /// /// The URI template (according to RFC 6570) that can be used to construct resource URIs. @@ -33,10 +33,4 @@ public record ResourceTemplate /// [JsonPropertyName("mimeType")] public string? MimeType { get; init; } - - /// - /// Optional annotations for the resource template. - /// - [JsonPropertyName("annotations")] - public Annotations? Annotations { get; init; } } \ No newline at end of file