File tree Expand file tree Collapse file tree
src/ModelContextProtocol.Core/Client Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -52,15 +52,14 @@ public static async Task<McpClient> CreateAsync(
5252 {
5353 await clientSession . ConnectAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
5454 }
55- catch ( Exception ex ) when ( ex is not TransportClosedException )
55+ catch ( Exception ex ) when ( ex is not OperationCanceledException and not TransportClosedException )
5656 {
57- // ConnectAsync already disposed the session. Call DisposeAsync again (no-op)
58- // to ensure cleanup, then check if the transport provided structured completion
59- // details indicating why the transport closed.
57+ // ConnectAsync already disposed the session (which includes awaiting Completion).
58+ // Check if the transport provided structured completion details indicating
59+ // why the transport closed that aren't already in the original exception chain .
6060 ClientCompletionDetails ? completionDetails = null ;
6161 try
6262 {
63- await clientSession . DisposeAsync ( ) . ConfigureAwait ( false ) ;
6463 completionDetails = await clientSession . Completion . ConfigureAwait ( false ) ;
6564 }
6665 catch { } // allow the original exception to propagate if completion is unavailable
@@ -79,19 +78,6 @@ public static async Task<McpClient> CreateAsync(
7978
8079 throw ;
8180 }
82- catch
83- {
84- // The exception is already a TransportClosedException (e.g., from
85- // ProcessMessagesCoreAsync propagating channel completion details).
86- // Just ensure cleanup and re-throw.
87- try
88- {
89- await clientSession . DisposeAsync ( ) . ConfigureAwait ( false ) ;
90- }
91- catch { }
92-
93- throw ;
94- }
9581
9682 return clientSession ;
9783 }
You can’t perform that action at this time.
0 commit comments