Skip to content

Commit be733e2

Browse files
Adding Cancellation back to McpSession for all requests. Accidental removal from bad merge.
1 parent 46cff23 commit be733e2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/ModelContextProtocol/Shared/McpSession.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)