|
4 | 4 |
|
5 | 5 | package io.modelcontextprotocol.modern.server; |
6 | 6 |
|
| 7 | +import java.time.Duration; |
7 | 8 | import java.util.HashMap; |
8 | 9 | import java.util.List; |
9 | 10 | import java.util.Map; |
|
12 | 13 |
|
13 | 14 | import io.modelcontextprotocol.common.McpTransportContext; |
14 | 15 | import io.modelcontextprotocol.json.McpJsonDefaults; |
| 16 | +import io.modelcontextprotocol.modern.JsonRpc.JSONRPCMessage; |
15 | 17 | import io.modelcontextprotocol.modern.JsonRpc.JSONRPCNotification; |
16 | 18 | import io.modelcontextprotocol.modern.JsonRpc.JSONRPCRequest; |
17 | 19 | import io.modelcontextprotocol.modern.JsonRpc.JSONRPCResponse; |
|
36 | 38 | import org.junit.jupiter.api.Test; |
37 | 39 | import org.junit.jupiter.params.ParameterizedTest; |
38 | 40 | import org.junit.jupiter.params.provider.MethodSource; |
| 41 | +import reactor.core.publisher.Flux; |
39 | 42 | import reactor.core.publisher.Mono; |
40 | 43 | import reactor.test.StepVerifier; |
41 | 44 |
|
@@ -289,6 +292,53 @@ void resourceSubscribeIsNotAdvertisedWithoutSubscriptions() { |
289 | 292 | assertThat(resourcesCapability(server)).containsEntry("subscribe", false); |
290 | 293 | } |
291 | 294 |
|
| 295 | + @Test |
| 296 | + void changeBroadcastAsSoonAsAckIsDeliveredReachesTheStream() { |
| 297 | + McpChangeFeed feed = new McpChangeFeed(); |
| 298 | + McpServer server = toolsServer(feed); |
| 299 | + |
| 300 | + // Broadcast a change synchronously from inside the ack's onNext, on the |
| 301 | + // thread delivering the ack. This is the earliest moment a test harness |
| 302 | + // reacting to the ack can broadcast a change, so the stream must already |
| 303 | + // be subscribed to the feed by the time the ack is emitted. |
| 304 | + Flux<JSONRPCMessage> messages = listen(server, 1).messages().doOnNext(msg -> { |
| 305 | + if (msg instanceof JSONRPCNotification notification |
| 306 | + && notification.method().equals(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED)) { |
| 307 | + feed.broadcast(new ServerChange.ToolsListChanged()); |
| 308 | + } |
| 309 | + }); |
| 310 | + |
| 311 | + // A lost change never arrives: time out instead of hanging. |
| 312 | + StepVerifier.create(messages) |
| 313 | + .expectNextMatches(msg -> ((JSONRPCNotification) msg).method() |
| 314 | + .equals(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED)) |
| 315 | + .expectNextMatches(msg -> ((JSONRPCNotification) msg).method() |
| 316 | + .equals(McpSchema.METHOD_NOTIFICATION_TOOLS_LIST_CHANGED)) |
| 317 | + .then(server::closeGracefully) |
| 318 | + .expectNextMatches(msg -> msg instanceof JSONRPCResponse) |
| 319 | + .expectComplete() |
| 320 | + .verify(Duration.ofSeconds(5)); |
| 321 | + } |
| 322 | + |
| 323 | + @Test |
| 324 | + void changeDeliveredBeforeAckIsSentFollowsTheAck() { |
| 325 | + // Delivers a change as soon as the listen stream subscribes, before the ack is |
| 326 | + // out. |
| 327 | + McpChangePublisher publisher = () -> Flux.<ServerChange>just(new ServerChange.ToolsListChanged()) |
| 328 | + .concatWith(Flux.never()); |
| 329 | + McpServer server = toolsServer(publisher); |
| 330 | + |
| 331 | + StepVerifier.create(listen(server, 1).messages()) |
| 332 | + .expectNextMatches(msg -> ((JSONRPCNotification) msg).method() |
| 333 | + .equals(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED)) |
| 334 | + .expectNextMatches(msg -> ((JSONRPCNotification) msg).method() |
| 335 | + .equals(McpSchema.METHOD_NOTIFICATION_TOOLS_LIST_CHANGED)) |
| 336 | + .then(server::closeGracefully) |
| 337 | + .expectNextMatches(msg -> msg instanceof JSONRPCResponse) |
| 338 | + .expectComplete() |
| 339 | + .verify(Duration.ofSeconds(5)); |
| 340 | + } |
| 341 | + |
292 | 342 | private static McpServer resourcesServer(boolean supportsSubscribe, McpChangePublisher publisher) { |
293 | 343 | McpAsyncResourceRepository repository = new McpAsyncResourceRepository() { |
294 | 344 | @Override |
|
0 commit comments