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
This commit is contained in:
parent
8fcdf95785
commit
d21d933876
1 changed files with 292 additions and 288 deletions
|
|
@ -24,7 +24,6 @@ public class McpServerTests : LoggedTest
|
|||
{
|
||||
return new McpServerOptions
|
||||
{
|
||||
ServerInfo = new Implementation { Name = "TestServer", Version = "1.0" },
|
||||
ProtocolVersion = "2024",
|
||||
InitializationTimeout = TimeSpan.FromSeconds(30),
|
||||
Capabilities = capabilities,
|
||||
|
|
@ -189,8 +188,8 @@ public class McpServerTests : LoggedTest
|
|||
{
|
||||
var result = JsonSerializer.Deserialize<InitializeResult>(response);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("TestServer", result.ServerInfo.Name);
|
||||
Assert.Equal("1.0", result.ServerInfo.Version);
|
||||
Assert.Equal("ModelContextProtocol.Tests", result.ServerInfo.Name);
|
||||
Assert.Equal("1.0.0.0", result.ServerInfo.Version);
|
||||
Assert.Equal("2024", result.ProtocolVersion);
|
||||
});
|
||||
}
|
||||
|
|
@ -634,8 +633,6 @@ public class McpServerTests : LoggedTest
|
|||
public Implementation? ClientInfo => throw new NotImplementedException();
|
||||
public McpServerOptions ServerOptions => throw new NotImplementedException();
|
||||
public IServiceProvider? Services => throw new NotImplementedException();
|
||||
public void AddNotificationHandler(string method, Func<JsonRpcNotification, Task> handler) =>
|
||||
throw new NotImplementedException();
|
||||
public Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default) =>
|
||||
throw new NotImplementedException();
|
||||
public Task RunAsync(CancellationToken cancellationToken = default) =>
|
||||
|
|
@ -649,13 +646,16 @@ public class McpServerTests : LoggedTest
|
|||
var options = CreateOptions();
|
||||
|
||||
var notificationReceived = new TaskCompletionSource<JsonRpcNotification>();
|
||||
options.Capabilities = new()
|
||||
{
|
||||
NotificationHandlers = [new(NotificationMethods.ProgressNotification, notification =>
|
||||
{
|
||||
notificationReceived.TrySetResult(notification);
|
||||
return Task.CompletedTask;
|
||||
})],
|
||||
};
|
||||
|
||||
var server = McpServerFactory.Create(transport, options, LoggerFactory);
|
||||
server.AddNotificationHandler(NotificationMethods.ProgressNotification, notification =>
|
||||
{
|
||||
notificationReceived.SetResult(notification);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
Task serverTask = server.RunAsync(TestContext.Current.CancellationToken);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue