HttpListener based SSE Server Support (#73)
This commit is contained in:
parent
4597bbd7c2
commit
b5a26e4c33
1 changed files with 1197 additions and 11 deletions
83
tests/mcpdotnet.Tests/SseServerIntegrationTestFixture.cs
Normal file
83
tests/mcpdotnet.Tests/SseServerIntegrationTestFixture.cs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
using McpDotNet.Client;
|
||||
using McpDotNet.Configuration;
|
||||
using McpDotNet.Protocol.Transport;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace McpDotNet.Tests;
|
||||
|
||||
public class SseServerIntegrationTestFixture : IDisposable
|
||||
{
|
||||
private Process _process;
|
||||
|
||||
public ILoggerFactory LoggerFactory { get; }
|
||||
public McpClientFactory Factory { get; }
|
||||
public McpClientOptions DefaultOptions { get; }
|
||||
public McpServerConfig DefaultConfig { get; }
|
||||
|
||||
public SseServerIntegrationTestFixture()
|
||||
{
|
||||
LoggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(builder =>
|
||||
builder.AddConsole()
|
||||
.SetMinimumLevel(LogLevel.Debug));
|
||||
|
||||
DefaultOptions = new()
|
||||
{
|
||||
ClientInfo = new() { Name = "IntegrationTestClient", Version = "1.0.0" },
|
||||
Capabilities = new() { Sampling = new(), Roots = new() }
|
||||
};
|
||||
|
||||
DefaultConfig = new McpServerConfig
|
||||
{
|
||||
Id = "test_server",
|
||||
Name = "TestServer",
|
||||
TransportType = TransportTypes.Sse,
|
||||
TransportOptions = [],
|
||||
Location = "http://localhost:3001/sse"
|
||||
};
|
||||
|
||||
// Inject the mock transport into the factory
|
||||
Factory = new McpClientFactory(
|
||||
[DefaultConfig],
|
||||
DefaultOptions,
|
||||
LoggerFactory
|
||||
);
|
||||
|
||||
Start();
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
// Start the server (which is at TestSseServer.exe on windows and "dotnet TestSseServer.dll" on linux)
|
||||
var processStartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = OperatingSystem.IsWindows() ? "TestSseServer.exe" : "dotnet",
|
||||
Arguments = "TestSseServer.dll",
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
};
|
||||
|
||||
_process = Process.Start(processStartInfo)
|
||||
?? throw new InvalidOperationException($"Could not start process for {processStartInfo.FileName} with '{processStartInfo.Arguments}'.");
|
||||
|
||||
// Wait 1 second
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = Factory.GetClientAsync("test_server").Result;
|
||||
client.DisposeAsync().AsTask().Wait();
|
||||
LoggerFactory?.Dispose();
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Kill the server process
|
||||
_process.Kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue