1
+ using ModelContextProtocol . Protocol . Messages ;
2
+ using ModelContextProtocol . Shared ;
3
+ using ModelContextProtocol . Utils ;
4
+
5
+ namespace ModelContextProtocol ;
6
+
7
+ /// <summary>Provides extension methods for interacting with an <see cref="IMcpSession"/>.</summary>
8
+ public static class McpEndpointExtensions
9
+ {
10
+ /// <summary>
11
+ /// Notifies the connected endpoint of an event.
12
+ /// </summary>
13
+ /// <param name="endpoint">The endpoint issuing the notification.</param>
14
+ /// <param name="notification">The notification to send.</param>
15
+ /// <param name="cancellationToken">A token to cancel the operation.</param>
16
+ /// <exception cref="ArgumentNullException"><paramref name="endpoint"/> is <see langword="null"/>.</exception>
17
+ /// <returns>A task representing the completion of the operation.</returns>
18
+ public static Task NotifyAsync (
19
+ this IMcpSession endpoint ,
20
+ JsonRpcNotification notification ,
21
+ CancellationToken cancellationToken = default )
22
+ {
23
+ Throw . IfNull ( endpoint ) ;
24
+
25
+ return endpoint . SendMessageAsync ( notification , cancellationToken ) ;
26
+ }
27
+
28
+ /// <summary>
29
+ /// Notifies the connected endpoint of an event.
30
+ /// </summary>
31
+ /// <param name="endpoint">The endpoint issuing the notification.</param>
32
+ /// <param name="method">The method to call.</param>
33
+ /// <param name="parameters">The parameters to send.</param>
34
+ /// <param name="cancellationToken">A token to cancel the operation.</param>
35
+ /// <exception cref="ArgumentNullException"><paramref name="endpoint"/> is <see langword="null"/>.</exception>
36
+ /// <returns>A task representing the completion of the operation.</returns>
37
+ public static Task NotifyAsync (
38
+ this IMcpSession endpoint ,
39
+ string method ,
40
+ object ? parameters = null ,
41
+ CancellationToken cancellationToken = default )
42
+ {
43
+ Throw . IfNull ( endpoint ) ;
44
+
45
+ return endpoint . NotifyAsync ( new ( )
46
+ {
47
+ Method = method ,
48
+ Params = parameters ,
49
+ } , cancellationToken ) ;
50
+ }
51
+
52
+ /// <summary>Notifies the connected endpoint of progress.</summary>
53
+ /// <param name="endpoint">The endpoint issuing the notification.</param>
54
+ /// <param name="progressToken">The <see cref="ProgressToken"/> identifying the operation.</param>
55
+ /// <param name="progress">The progress update to send.</param>
56
+ /// <param name="cancellationToken">A token to cancel the operation.</param>
57
+ /// <returns>A task representing the completion of the operation.</returns>
58
+ /// <exception cref="ArgumentNullException"><paramref name="endpoint"/> is <see langword="null"/>.</exception>
59
+ public static Task NotifyProgressAsync (
60
+ this IMcpSession endpoint ,
61
+ ProgressToken progressToken ,
62
+ ProgressNotificationValue progress ,
63
+ CancellationToken cancellationToken = default )
64
+ {
65
+ Throw . IfNull ( endpoint ) ;
66
+
67
+ return endpoint . NotifyAsync (
68
+ NotificationMethods . ProgressNotification ,
69
+ new ProgressNotification ( )
70
+ {
71
+ ProgressToken = progressToken ,
72
+ Progress = progress ,
73
+ } , cancellationToken ) ;
74
+ }
75
+
76
+ /// <summary>
77
+ /// Notifies the connected endpoint that a request has been cancelled.
78
+ /// </summary>
79
+ /// <param name="endpoint">The endpoint issuing the notification.</param>
80
+ /// <param name="requestId">The ID of the request to cancel.</param>
81
+ /// <param name="reason">An optional reason for the cancellation.</param>
82
+ /// <param name="cancellationToken">A token to cancel the operation.</param>
83
+ /// <returns>A task representing the completion of the operation.</returns>
84
+ /// <exception cref="ArgumentNullException"><paramref name="endpoint"/> is <see langword="null"/>.</exception>
85
+ public static Task NotifyCancelAsync (
86
+ this IMcpSession endpoint ,
87
+ RequestId requestId ,
88
+ string ? reason = null ,
89
+ CancellationToken cancellationToken = default )
90
+ {
91
+ Throw . IfNull ( endpoint ) ;
92
+
93
+ return endpoint . NotifyAsync (
94
+ NotificationMethods . CancelledNotification ,
95
+ new CancelledNotification ( )
96
+ {
97
+ RequestId = requestId ,
98
+ Reason = reason ,
99
+ } , cancellationToken ) ;
100
+ }
101
+ }
0 commit comments