Use Kestrel for all in-memory HTTP tests (#225)
* Add route pattern parameter to MapMcp * Add configureOptionsAsync * Use Kestrel for all in-memory HTTP tests * Use ApplicationStopping token for SSE responses * Use RegisterNotificationHandler in MapMcp tests * Work around SocketsHttpHandler bug where it doesn't call DispseAsync on the stream returned by the ConnectCallback
This commit is contained in:
parent
848c9df433
commit
faf12b6a94
1 changed files with 631 additions and 642 deletions
|
|
@ -1,4 +1,6 @@
|
|||
using ModelContextProtocol.Protocol.Transport;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ModelContextProtocol.Client;
|
||||
using ModelContextProtocol.Protocol.Transport;
|
||||
using ModelContextProtocol.Test.Utils;
|
||||
using ModelContextProtocol.Tests.Utils;
|
||||
using ModelContextProtocol.TestSseServer;
|
||||
|
|
@ -7,28 +9,52 @@ namespace ModelContextProtocol.Tests;
|
|||
|
||||
public class SseServerIntegrationTestFixture : IAsyncDisposable
|
||||
{
|
||||
private readonly KestrelInMemoryTransport _inMemoryTransport = new();
|
||||
|
||||
private readonly Task _serverTask;
|
||||
private readonly CancellationTokenSource _stopCts = new();
|
||||
|
||||
// XUnit's ITestOutputHelper is created per test, while this fixture is used for
|
||||
// multiple tests, so this dispatches the output to the current test.
|
||||
private readonly DelegatingTestOutputHelper _delegatingTestOutputHelper = new();
|
||||
|
||||
public McpServerConfig DefaultConfig { get; }
|
||||
private McpServerConfig DefaultServerConfig { get; } = new McpServerConfig
|
||||
{
|
||||
Id = "test_server",
|
||||
Name = "TestServer",
|
||||
TransportType = TransportTypes.Sse,
|
||||
TransportOptions = [],
|
||||
Location = $"http://localhost/sse"
|
||||
};
|
||||
|
||||
public SseServerIntegrationTestFixture()
|
||||
{
|
||||
// Ensure that test suites running against different TFMs and possibly concurrently use different port numbers.
|
||||
int port = 3001 + Environment.Version.Major;
|
||||
|
||||
DefaultConfig = new McpServerConfig
|
||||
var socketsHttpHandler = new SocketsHttpHandler()
|
||||
{
|
||||
Id = "test_server",
|
||||
Name = "TestServer",
|
||||
TransportType = TransportTypes.Sse,
|
||||
TransportOptions = [],
|
||||
Location = $"http://localhost:{port}/sse"
|
||||
ConnectCallback = (context, token) =>
|
||||
{
|
||||
var connection = _inMemoryTransport.CreateConnection();
|
||||
return new(connection.ClientStream);
|
||||
},
|
||||
};
|
||||
|
||||
_serverTask = Program.MainAsync([port.ToString()], new XunitLoggerProvider(_delegatingTestOutputHelper), _stopCts.Token);
|
||||
HttpClient = new HttpClient(socketsHttpHandler)
|
||||
{
|
||||
BaseAddress = new Uri(DefaultServerConfig.Location),
|
||||
};
|
||||
_serverTask = Program.MainAsync([], new XunitLoggerProvider(_delegatingTestOutputHelper), _inMemoryTransport, _stopCts.Token);
|
||||
}
|
||||
|
||||
public HttpClient HttpClient { get; }
|
||||
|
||||
public Task<IMcpClient> ConnectMcpClientAsync(McpClientOptions? options, ILoggerFactory loggerFactory)
|
||||
{
|
||||
return McpClientFactory.CreateAsync(
|
||||
DefaultServerConfig,
|
||||
options,
|
||||
(_, _) => new SseClientTransport(new(), DefaultServerConfig, HttpClient, loggerFactory),
|
||||
loggerFactory,
|
||||
TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
public void Initialize(ITestOutputHelper output)
|
||||
|
|
@ -44,7 +70,10 @@ public class SseServerIntegrationTestFixture : IAsyncDisposable
|
|||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
_delegatingTestOutputHelper.CurrentTestOutputHelper = null;
|
||||
|
||||
HttpClient.Dispose();
|
||||
_stopCts.Cancel();
|
||||
|
||||
try
|
||||
{
|
||||
await _serverTask.ConfigureAwait(false);
|
||||
|
|
@ -52,6 +81,7 @@ public class SseServerIntegrationTestFixture : IAsyncDisposable
|
|||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
|
||||
_stopCts.Dispose();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue