From a487b441a7b1f89c36d6c2f696af5eddbe562b0c Mon Sep 17 00:00:00 2001 From: Aaron Powell Date: Wed, 26 Mar 2025 10:01:15 +1100 Subject: [PATCH 1/3] Adding a base class for request params that exposes the progressToken This aligns more with the type structure see in the other SDK's (eg: https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/types.py#L41) while also ensuring they are exposing the JSONRPCRequest (https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json#L771-L806) data. Storing progressToken as an object as it could be an int or string (per https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json#L1268-L1274). Fixes #67 --- .../Protocol/Types/CallToolRequestParams.cs | 2 +- .../Protocol/Types/CompleteRequestParams.cs | 2 +- .../Types/CreateMessageRequestParams.cs | 2 +- .../Protocol/Types/GetPromptRequestParams.cs | 2 +- .../Protocol/Types/InitializeRequestParams.cs | 2 +- .../Protocol/Types/ReadResourceRequestParams.cs | 2 +- .../Protocol/Types/RequestParams.cs | 17 +++++++++++++++++ .../Protocol/Types/RequestParamsMetadata.cs | 16 ++++++++++++++++ .../Protocol/Types/SetLevelRequestParams.cs | 2 +- .../Protocol/Types/SubscribeRequestParams.cs | 2 +- .../Protocol/Types/UnsubscribeRequestParams.cs | 2 +- 11 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 src/ModelContextProtocol/Protocol/Types/RequestParams.cs create mode 100644 src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs diff --git a/src/ModelContextProtocol/Protocol/Types/CallToolRequestParams.cs b/src/ModelContextProtocol/Protocol/Types/CallToolRequestParams.cs index a33d3aac8..3a9b6bd9f 100644 --- a/src/ModelContextProtocol/Protocol/Types/CallToolRequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/CallToolRequestParams.cs @@ -4,7 +4,7 @@ /// Used by the client to invoke a tool provided by the server. /// See the schema for details /// -public class CallToolRequestParams +public class CallToolRequestParams : RequestParams { /// /// Tool name. diff --git a/src/ModelContextProtocol/Protocol/Types/CompleteRequestParams.cs b/src/ModelContextProtocol/Protocol/Types/CompleteRequestParams.cs index cb389914c..eea139da4 100644 --- a/src/ModelContextProtocol/Protocol/Types/CompleteRequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/CompleteRequestParams.cs @@ -4,7 +4,7 @@ /// A request from the client to the server, to ask for completion options. /// See the schema for details /// -public class CompleteRequestParams +public class CompleteRequestParams : RequestParams { /// /// The reference's information diff --git a/src/ModelContextProtocol/Protocol/Types/CreateMessageRequestParams.cs b/src/ModelContextProtocol/Protocol/Types/CreateMessageRequestParams.cs index 02be3364e..329e33dbb 100644 --- a/src/ModelContextProtocol/Protocol/Types/CreateMessageRequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/CreateMessageRequestParams.cs @@ -9,7 +9,7 @@ /// clients have full discretion over model selection and should inform users before sampling. /// See the schema for details /// -public class CreateMessageRequestParams +public class CreateMessageRequestParams : RequestParams { /// /// A request to include context from one or more MCP servers (including the caller), to be attached to the prompt. The client MAY ignore this request. diff --git a/src/ModelContextProtocol/Protocol/Types/GetPromptRequestParams.cs b/src/ModelContextProtocol/Protocol/Types/GetPromptRequestParams.cs index 5c4ce417a..6b05930a3 100644 --- a/src/ModelContextProtocol/Protocol/Types/GetPromptRequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/GetPromptRequestParams.cs @@ -4,7 +4,7 @@ /// Used by the client to get a prompt provided by the server. /// See the schema for details /// -public class GetPromptRequestParams +public class GetPromptRequestParams : RequestParams { /// /// he name of the prompt or prompt template. diff --git a/src/ModelContextProtocol/Protocol/Types/InitializeRequestParams.cs b/src/ModelContextProtocol/Protocol/Types/InitializeRequestParams.cs index 179371aae..5790c4fc6 100644 --- a/src/ModelContextProtocol/Protocol/Types/InitializeRequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/InitializeRequestParams.cs @@ -6,7 +6,7 @@ namespace ModelContextProtocol.Protocol.Types; /// Parameters for an initialization request sent to the server. /// See the schema for details /// -public record InitializeRequestParams +public class InitializeRequestParams : RequestParams { /// /// The version of the Model Context Protocol that the client wants to use. diff --git a/src/ModelContextProtocol/Protocol/Types/ReadResourceRequestParams.cs b/src/ModelContextProtocol/Protocol/Types/ReadResourceRequestParams.cs index fc1c563ef..bf345d73b 100644 --- a/src/ModelContextProtocol/Protocol/Types/ReadResourceRequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/ReadResourceRequestParams.cs @@ -4,7 +4,7 @@ /// Sent from the client to the server, to read a specific resource URI. /// See the schema for details /// -public class ReadResourceRequestParams +public class ReadResourceRequestParams : RequestParams { /// /// The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it. diff --git a/src/ModelContextProtocol/Protocol/Types/RequestParams.cs b/src/ModelContextProtocol/Protocol/Types/RequestParams.cs new file mode 100644 index 000000000..54ca3fd2c --- /dev/null +++ b/src/ModelContextProtocol/Protocol/Types/RequestParams.cs @@ -0,0 +1,17 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace ModelContextProtocol.Protocol.Types; + +/// +/// Base class for all request parameters. +/// See the schema for details +/// +public abstract class RequestParams +{ + /// + /// Metadata related to the tool invocation. + /// + [System.Text.Json.Serialization.JsonPropertyName("_meta")] + public RequestParamsMetadata? Meta { get; init; } +} diff --git a/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs b/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs new file mode 100644 index 000000000..d7e742542 --- /dev/null +++ b/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace ModelContextProtocol.Protocol.Types; + +/// +/// Metadata related to the request. +/// +public class RequestParamsMetadata +{ + /// + /// If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications. + /// + [System.Text.Json.Serialization.JsonPropertyName("progressToken")] + public object ProgressToken { get; set; } = default!; +} diff --git a/src/ModelContextProtocol/Protocol/Types/SetLevelRequestParams.cs b/src/ModelContextProtocol/Protocol/Types/SetLevelRequestParams.cs index 9491cd77b..bdc987ab8 100644 --- a/src/ModelContextProtocol/Protocol/Types/SetLevelRequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/SetLevelRequestParams.cs @@ -6,7 +6,7 @@ namespace ModelContextProtocol.Protocol.Types; /// A request from the client to the server, to enable or adjust logging. /// See the schema for details /// -public class SetLevelRequestParams +public class SetLevelRequestParams : RequestParams { /// /// The level of logging that the client wants to receive from the server. diff --git a/src/ModelContextProtocol/Protocol/Types/SubscribeRequestParams.cs b/src/ModelContextProtocol/Protocol/Types/SubscribeRequestParams.cs index a25715408..e0963679c 100644 --- a/src/ModelContextProtocol/Protocol/Types/SubscribeRequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/SubscribeRequestParams.cs @@ -4,7 +4,7 @@ /// Sent from the client to request updated notifications from the server whenever a particular primitive changes. /// See the schema for details /// -public class SubscribeRequestParams +public class SubscribeRequestParams : RequestParams { /// /// The URI of the resource to subscribe to. The URI can use any protocol; it is up to the server how to interpret it. diff --git a/src/ModelContextProtocol/Protocol/Types/UnsubscribeRequestParams.cs b/src/ModelContextProtocol/Protocol/Types/UnsubscribeRequestParams.cs index 13c4efd21..b138c5bd1 100644 --- a/src/ModelContextProtocol/Protocol/Types/UnsubscribeRequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/UnsubscribeRequestParams.cs @@ -4,7 +4,7 @@ /// Sent from the client to request not receiving updated notifications from the server whenever a primitive resource changes. /// See the schema for details /// -public class UnsubscribeRequestParams +public class UnsubscribeRequestParams : RequestParams { /// /// The URI of the resource to unsubscribe fro. The URI can use any protocol; it is up to the server how to interpret it. From 2ba6aa38b8eabc824bda29348865e2e6b1ca2029 Mon Sep 17 00:00:00 2001 From: Aaron Powell Date: Wed, 26 Mar 2025 13:42:17 +1100 Subject: [PATCH 2/3] Using usings --- .../Protocol/Types/RequestParams.cs | 10 ++++++---- .../Protocol/Types/RequestParamsMetadata.cs | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/ModelContextProtocol/Protocol/Types/RequestParams.cs b/src/ModelContextProtocol/Protocol/Types/RequestParams.cs index 54ca3fd2c..04b1c8b2f 100644 --- a/src/ModelContextProtocol/Protocol/Types/RequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/RequestParams.cs @@ -1,6 +1,8 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Text.Json.Serialization; + namespace ModelContextProtocol.Protocol.Types; /// @@ -12,6 +14,6 @@ public abstract class RequestParams /// /// Metadata related to the tool invocation. /// - [System.Text.Json.Serialization.JsonPropertyName("_meta")] + [JsonPropertyName("_meta")] public RequestParamsMetadata? Meta { get; init; } } diff --git a/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs b/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs index d7e742542..8a852e7ea 100644 --- a/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs +++ b/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs @@ -1,6 +1,8 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Text.Json.Serialization; + namespace ModelContextProtocol.Protocol.Types; /// @@ -11,6 +13,6 @@ public class RequestParamsMetadata /// /// If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications. /// - [System.Text.Json.Serialization.JsonPropertyName("progressToken")] + [JsonPropertyName("progressToken")] public object ProgressToken { get; set; } = default!; } From 311fd806e842003341f3180112b12c528246a7b0 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 25 Mar 2025 23:24:45 -0400 Subject: [PATCH 3/3] Apply suggestions from code review --- src/ModelContextProtocol/Protocol/Types/RequestParams.cs | 3 --- .../Protocol/Types/RequestParamsMetadata.cs | 3 --- 2 files changed, 6 deletions(-) diff --git a/src/ModelContextProtocol/Protocol/Types/RequestParams.cs b/src/ModelContextProtocol/Protocol/Types/RequestParams.cs index 04b1c8b2f..73dbc2a5c 100644 --- a/src/ModelContextProtocol/Protocol/Types/RequestParams.cs +++ b/src/ModelContextProtocol/Protocol/Types/RequestParams.cs @@ -1,6 +1,3 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - using System.Text.Json.Serialization; namespace ModelContextProtocol.Protocol.Types; diff --git a/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs b/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs index 8a852e7ea..919fb8ff3 100644 --- a/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs +++ b/src/ModelContextProtocol/Protocol/Types/RequestParamsMetadata.cs @@ -1,6 +1,3 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - using System.Text.Json.Serialization; namespace ModelContextProtocol.Protocol.Types;