@@ -100,7 +100,7 @@ internal async Task<HttpResponseMessage> SendHttpRequestAsync(JsonRpcMessage mes
100100 // indicates the session has ended. Signal completion so McpClient.Completion resolves.
101101 if ( response . StatusCode == HttpStatusCode . NotFound && SessionId is not null )
102102 {
103- SetSessionExpired ( response . StatusCode ) ;
103+ SetSessionExpired ( ) ;
104104 }
105105
106106 return response ;
@@ -198,6 +198,8 @@ public override async ValueTask DisposeAsync()
198198 // This class isn't directly exposed to public callers, so we don't have to worry about changing the _state in this case.
199199 if ( _options . TransportMode is not HttpTransportMode . AutoDetect || _getReceiveTask is not null )
200200 {
201+ // _disconnectError is set when the server returns 404 indicating session expiry.
202+ // When null, this is a graceful client-initiated closure (no error).
201203 SetDisconnected ( _disconnectError ?? new TransportClosedException ( new HttpClientCompletionDetails ( ) ) ) ;
202204 }
203205 }
@@ -292,7 +294,7 @@ await SendGetSseRequestWithRetriesAsync(
292294 // indicates the session has ended. Signal completion so McpClient.Completion resolves.
293295 if ( response . StatusCode == HttpStatusCode . NotFound && SessionId is not null )
294296 {
295- SetSessionExpired ( response . StatusCode ) ;
297+ SetSessionExpired ( ) ;
296298 }
297299
298300 // If the server could be reached but returned a non-success status code,
@@ -485,13 +487,13 @@ private static TimeSpan ElapsedSince(long stopwatchTimestamp)
485487#endif
486488 }
487489
488- private void SetSessionExpired ( HttpStatusCode statusCode )
490+ private void SetSessionExpired ( )
489491 {
490492 // Store the error before canceling so DisposeAsync can use it if it races us, especially
491493 // after the call to Cancel below, to invoke SetDisconnected.
492494 _disconnectError = new TransportClosedException ( new HttpClientCompletionDetails
493495 {
494- HttpStatusCode = statusCode ,
496+ HttpStatusCode = HttpStatusCode . NotFound ,
495497 Exception = new McpException (
496498 "The server returned HTTP 404 for a request with an Mcp-Session-Id, indicating the session has expired. " +
497499 "To continue, create a new client session or call ResumeSessionAsync with a new connection." ) ,
0 commit comments