11using ModelContextProtocol . Protocol . Messages ;
22using ModelContextProtocol . Utils ;
3+ using ModelContextProtocol . Utils . Json ;
4+ using System . Text . Json ;
5+ using System . Text . Json . Nodes ;
6+ using System . Text . Json . Serialization . Metadata ;
37
48namespace ModelContextProtocol ;
59
610/// <summary>Provides extension methods for interacting with an <see cref="IMcpEndpoint"/>.</summary>
711public static class McpEndpointExtensions
812{
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="parametersTypeInfo">The type information for request parameter serialization.</param>
22+ /// <param name="resultTypeInfo">The type information for request parameter deserialization.</param>
23+ /// <param name="requestId">The request id for the request.</param>
24+ /// <param name="cancellationToken">A token to cancel the operation.</param>
25+ /// <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 > (
27+ this IMcpEndpoint endpoint ,
28+ string method ,
29+ TParameters parameters ,
30+ JsonTypeInfo < TParameters > parametersTypeInfo ,
31+ JsonTypeInfo < TResult > resultTypeInfo ,
32+ RequestId ? requestId = null ,
33+ CancellationToken cancellationToken = default )
34+ where TResult : notnull
35+ {
36+ Throw . IfNull ( endpoint ) ;
37+ Throw . IfNullOrWhiteSpace ( method ) ;
38+ Throw . IfNull ( parametersTypeInfo ) ;
39+ Throw . IfNull ( resultTypeInfo ) ;
40+
41+ JsonRpcRequest jsonRpcRequest = new ( )
42+ {
43+ Method = method ,
44+ Params = JsonSerializer . SerializeToNode ( parameters , parametersTypeInfo ) ,
45+ } ;
46+
47+ if ( requestId is { } id )
48+ {
49+ jsonRpcRequest . Id = id ;
50+ }
51+
52+ JsonRpcResponse response = await endpoint . SendRequestAsync ( jsonRpcRequest , cancellationToken ) . ConfigureAwait ( false ) ;
53+ return JsonSerializer . Deserialize ( response . Result , resultTypeInfo ) ?? throw new JsonException ( "Unexpected JSON result in response." ) ;
54+ }
55+
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+ JsonTypeInfo < TParameters > paramsTypeInfo = serializerOptions . GetTypeInfo < TParameters > ( ) ;
79+ JsonTypeInfo < TResult > resultTypeInfo = serializerOptions . GetTypeInfo < TResult > ( ) ;
80+ return SendRequestAsync ( endpoint , method , parameters , paramsTypeInfo , resultTypeInfo , requestId , cancellationToken ) ;
81+ }
82+
83+ /// <summary>
84+ /// Sends a notification to the server with parameters.
85+ /// </summary>
86+ /// <param name="client">The client.</param>
87+ /// <param name="method">The notification method name.</param>
88+ /// <param name="cancellationToken">A token to cancel the operation.</param>
89+ public static Task SendNotificationAsync ( this IMcpEndpoint client , string method , CancellationToken cancellationToken = default )
90+ {
91+ Throw . IfNull ( client ) ;
92+ Throw . IfNullOrWhiteSpace ( method ) ;
93+ return client . SendMessageAsync ( new JsonRpcNotification { Method = method } , cancellationToken ) ;
94+ }
95+
96+ /// <summary>
97+ /// Sends a notification to the server with parameters.
98+ /// </summary>
99+ /// <param name="endpoint">The MCP client or server instance.</param>
100+ /// <param name="method">The JSON-RPC method name to invoke.</param>
101+ /// <param name="parameters">Object representing the request parameters.</param>
102+ /// <param name="parametersTypeInfo">The type information for request parameter serialization.</param>
103+ /// <param name="cancellationToken">A token to cancel the operation.</param>
104+ public static Task SendNotificationAsync < TParameters > (
105+ this IMcpEndpoint endpoint ,
106+ string method ,
107+ TParameters parameters ,
108+ JsonTypeInfo < TParameters > parametersTypeInfo ,
109+ CancellationToken cancellationToken = default )
110+ {
111+ Throw . IfNull ( endpoint ) ;
112+ Throw . IfNullOrWhiteSpace ( method ) ;
113+ Throw . IfNull ( parametersTypeInfo ) ;
114+
115+ JsonNode ? parametersJson = JsonSerializer . SerializeToNode ( parameters , parametersTypeInfo ) ;
116+ return endpoint . SendMessageAsync ( new JsonRpcNotification { Method = method , Params = parametersJson } , cancellationToken ) ;
117+ }
118+
119+ /// <summary>
120+ /// Sends a notification to the server with parameters.
121+ /// </summary>
122+ /// <param name="endpoint">The MCP client or server instance.</param>
123+ /// <param name="method">The JSON-RPC method name to invoke.</param>
124+ /// <param name="parameters">Object representing the request parameters.</param>
125+ /// <param name="serializerOptions">The options governing request serialization.</param>
126+ /// <param name="cancellationToken">A token to cancel the operation.</param>
127+ public static Task SendNotificationAsync < TParameters > (
128+ this IMcpEndpoint endpoint ,
129+ string method ,
130+ TParameters parameters ,
131+ JsonSerializerOptions ? serializerOptions = null ,
132+ CancellationToken cancellationToken = default )
133+ {
134+ serializerOptions ??= McpJsonUtilities . DefaultOptions ;
135+ JsonTypeInfo < TParameters > parametersTypeInfo = serializerOptions . GetTypeInfo < TParameters > ( ) ;
136+ return SendNotificationAsync ( endpoint , method , parameters , parametersTypeInfo , cancellationToken ) ;
137+ }
138+
9139 /// <summary>Notifies the connected endpoint of progress.</summary>
10- /// <param name="endpoint">The endpoint issueing the notification.</param>
140+ /// <param name="endpoint">The endpoint issuing the notification.</param>
11141 /// <param name="progressToken">The <see cref="ProgressToken"/> identifying the operation.</param>
12142 /// <param name="progress">The progress update to send.</param>
13143 /// <param name="cancellationToken">A token to cancel the operation.</param>
@@ -24,11 +154,11 @@ public static Task NotifyProgressAsync(
24154 return endpoint . SendMessageAsync ( new JsonRpcNotification ( )
25155 {
26156 Method = NotificationMethods . ProgressNotification ,
27- Params = new ProgressNotification ( )
157+ Params = JsonSerializer . SerializeToNode ( new ProgressNotification
28158 {
29159 ProgressToken = progressToken ,
30160 Progress = progress ,
31- } ,
161+ } , McpJsonUtilities . JsonContext . Default . ProgressNotification ) ,
32162 } , cancellationToken ) ;
33163 }
34164}
0 commit comments