Skip to content

Commit a43d0d8

Browse files
Copilotstephentoub
andauthored
Remove unnecessary try/catch around Completion await
GetCompletionDetailsAsync already catches all exceptions internally and always returns a ClientCompletionDetails, so Completion never faults. Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/e1a0f673-063d-4707-916d-51824e00b557 Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent fde5d08 commit a43d0d8

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

src/ModelContextProtocol.Core/Client/McpClient.Methods.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,15 @@ public static async Task<McpClient> CreateAsync(
5757
// ConnectAsync already disposed the session (which includes awaiting Completion).
5858
// Check if the transport provided structured completion details indicating
5959
// why the transport closed that aren't already in the original exception chain.
60-
ClientCompletionDetails? completionDetails = null;
61-
try
62-
{
63-
completionDetails = await clientSession.Completion.ConfigureAwait(false);
64-
}
65-
catch { } // allow the original exception to propagate if completion is unavailable
60+
var completionDetails = await clientSession.Completion.ConfigureAwait(false);
6661

6762
// If the transport closed with a non-graceful error (e.g., server process exited)
6863
// and the completion details carry an exception that's NOT already in the original
6964
// exception chain, throw a TransportClosedException with the structured details so
7065
// callers can programmatically inspect the closure reason (exit code, stderr, etc.).
7166
// When the same exception is already in the chain (e.g., HttpRequestException from
7267
// an HTTP transport), the original exception is more appropriate to re-throw.
73-
if (completionDetails?.Exception is { } detailsException &&
68+
if (completionDetails.Exception is { } detailsException &&
7469
!ExceptionChainContains(ex, detailsException))
7570
{
7671
throw new TransportClosedException(completionDetails);

0 commit comments

Comments
 (0)