csharp-sdk/tests/ModelContextProtocol.Tests/EverythingSseServerFixture.cs
Stephen Halter 9c124b1770
Add StreamableHttpHandler and WithHttpTransport (#291)
* Simplify ModelContextProtocol.AspNetCore README

* Add StreamableHttpHandler

* Use "docker info" in CheckIsDockerAvailable

* Call WithHttpTransport in samples, tests and README

* Cleanup test namespaces

* Add CanConnect_WithMcpClient_AfterCustomizingRoute test

* Simplify relative URI handling

* Handle request made directly to the MapMcp route pattern

* Add Messages_FromNewUser_AreRejected test

* Fix README

* Shorten UserIdClaim ValueTuple names

* Remove MaxReconnectAttempts and ReconnectDelay from SseClientTransportOptions

- Add proper AdditionalHeaders support
2025-04-14 10:15:33 -04:00

82 lines
No EOL
2.5 KiB
C#

using System.Diagnostics;
namespace ModelContextProtocol.Tests;
public class EverythingSseServerFixture : IAsyncDisposable
{
private readonly int _port;
private readonly string _containerName;
public static bool IsDockerAvailable => _isDockerAvailable ??= CheckIsDockerAvailable();
private static bool? _isDockerAvailable;
public EverythingSseServerFixture(int port)
{
_port = port;
_containerName = $"mcp-everything-server-{_port}";
}
public async Task StartAsync()
{
var processStartInfo = new ProcessStartInfo
{
FileName = "docker",
Arguments = $"run -p {_port}:3001 --name {_containerName} --rm tzolov/mcp-everything-server:v1",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
_ = Process.Start(processStartInfo)
?? throw new InvalidOperationException($"Could not start process for {processStartInfo.FileName} with '{processStartInfo.Arguments}'.");
// Wait for the server to start
await Task.Delay(10000);
}
public async ValueTask DisposeAsync()
{
try
{
// Stop the container
var stopInfo = new ProcessStartInfo
{
FileName = "docker",
Arguments = $"stop {_containerName}",
UseShellExecute = false
};
using var stopProcess = Process.Start(stopInfo)
?? throw new InvalidOperationException($"Could not stop process for {stopInfo.FileName} with '{stopInfo.Arguments}'.");
await stopProcess.WaitForExitAsync();
}
catch (Exception ex)
{
// Log the exception but don't throw
await Console.Error.WriteLineAsync($"Error stopping Docker container: {ex.Message}");
}
}
private static bool CheckIsDockerAvailable()
{
try
{
ProcessStartInfo processStartInfo = new()
{
FileName = "docker",
// "docker info" returns a non-zero exit code if docker engine is not running.
Arguments = "info",
UseShellExecute = false,
};
using var process = Process.Start(processStartInfo);
process?.WaitForExit();
return process?.ExitCode is 0;
}
catch
{
return false;
}
}
}