Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/ModelContextProtocol.Core/Client/StdioClientTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ public async Task<ITransport> ConnectAsync(CancellationToken cancellationToken =

LogTransportProcessStarted(logger, endpointName, process.Id);

process.BeginErrorReadLine();
// Suppress ExecutionContext flow so the Process's internal async
// stderr reader thread doesn't capture the caller's ambient context
// (e.g. AsyncLocal values from test infrastructure or HTTP request state).
using (ExecutionContext.SuppressFlow())
{
process.BeginErrorReadLine();
}

return new StdioClientSessionTransport(_options, process, endpointName, stderrRollingLog, errorHandler, _loggerFactory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading;

namespace ModelContextProtocol.Tests.Transport;

Expand Down Expand Up @@ -60,6 +61,46 @@
}

[Fact(Skip = "Platform not supported by this test.", SkipUnless = nameof(IsStdErrCallbackSupported))]
<<<<<<< suppress-ec-flow-stderr

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered

Check failure on line 64 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered
public async Task CreateAsync_StdErrCallback_DoesNotCaptureCallerAsyncLocal()
{
var asyncLocal = new AsyncLocal<string>();
asyncLocal.Value = "caller-context";

string? capturedValue = "not-set";
var received = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

StdioClientTransport transport = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
new(new()
{
Command = "cmd",
Arguments = ["/c", "echo test >&2 & exit /b 1"],
StandardErrorLines = _ =>
{
capturedValue = asyncLocal.Value;
received.TrySetResult(true);
}
}, LoggerFactory) :
new(new()
{
Command = "sh",
Arguments = ["-c", "echo test >&2; exit 1"],
StandardErrorLines = _ =>
{
capturedValue = asyncLocal.Value;
received.TrySetResult(true);
}
}, LoggerFactory);

await Assert.ThrowsAnyAsync<IOException>(() => McpClient.CreateAsync(transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken));

// Wait for the stderr callback to fire.
await received.Task.WaitAsync(TestContext.Current.CancellationToken);

// The callback should NOT see the caller's AsyncLocal value because
// ExecutionContext flow is suppressed for the stderr reader thread.
Assert.Null(capturedValue);
=======

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered

Check failure on line 103 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered
public async Task CreateAsync_StdErrCallbackThrows_DoesNotCrashProcess()
{
StdioClientTransport transport = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
Expand All @@ -68,6 +109,7 @@

// Should throw IOException for the failed server, not crash the host process.
await Assert.ThrowsAnyAsync<IOException>(() => McpClient.CreateAsync(transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken));
>>>>>>> main

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered

Check failure on line 112 in tests/ModelContextProtocol.Tests/Transport/StdioClientTransportTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Merge conflict marker encountered
}

[Theory]
Expand Down
Loading