Skip to content

Commit 8308c3b

Browse files
Copilotstephentoub
andauthored
Add Debug.Assert that Completion is already completed before awaiting it
ConnectAsync's DisposeAsync already awaits Completion, so by the time CreateAsync's catch block runs, it's guaranteed to be completed. The assert documents this invariant. Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/69e63301-5b59-4d6e-bf84-d1e02e421c67 Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent a43d0d8 commit 8308c3b

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.Logging;
22
using ModelContextProtocol.Protocol;
33
using ModelContextProtocol.Server;
4+
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
56
using System.Text.Json;
67
using System.Text.Json.Nodes;
@@ -57,6 +58,7 @@ public static async Task<McpClient> CreateAsync(
5758
// ConnectAsync already disposed the session (which includes awaiting Completion).
5859
// Check if the transport provided structured completion details indicating
5960
// why the transport closed that aren't already in the original exception chain.
61+
Debug.Assert(clientSession.Completion.IsCompleted, "Completion should already be finished after ConnectAsync's DisposeAsync.");
6062
var completionDetails = await clientSession.Completion.ConfigureAwait(false);
6163

6264
// If the transport closed with a non-graceful error (e.g., server process exited)

0 commit comments

Comments
 (0)