Skip to content

Commit 9c5781f

Browse files
committed
Address feedback and fix test failure
1 parent 031cae4 commit 9c5781f

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

src/ModelContextProtocol/Protocol/Transport/StdioClientSessionTransport.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,21 @@ protected override async ValueTask CleanupAsync(Exception? error = null, Cancell
8686
}
8787
catch { }
8888

89-
string errorMessage = "MCP server process exited unexpectedly.";
89+
string errorMessage = "MCP server process exited unexpectedly";
90+
91+
string? exitCode = null;
92+
try
93+
{
94+
exitCode = $" (exit code: {(uint)_process.ExitCode})";
95+
}
96+
catch { }
97+
9098
lock (_stderrRollingLog)
9199
{
92100
if (_stderrRollingLog.Count > 0)
93101
{
94102
errorMessage =
95-
$"{errorMessage}{Environment.NewLine}" +
103+
$"{errorMessage}{exitCode}{Environment.NewLine}" +
96104
$"Server's stderr tail:{Environment.NewLine}" +
97105
$"{string.Join(Environment.NewLine, _stderrRollingLog)}";
98106
}

src/ModelContextProtocol/Protocol/Transport/StreamClientSessionTransport.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ public StreamClientSessionTransport(
5959
/// <inheritdoc/>
6060
public override async Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default)
6161
{
62-
if (!IsConnected)
63-
{
64-
throw new InvalidOperationException("Transport is not connected.");
65-
}
66-
6762
string id = "(no id)";
6863
if (message is JsonRpcMessageWithId messageWithId)
6964
{
@@ -82,7 +77,7 @@ public override async Task SendMessageAsync(JsonRpcMessage message, Cancellation
8277
catch (Exception ex)
8378
{
8479
LogTransportSendFailed(Name, id, ex);
85-
throw new IOException("Failed to send message", ex);
80+
throw new IOException("Failed to send message.", ex);
8681
}
8782
}
8883

src/ModelContextProtocol/Protocol/Transport/StreamServerTransport.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override async Task SendMessageAsync(JsonRpcMessage message, Cancellation
6060
{
6161
if (!IsConnected)
6262
{
63-
throw new InvalidOperationException("Transport is not connected.");
63+
return;
6464
}
6565

6666
using var _ = await _sendLock.LockAsync(cancellationToken).ConfigureAwait(false);

src/ModelContextProtocol/Protocol/Transport/TransportBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected void SetConnected()
115115
return;
116116

117117
case StateDisconnected:
118-
throw new InvalidOperationException("Transport is already disconnected and can't be reconnected.");
118+
throw new IOException("Transport is already disconnected and can't be reconnected.");
119119

120120
default:
121121
Debug.Fail($"Unexpected state: {state}");

src/ModelContextProtocol/Shared/McpEndpoint.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,18 @@ public virtual async ValueTask DisposeUnsynchronizedAsync()
125125
}
126126

127127
protected McpSession GetSessionOrThrow()
128-
=> _session ?? throw new InvalidOperationException($"This should be unreachable from public API! Call {nameof(InitializeSession)} before sending messages.");
128+
{
129+
#if NET
130+
ObjectDisposedException.ThrowIf(_disposed, this);
131+
#else
132+
if (_disposed)
133+
{
134+
throw new ObjectDisposedException(GetType().Name);
135+
}
136+
#endif
137+
138+
return _session ?? throw new InvalidOperationException($"This should be unreachable from public API! Call {nameof(InitializeSession)} before sending messages.");
139+
}
129140

130141
[LoggerMessage(Level = LogLevel.Information, Message = "{EndpointName} shutting down.")]
131142
private partial void LogEndpointShuttingDown(string endpointName);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ModelContextProtocol.Client;
22
using ModelContextProtocol.Protocol.Transport;
3+
using System.Runtime.InteropServices;
34

45
namespace ModelContextProtocol.Tests.Transport;
56

@@ -8,13 +9,11 @@ public class StdioClientTransportTests
89
[Fact]
910
public async Task CreateAsync_ValidProcessInvalidServer_Throws()
1011
{
11-
StdioClientTransport transport = new(new() { Command = "echo", Arguments = ["this is a test", "1>&2"] });
12+
StdioClientTransport transport = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
13+
new(new() { Command = "cmd", Arguments = ["/C", "echo \"this is a test\" >&2"] }) :
14+
new(new() { Command = "sh", Arguments = ["-c", "echo this is a test 1>&2"] });
1215

1316
IOException e = await Assert.ThrowsAsync<IOException>(() => McpClientFactory.CreateAsync(transport, cancellationToken: TestContext.Current.CancellationToken));
14-
string exStr = e.ToString();
15-
if (!exStr.Contains("this is a test"))
16-
{
17-
throw new Exception($"Expected error message not found in exception: {exStr}");
18-
}
17+
Assert.Contains("this is a test", e.ToString());
1918
}
2019
}

0 commit comments

Comments
 (0)