Overhaul McpClientFactory/McpServerFactory (#87)
Handlers are now specified before connecting the client/server. Otherwise, race conditions exist. Handlers move into being a part of the McpClientOptions/McpServerOptions, so that they're provided to the factories. Where relevant, the handlers are specified as part of the capability descriptors, so that they go hand in hand. The factories are no longer stateful. Instead of allocating a factory and then calling a create method on it, you just call a static factory method.
This commit is contained in:
parent
63b24a4036
commit
08b0a04dab
1 changed files with 1306 additions and 2243 deletions
|
|
@ -1,8 +1,7 @@
|
|||
using McpDotNet.Protocol.Transport;
|
||||
using McpDotNet.Protocol.Types;
|
||||
using McpDotNet.Server;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
|
||||
namespace McpDotNet.Tests.Server;
|
||||
|
|
@ -10,16 +9,12 @@ namespace McpDotNet.Tests.Server;
|
|||
public class McpServerFactoryTests
|
||||
{
|
||||
private readonly Mock<IServerTransport> _serverTransport;
|
||||
private readonly Mock<ILoggerFactory> _loggerFactory;
|
||||
private readonly Mock<IOptions<McpServerDelegates>> _serverDelegates;
|
||||
private readonly McpServerOptions _options;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public McpServerFactoryTests()
|
||||
{
|
||||
_serverTransport = new Mock<IServerTransport>();
|
||||
_loggerFactory = new Mock<ILoggerFactory>();
|
||||
_serverDelegates = new Mock<IOptions<McpServerDelegates>>();
|
||||
_options = new McpServerOptions
|
||||
{
|
||||
ServerInfo = new Implementation { Name = "TestServer", Version = "1.0" },
|
||||
|
|
@ -30,54 +25,26 @@ public class McpServerFactoryTests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_Should_Initialize_With_Valid_Parameters()
|
||||
public async Task Create_Should_Initialize_With_Valid_Parameters()
|
||||
{
|
||||
// Arrange & Act
|
||||
var factory = new McpServerFactory(_serverTransport.Object, _options, _loggerFactory.Object, _serverDelegates.Object, _serviceProvider);
|
||||
await using IMcpServer server = McpServerFactory.Create(_serverTransport.Object, _options, NullLoggerFactory.Instance);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(factory);
|
||||
Assert.NotNull(server);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_Throws_For_Null_ServerTransport()
|
||||
{
|
||||
// Arrange, Act & Assert
|
||||
Assert.Throws<ArgumentNullException>(() => new McpServerFactory(null!, _options, _loggerFactory.Object, _serverDelegates.Object, _serviceProvider));
|
||||
Assert.Throws<ArgumentNullException>("serverTransport", () => McpServerFactory.Create(null!, _options, NullLoggerFactory.Instance));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_Throws_For_Null_Options()
|
||||
{
|
||||
// Arrange, Act & Assert
|
||||
Assert.Throws<ArgumentNullException>(() => new McpServerFactory(_serverTransport.Object, null!, _loggerFactory.Object, _serverDelegates.Object, _serviceProvider));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_Does_Not_Throw_For_Null_ServerDelegates()
|
||||
{
|
||||
var factory = new McpServerFactory(_serverTransport.Object, _options, _loggerFactory.Object, null, _serviceProvider);
|
||||
Assert.NotNull(factory);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_Does_Not_Throw_For_Null_ServiceProvider()
|
||||
{
|
||||
var factory = new McpServerFactory(_serverTransport.Object, _options, _loggerFactory.Object, _serverDelegates.Object, null);
|
||||
Assert.NotNull(factory);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateServer_Return_IMcpServerInstance()
|
||||
{
|
||||
// Arrange
|
||||
var factory = new McpServerFactory(_serverTransport.Object, _options, _loggerFactory.Object, _serverDelegates.Object, _serviceProvider);
|
||||
|
||||
// Act
|
||||
var server = factory.CreateServer();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(server);
|
||||
Assert.IsAssignableFrom<IMcpServer>(server);
|
||||
Assert.Throws<ArgumentNullException>("serverOptions", () => McpServerFactory.Create(_serverTransport.Object, null!, NullLoggerFactory.Instance));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue