Skip to content

Fix and enhance cancellation operations across MCP Sessions. #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Propagate CancellationToken request cancellation to remote endpoint
  • Loading branch information
stephentoub committed Apr 8, 2025
commit 0a73151f9c0d85e2b3c399a16ccf6dddb463fda2
12 changes: 6 additions & 6 deletions src/ModelContextProtocol/McpEndpointExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ public static Task NotifyProgressAsync(
{
Throw.IfNull(endpoint);

return endpoint.SendMessageAsync(new JsonRpcNotification()
{
Method = NotificationMethods.ProgressNotification,
Params = JsonSerializer.SerializeToNode(new ProgressNotification
return endpoint.SendNotificationAsync(
NotificationMethods.ProgressNotification,
new ProgressNotification
{
ProgressToken = progressToken,
Progress = progress,
}, McpJsonUtilities.JsonContext.Default.ProgressNotification),
}, cancellationToken);
},
McpJsonUtilities.JsonContext.Default.ProgressNotification,
cancellationToken);
}
}
29 changes: 27 additions & 2 deletions src/ModelContextProtocol/Shared/McpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ await _transport.SendMessageAsync(new JsonRpcResponse
}, cancellationToken).ConfigureAwait(false);
}

private CancellationTokenRegistration RegisterCancellation(CancellationToken cancellationToken, RequestId requestId)
{
if (!cancellationToken.CanBeCanceled)
{
return default;
}

return cancellationToken.Register(static objState =>
{
var state = (Tuple<McpSession, RequestId>)objState!;
_ = state.Item1.SendMessageAsync(new JsonRpcNotification
{
Method = NotificationMethods.CancelledNotification,
Params = JsonSerializer.SerializeToNode(new CancelledNotification { RequestId = state.Item2 }, McpJsonUtilities.JsonContext.Default.CancelledNotification)
});
}, Tuple.Create(this, requestId));
}

public IAsyncDisposable RegisterNotificationHandler(string method, Func<JsonRpcNotification, CancellationToken, Task> handler)
{
Throw.IfNullOrWhiteSpace(method);
Expand Down Expand Up @@ -357,9 +375,16 @@ public async Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, Canc
_logger.SendingRequest(EndpointName, request.Method);

await _transport.SendMessageAsync(request, cancellationToken).ConfigureAwait(false);

_logger.RequestSentAwaitingResponse(EndpointName, request.Method, request.Id.ToString());
var response = await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(false);

// Now that the request has been sent, register for cancellation. If we registered before,
// a cancellation request could arrive before the server knew about that request ID, in which
// case the server could ignore it.
IJsonRpcMessage? response;
using (var registration = RegisterCancellation(cancellationToken, request.Id))
{
response = await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(false);
}

if (response is JsonRpcError error)
{
Expand Down