Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions tests/ModelContextProtocol.TestServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ private static ILoggerFactory CreateLoggerFactory()

private static async Task Main(string[] args)
{
if (args.Contains("--echo-cli-arg-and-exit"))
{
Console.Error.WriteLine($"CLI_ARG:{JsonSerializer.Serialize(ParseCliArgument(args))}");
Console.Error.Flush();
return;
}

Log.Logger.Information("Starting server...");

string? cliArg = ParseCliArgument(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ public async Task EscapesCliArgumentsCorrectly(string? cliArgumentValue)
{
Assert.Skip("mono runtime does not handle arguments ending with backslash correctly.");
}


const string OutputPrefix = "CLI_ARG:";
var capturedArgument = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
string cliArgument = $"--cli-arg={cliArgumentValue}";
string testServerExecutable = Path.Combine(AppContext.BaseDirectory, "TestServer.exe");
string testServerDll = Path.Combine(AppContext.BaseDirectory, "TestServer.dll");
Expand All @@ -168,25 +170,34 @@ public async Task EscapesCliArgumentsCorrectly(string? cliArgumentValue)
},
Arguments = (PlatformDetection.IsMonoRuntime, PlatformDetection.IsWindows) switch
{
(true, _) => [testServerExecutable, cliArgument],
(_, true) => [cliArgument],
_ => [testServerDll, cliArgument],
(true, _) => [testServerExecutable, "--echo-cli-arg-and-exit", cliArgument],
(_, true) => ["--echo-cli-arg-and-exit", cliArgument],
_ => [testServerDll, "--echo-cli-arg-and-exit", cliArgument],
},
StandardErrorLines = line =>
{
if (line.StartsWith(OutputPrefix, StringComparison.Ordinal))
{
capturedArgument.TrySetResult(line[OutputPrefix.Length..]);
}
},
};

var transport = new StdioClientTransport(options, LoggerFactory);

// Act: Create client (handshake) and list tools to ensure full round trip works with the argument present.
await using var client = await McpClient.CreateAsync(transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken);
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);

// Assert
Assert.NotNull(tools);
Assert.NotEmpty(tools);

var result = await client.CallToolAsync("echoCliArg", cancellationToken: TestContext.Current.CancellationToken);
var content = Assert.IsType<TextContentBlock>(Assert.Single(result.Content));
Assert.Equal(cliArgumentValue ?? "", content.Text);
await using var session = await transport.ConnectAsync(TestContext.Current.CancellationToken);

string serializedArgument = await capturedArgument.Task.WaitAsync(
TestConstants.DefaultTimeout,
TestContext.Current.CancellationToken);
JsonElement parsedArgument = JsonElement.Parse(serializedArgument);
Assert.Equal(cliArgumentValue ?? "", parsedArgument.GetString());

var exception = await Assert.ThrowsAsync<ClientTransportClosedException>(
async () => await session.MessageReader.Completion.WaitAsync(
TestConstants.DefaultTimeout,
TestContext.Current.CancellationToken));
var completionDetails = Assert.IsType<StdioClientCompletionDetails>(exception.Details);
Assert.Equal(0, completionDetails.ExitCode);
}

[Fact(Skip = "Platform not supported by this test.", SkipUnless = nameof(IsStdErrCallbackSupported))]
Expand Down