File tree Expand file tree Collapse file tree 1 file changed +14
-0
lines changed
src/ModelContextProtocol/Shared Expand file tree Collapse file tree 1 file changed +14
-0
lines changed Original file line number Diff line number Diff line change @@ -310,6 +310,19 @@ await _transport.SendMessageAsync(new JsonRpcResponse
310310 } , cancellationToken ) . ConfigureAwait ( false ) ;
311311 }
312312
313+ private void RegisterCancellation ( CancellationToken cancellationToken , RequestId requestId )
314+ {
315+ cancellationToken . Register ( async ( ) => await SendMessageAsync ( new JsonRpcNotification
316+ {
317+ Method = NotificationMethods . CancelledNotification ,
318+ Params = JsonSerializer . SerializeToNode ( new CancelledNotification
319+ {
320+ RequestId = requestId ,
321+ Reason = "Request cancelled"
322+ } , McpJsonUtilities . JsonContext . Default . CancelledNotification )
323+ } , cancellationToken ) ) ;
324+ }
325+
313326 /// <summary>
314327 /// Sends a JSON-RPC request to the server.
315328 /// It is strongly recommended use the capability-specific methods instead of this one.
@@ -320,6 +333,7 @@ await _transport.SendMessageAsync(new JsonRpcResponse
320333 /// <returns>A task containing the server's response.</returns>
321334 public async Task < JsonRpcResponse > SendRequestAsync ( JsonRpcRequest request , CancellationToken cancellationToken )
322335 {
336+ RegisterCancellation ( cancellationToken , request . Id ) ;
323337 if ( ! _transport . IsConnected )
324338 {
325339 _logger . EndpointNotConnected ( EndpointName ) ;
You can’t perform that action at this time.
0 commit comments