@@ -10,6 +10,35 @@ namespace ModelContextProtocol;
1010/// <summary>Provides extension methods for interacting with an <see cref="IMcpEndpoint"/>.</summary>
1111public static class McpEndpointExtensions
1212{
13+ /// <summary>
14+ /// Sends a JSON-RPC request and attempts to deserialize the result to <typeparamref name="TResult"/>.
15+ /// </summary>
16+ /// <typeparam name="TParameters">The type of the request parameters to serialize from.</typeparam>
17+ /// <typeparam name="TResult">The type of the result to deserialize to.</typeparam>
18+ /// <param name="endpoint">The MCP client or server instance.</param>
19+ /// <param name="method">The JSON-RPC method name to invoke.</param>
20+ /// <param name="parameters">Object representing the request parameters.</param>
21+ /// <param name="requestId">The request id for the request.</param>
22+ /// <param name="serializerOptions">The options governing request serialization.</param>
23+ /// <param name="cancellationToken">A token to cancel the operation.</param>
24+ /// <returns>A task that represents the asynchronous operation. The task result contains the deserialized result.</returns>
25+ public static Task < TResult > SendRequestAsync < TParameters , TResult > (
26+ this IMcpEndpoint endpoint ,
27+ string method ,
28+ TParameters parameters ,
29+ JsonSerializerOptions ? serializerOptions = null ,
30+ RequestId ? requestId = null ,
31+ CancellationToken cancellationToken = default )
32+ where TResult : notnull
33+ {
34+ serializerOptions ??= McpJsonUtilities . DefaultOptions ;
35+ serializerOptions . MakeReadOnly ( ) ;
36+
37+ JsonTypeInfo < TParameters > paramsTypeInfo = serializerOptions . GetTypeInfo < TParameters > ( ) ;
38+ JsonTypeInfo < TResult > resultTypeInfo = serializerOptions . GetTypeInfo < TResult > ( ) ;
39+ return SendRequestAsync ( endpoint , method , parameters , paramsTypeInfo , resultTypeInfo , requestId , cancellationToken ) ;
40+ }
41+
1342 /// <summary>
1443 /// Sends a JSON-RPC request and attempts to deserialize the result to <typeparamref name="TResult"/>.
1544 /// </summary>
@@ -23,7 +52,7 @@ public static class McpEndpointExtensions
2352 /// <param name="requestId">The request id for the request.</param>
2453 /// <param name="cancellationToken">A token to cancel the operation.</param>
2554 /// <returns>A task that represents the asynchronous operation. The task result contains the deserialized result.</returns>
26- public static async Task < TResult > SendRequestAsync < TParameters , TResult > (
55+ internal static async Task < TResult > SendRequestAsync < TParameters , TResult > (
2756 this IMcpEndpoint endpoint ,
2857 string method ,
2958 TParameters parameters ,
@@ -53,34 +82,6 @@ public static async Task<TResult> SendRequestAsync<TParameters, TResult>(
5382 return JsonSerializer . Deserialize ( response . Result , resultTypeInfo ) ?? throw new JsonException ( "Unexpected JSON result in response." ) ;
5483 }
5584
56- /// <summary>
57- /// Sends a JSON-RPC request and attempts to deserialize the result to <typeparamref name="TResult"/>.
58- /// </summary>
59- /// <typeparam name="TParameters">The type of the request parameters to serialize from.</typeparam>
60- /// <typeparam name="TResult">The type of the result to deserialize to.</typeparam>
61- /// <param name="endpoint">The MCP client or server instance.</param>
62- /// <param name="method">The JSON-RPC method name to invoke.</param>
63- /// <param name="parameters">Object representing the request parameters.</param>
64- /// <param name="serializerOptions">The options governing request serialization.</param>
65- /// <param name="requestId">The request id for the request.</param>
66- /// <param name="cancellationToken">A token to cancel the operation.</param>
67- /// <returns>A task that represents the asynchronous operation. The task result contains the deserialized result.</returns>
68- public static Task < TResult > SendRequestAsync < TParameters , TResult > (
69- this IMcpEndpoint endpoint ,
70- string method ,
71- TParameters parameters ,
72- JsonSerializerOptions ? serializerOptions = null ,
73- RequestId ? requestId = null ,
74- CancellationToken cancellationToken = default )
75- where TResult : notnull
76- {
77- serializerOptions ??= McpJsonUtilities . DefaultOptions ;
78- McpJsonUtilities . ValidateSerializerOptions ( serializerOptions ) ;
79- JsonTypeInfo < TParameters > paramsTypeInfo = serializerOptions . GetTypeInfo < TParameters > ( ) ;
80- JsonTypeInfo < TResult > resultTypeInfo = serializerOptions . GetTypeInfo < TResult > ( ) ;
81- return SendRequestAsync ( endpoint , method , parameters , paramsTypeInfo , resultTypeInfo , requestId , cancellationToken ) ;
82- }
83-
8485 /// <summary>
8586 /// Sends a notification to the server with parameters.
8687 /// </summary>
@@ -100,21 +101,20 @@ public static Task SendNotificationAsync(this IMcpEndpoint client, string method
100101 /// <param name="endpoint">The MCP client or server instance.</param>
101102 /// <param name="method">The JSON-RPC method name to invoke.</param>
102103 /// <param name="parameters">Object representing the request parameters.</param>
103- /// <param name="parametersTypeInfo ">The type information for request parameter serialization.</param>
104+ /// <param name="serializerOptions ">The options governing request serialization.</param>
104105 /// <param name="cancellationToken">A token to cancel the operation.</param>
105106 public static Task SendNotificationAsync < TParameters > (
106107 this IMcpEndpoint endpoint ,
107108 string method ,
108109 TParameters parameters ,
109- JsonTypeInfo < TParameters > parametersTypeInfo ,
110+ JsonSerializerOptions ? serializerOptions = null ,
110111 CancellationToken cancellationToken = default )
111112 {
112- Throw . IfNull ( endpoint ) ;
113- Throw . IfNullOrWhiteSpace ( method ) ;
114- Throw . IfNull ( parametersTypeInfo ) ;
113+ serializerOptions ??= McpJsonUtilities . DefaultOptions ;
114+ serializerOptions . MakeReadOnly ( ) ;
115115
116- JsonNode ? parametersJson = JsonSerializer . SerializeToNode ( parameters , parametersTypeInfo ) ;
117- return endpoint . SendMessageAsync ( new JsonRpcNotification { Method = method , Params = parametersJson } , cancellationToken ) ;
116+ JsonTypeInfo < TParameters > parametersTypeInfo = serializerOptions . GetTypeInfo < TParameters > ( ) ;
117+ return SendNotificationAsync ( endpoint , method , parameters , parametersTypeInfo , cancellationToken ) ;
118118 }
119119
120120 /// <summary>
@@ -123,19 +123,21 @@ public static Task SendNotificationAsync<TParameters>(
123123 /// <param name="endpoint">The MCP client or server instance.</param>
124124 /// <param name="method">The JSON-RPC method name to invoke.</param>
125125 /// <param name="parameters">Object representing the request parameters.</param>
126- /// <param name="serializerOptions ">The options governing request serialization.</param>
126+ /// <param name="parametersTypeInfo ">The type information for request parameter serialization.</param>
127127 /// <param name="cancellationToken">A token to cancel the operation.</param>
128- public static Task SendNotificationAsync < TParameters > (
128+ internal static Task SendNotificationAsync < TParameters > (
129129 this IMcpEndpoint endpoint ,
130130 string method ,
131131 TParameters parameters ,
132- JsonSerializerOptions ? serializerOptions = null ,
132+ JsonTypeInfo < TParameters > parametersTypeInfo ,
133133 CancellationToken cancellationToken = default )
134134 {
135- serializerOptions ??= McpJsonUtilities . DefaultOptions ;
136- McpJsonUtilities . ValidateSerializerOptions ( serializerOptions ) ;
137- JsonTypeInfo < TParameters > parametersTypeInfo = serializerOptions . GetTypeInfo < TParameters > ( ) ;
138- return SendNotificationAsync ( endpoint , method , parameters , parametersTypeInfo , cancellationToken ) ;
135+ Throw . IfNull ( endpoint ) ;
136+ Throw . IfNullOrWhiteSpace ( method ) ;
137+ Throw . IfNull ( parametersTypeInfo ) ;
138+
139+ JsonNode ? parametersJson = JsonSerializer . SerializeToNode ( parameters , parametersTypeInfo ) ;
140+ return endpoint . SendMessageAsync ( new JsonRpcNotification { Method = method , Params = parametersJson } , cancellationToken ) ;
139141 }
140142
141143 /// <summary>Notifies the connected endpoint of progress.</summary>
0 commit comments