csharp-sdk/tests/ModelContextProtocol.Tests/SseServerIntegrationTestFixture.cs
Stephen Toub d21d933876
Move notification handler registrations to capabilities (#207)
* Move notification handler registrations to capabilities

Currently request handlers are set on the capability objects, but notification handlers are set after construction via an AddNotificationHandler method on the IMcpEndpoint interface. This moves handler specification to be at construction as well. This makes it more consistent with request handlers, simplifies the IMcpEndpoint interface to just be about message sending, and avoids a concurrency bug that could occur if someone tried to add a handler while the endpoint was processing notifications.

* Address more feedback and further cleanup
2025-04-04 13:11:07 -04:00

57 lines
1.7 KiB
C#

using ModelContextProtocol.Protocol.Transport;
using ModelContextProtocol.Test.Utils;
using ModelContextProtocol.Tests.Utils;
using ModelContextProtocol.TestSseServer;
namespace ModelContextProtocol.Tests;
public class SseServerIntegrationTestFixture : IAsyncDisposable
{
private readonly Task _serverTask;
private readonly CancellationTokenSource _stopCts = new();
private readonly DelegatingTestOutputHelper _delegatingTestOutputHelper = new();
public McpServerConfig DefaultConfig { get; }
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
{
Id = "test_server",
Name = "TestServer",
TransportType = TransportTypes.Sse,
TransportOptions = [],
Location = $"http://localhost:{port}/sse"
};
_serverTask = Program.MainAsync([port.ToString()], new XunitLoggerProvider(_delegatingTestOutputHelper), _stopCts.Token);
}
public void Initialize(ITestOutputHelper output)
{
_delegatingTestOutputHelper.CurrentTestOutputHelper = output;
}
public void TestCompleted()
{
_delegatingTestOutputHelper.CurrentTestOutputHelper = null;
}
public async ValueTask DisposeAsync()
{
_delegatingTestOutputHelper.CurrentTestOutputHelper = null;
_stopCts.Cancel();
try
{
await _serverTask.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
_stopCts.Dispose();
}
}