Skip to content

Commit 575c2bc

Browse files
committed
Use RegisterNotificationHandler in MapMcp tests
1 parent 7afc4bd commit 575c2bc

1 file changed

Lines changed: 12 additions & 42 deletions

File tree

tests/ModelContextProtocol.Tests/SseIntegrationTests.cs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -72,56 +72,26 @@ public async Task ConnectAndReceiveNotification_InMemoryServer()
7272
var receivedNotification = new TaskCompletionSource<string?>();
7373

7474
await using var app = Builder.Build();
75-
app.MapMcp(configureOptionsAsync: (httpContext, mcpServerOptions, CancellationToken) =>
75+
app.MapMcp(runSessionAsync: (httpContext, mcpServer, cancellationToken) =>
7676
{
77-
mcpServerOptions.Capabilities = new()
77+
mcpServer.RegisterNotificationHandler("test/notification", async (notification, cancellationToken) =>
7878
{
79-
NotificationHandlers =
80-
[
81-
new("test/notification", async notification =>
82-
{
83-
Assert.Equal("Hello from client!", notification.Params?["message"]?.GetValue<string>());
84-
85-
var server = httpContext.Features.GetRequiredFeature<IMcpServer>();
86-
87-
// REVIEW: Where is the CancellationToken for notification handlers?
88-
// The httpContext.RequestAborted trick will not always work once we fully support the HTTP streaming spec.
89-
await server.SendNotificationAsync("test/notification", new { message = "Hello from server!" }, cancellationToken: httpContext.RequestAborted);
90-
}),
91-
],
92-
};
93-
94-
return Task.CompletedTask;
79+
Assert.Equal("Hello from client!", notification.Params?["message"]?.GetValue<string>());
80+
var server = httpContext.Features.GetRequiredFeature<IMcpServer>();
81+
await server.SendNotificationAsync("test/notification", new { message = "Hello from server!" }, cancellationToken: cancellationToken);
82+
});
83+
return mcpServer.RunAsync(cancellationToken);
9584
});
9685
await app.StartAsync(TestContext.Current.CancellationToken);
9786

9887
using var httpClient = CreateHttpClient();
99-
await using var mcpClient = await ConnectMcpClient(httpClient, new()
100-
{
101-
Capabilities = new()
102-
{
103-
NotificationHandlers = [new("test/notification", args =>
104-
{
105-
var msg = args.Params?["message"]?.GetValue<string>();
106-
receivedNotification.SetResult(msg);
107-
108-
return Task.CompletedTask;
109-
})],
110-
},
111-
});
88+
await using var mcpClient = await ConnectMcpClient(httpClient);
11289

113-
await using var mcpClient2 = await ConnectMcpClient(httpClient, new()
90+
mcpClient.RegisterNotificationHandler("test/notification", (args, ca) =>
11491
{
115-
Capabilities = new()
116-
{
117-
NotificationHandlers = [new("test/notification", args =>
118-
{
119-
var msg = args.Params?["message"]?.GetValue<string>();
120-
receivedNotification.SetResult(msg);
121-
122-
return Task.CompletedTask;
123-
})],
124-
},
92+
var msg = args.Params?["message"]?.GetValue<string>();
93+
receivedNotification.SetResult(msg);
94+
return Task.CompletedTask;
12595
});
12696

12797
// Send a test message through POST endpoint

0 commit comments

Comments
 (0)