csharp-sdk/tests/ModelContextProtocol.Tests/Server/McpServerFactoryTests.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

45 lines
1.3 KiB
C#

using ModelContextProtocol.Server;
using ModelContextProtocol.Tests.Utils;
namespace ModelContextProtocol.Tests.Server;
public class McpServerFactoryTests : LoggedTest
{
private readonly McpServerOptions _options;
public McpServerFactoryTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
_options = new McpServerOptions
{
ProtocolVersion = "1.0",
InitializationTimeout = TimeSpan.FromSeconds(30)
};
}
[Fact]
public async Task Create_Should_Initialize_With_Valid_Parameters()
{
// Arrange & Act
await using var transport = new TestServerTransport();
await using IMcpServer server = McpServerFactory.Create(transport, _options, LoggerFactory);
// Assert
Assert.NotNull(server);
}
[Fact]
public void Create_Throws_For_Null_ServerTransport()
{
// Arrange, Act & Assert
Assert.Throws<ArgumentNullException>("transport", () => McpServerFactory.Create(null!, _options, LoggerFactory));
}
[Fact]
public async Task Create_Throws_For_Null_Options()
{
// Arrange, Act & Assert
await using var transport = new TestServerTransport();
Assert.Throws<ArgumentNullException>("serverOptions", () => McpServerFactory.Create(transport, null!, LoggerFactory));
}
}