@@ -6,6 +6,48 @@ namespace ModelContextProtocol;
66/// <summary>Provides extension methods for interacting with an <see cref="IMcpEndpoint"/>.</summary>
77public static class McpEndpointExtensions
88{
9+ /// <summary>
10+ /// Notifies the connected endpoint of an event.
11+ /// </summary>
12+ /// <param name="endpoint">The endpoint issueing the notification.</param>
13+ /// <param name="notification">The notification to send.</param>
14+ /// <param name="cancellationToken">A token to cancel the operation.</param>
15+ /// <exception cref="ArgumentNullException"><paramref name="endpoint"/> is <see langword="null"/>.</exception>
16+ /// <returns>A task representing the completion of the operation.</returns>
17+ public static Task NotifyAsync (
18+ this IMcpEndpoint endpoint ,
19+ JsonRpcNotification notification ,
20+ CancellationToken cancellationToken = default )
21+ {
22+ Throw . IfNull ( endpoint ) ;
23+
24+ return endpoint . SendMessageAsync ( notification , cancellationToken ) ;
25+ }
26+
27+ /// <summary>
28+ /// Notifies the connected endpoint of an event.
29+ /// </summary>
30+ /// <param name="endpoint">The endpoint issueing the notification.</param>
31+ /// <param name="method">The method to call.</param>
32+ /// <param name="parameters">The parameters to send.</param>
33+ /// <param name="cancellationToken">A token to cancel the operation.</param>
34+ /// <exception cref="ArgumentNullException"><paramref name="endpoint"/> is <see langword="null"/>.</exception>
35+ /// <returns>A task representing the completion of the operation.</returns>
36+ public static Task NotifyAsync (
37+ this IMcpEndpoint endpoint ,
38+ string method ,
39+ object ? parameters = null ,
40+ CancellationToken cancellationToken = default )
41+ {
42+ Throw . IfNull ( endpoint ) ;
43+
44+ return endpoint . NotifyAsync ( new ( )
45+ {
46+ Method = method ,
47+ Params = parameters ,
48+ } , cancellationToken ) ;
49+ }
50+
951 /// <summary>Notifies the connected endpoint of progress.</summary>
1052 /// <param name="endpoint">The endpoint issueing the notification.</param>
1153 /// <param name="progressToken">The <see cref="ProgressToken"/> identifying the operation.</param>
@@ -21,14 +63,38 @@ public static Task NotifyProgressAsync(
2163 {
2264 Throw . IfNull ( endpoint ) ;
2365
24- return endpoint . SendMessageAsync ( new JsonRpcNotification ( )
25- {
26- Method = NotificationMethods . ProgressNotification ,
27- Params = new ProgressNotification ( )
66+ return endpoint . NotifyAsync (
67+ NotificationMethods . ProgressNotification ,
68+ new ProgressNotification ( )
2869 {
2970 ProgressToken = progressToken ,
3071 Progress = progress ,
31- } ,
32- } , cancellationToken ) ;
72+ } , cancellationToken ) ;
73+ }
74+
75+ /// <summary>
76+ /// Notifies the connected endpoint that a request has been cancelled.
77+ /// </summary>
78+ /// <param name="endpoint">The endpoint issueing the notification.</param>
79+ /// <param name="requestId">The ID of the request to cancel.</param>
80+ /// <param name="reason">An optional reason for the cancellation.</param>
81+ /// <param name="cancellationToken">A token to cancel the operation.</param>
82+ /// <returns>A task representing the completion of the operation.</returns>
83+ /// <exception cref="ArgumentNullException"><paramref name="endpoint"/> is <see langword="null"/>.</exception>
84+ public static Task NotifyCancelAsync (
85+ this IMcpEndpoint endpoint ,
86+ RequestId requestId ,
87+ string ? reason = null ,
88+ CancellationToken cancellationToken = default )
89+ {
90+ Throw . IfNull ( endpoint ) ;
91+
92+ return endpoint . NotifyAsync (
93+ NotificationMethods . CancelledNotification ,
94+ new CancelledNotification ( )
95+ {
96+ RequestId = requestId ,
97+ Reason = reason ,
98+ } , cancellationToken ) ;
3399 }
34100}
0 commit comments