Skip to content
36 changes: 35 additions & 1 deletion src/ModelContextProtocol.Core/McpSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,38 @@ public async Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, Canc
LogSendingRequest(EndpointName, request.Method);
}

await SendToRelatedTransportAsync(request, cancellationToken).ConfigureAwait(false);
// For most requests, wait for either the transport send to complete or for the
// response to arrive via a concurrent channel (e.g. the background GET SSE stream
// in Streamable HTTP). Without this, the foreground transport send could block
// indefinitely waiting for a response that was already delivered via a different stream.
Copy link
Contributor

@halter73 halter73 Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Responses should not be delivered to different streams in practice ignoring Tasks and SSE event stream resumption logic. The protocol spec opens the door for it but definitely discourages it, and our server should never send the response to another stream while leaving the HTTP response for the originating request open. If the conformance server is doing that, that seems like a major bug in the conformance MCP server.

Copy link
Contributor

@stephentoub stephentoub Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the conformance server is doing that, that seems like a major bug in the conformance MCP server.

@halter73, isn't that what the sse-retry test is written to do and test?

The client sends a tools/call via POST.
The server closes the POST SSE stream without sending the response.
The client reconnects via GET (a completely different HTTP request/stream).
The server delivers the tools/call response on the GET stream, a different stream than the one that originated the request.

Copy link
Contributor

@halter73 halter73 Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server closes the POST SSE stream without sending the response.

But it still should complete the HTTP response unblocking StreamableHttpClientSessionTransport.SendMessageAsync. It's best to await it so client users know that as long as all their McpClient requests are complete so are all their HTTP requests, and if not, they can still cancel the underlying HTTP request. It's also nice to let unrecoverable HTTP errors bubble up as an observable exception instead of being forced to look at logs.

//
// For the initialize request, always await the send to completion. The transport's
// SendMessageAsync has side effects (setting SessionId, starting background tasks)
// that must complete before subsequent messages are sent.
if (!tcs.Task.IsCompleted)
{
Task sendTask = SendToRelatedTransportAsync(request, cancellationToken);
if (method == RequestMethods.Initialize ||
sendTask == await Task.WhenAny(sendTask, tcs.Task).ConfigureAwait(false))
{
await sendTask.ConfigureAwait(false);
}
else
{
// The response arrived via a concurrent channel before the transport send completed.
// Let the send finish naturally but observe any faults at debug level.
_ = sendTask.ContinueWith(
Copy link
Contributor

@halter73 halter73 Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this at all. This seems way too cute. We shouldn't complicate the client logic because some servers might be leaving response streams open. I doubt that's what's happening in the conformance test anyway as I point out in #1368 (comment)

static (t, s) =>
{
var handler = (McpSessionHandler)s!;
handler.LogTransportSendFaulted(handler.EndpointName, t.Exception!);
},
this,
CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted,
TaskScheduler.Default);
}
}

// 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
Expand Down Expand Up @@ -1078,4 +1109,7 @@ private static McpProtocolException CreateRemoteProtocolException(JsonRpcError e

[LoggerMessage(Level = LogLevel.Trace, Message = "{EndpointName} session {SessionId} disposed with transport {TransportKind}")]
private partial void LogSessionDisposed(string endpointName, string sessionId, string transportKind);

[LoggerMessage(Level = LogLevel.Debug, Message = "{EndpointName} transport send faulted after response was already received.")]
private partial void LogTransportSendFaulted(string endpointName, Exception exception);
}