Skip to content

Commit b9dd1af

Browse files
authored
Merge branch 'main' into modern-client
2 parents b58d21c + 33e1581 commit b9dd1af

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/modern/server/SubscriptionsFeature.java‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,29 @@ public void capabilities(ServerCapabilities.Builder builder) {
131131
}
132132

133133
private Mono<Result> listen(McpRequestContext ctx, SubscriptionFilter honoured, McpAsyncNotifier notifier) {
134+
// To avoid race conditions in test harnesses, need to ensure that:
135+
// 1. Changes are subscribed to BEFORE the ack
136+
// 2. But the ACK goes before any notification
137+
//
138+
// Achieve this by:
139+
// 1. Merging changes + ack
140+
// 2. Forcing ack to be sent first in the changes
141+
// 3. Caching the "send ack" so that it only ever goes out once
134142
Map<String, Object> subscriptionMeta = Map.of(MetaKeys.SUBSCRIPTION_ID, ctx.requestId());
135-
Mono<Void> ack = notifier.notify(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED,
136-
new SubscriptionsAcknowledgedParams(honoured, subscriptionMeta));
143+
Mono<Void> ack = notifier
144+
.notify(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED,
145+
new SubscriptionsAcknowledgedParams(honoured, subscriptionMeta))
146+
.cache();
137147

138148
Flux<Void> forwardChanges = this.publisher.changes()
139149
.filter(change -> change.isHonouredBy(honoured))
140150
.takeUntilOther(this.shutdown.asMono())
141151
.concatMap(change -> {
142152
Map<String, Object> params = params(change, subscriptionMeta);
143-
return params == null ? Mono.empty() : notifier.notify(change.method(), params);
153+
return params == null ? Mono.empty() : ack.then(notifier.notify(change.method(), params));
144154
});
145155

146-
return ack.thenMany(forwardChanges)
156+
return Flux.merge(forwardChanges, ack)
147157
.then(Mono.fromSupplier(() -> (Result) SubscriptionsListenResult.forSubscription(ctx.requestId())));
148158
}
149159

‎mcp-test/src/test/java/io/modelcontextprotocol/modern/server/SubscriptionsFeatureTests.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package io.modelcontextprotocol.modern.server;
66

7+
import java.time.Duration;
78
import java.util.HashMap;
89
import java.util.List;
910
import java.util.Map;
@@ -12,6 +13,7 @@
1213

1314
import io.modelcontextprotocol.common.McpTransportContext;
1415
import io.modelcontextprotocol.json.McpJsonDefaults;
16+
import io.modelcontextprotocol.modern.JsonRpc.JSONRPCMessage;
1517
import io.modelcontextprotocol.modern.JsonRpc.JSONRPCNotification;
1618
import io.modelcontextprotocol.modern.JsonRpc.JSONRPCRequest;
1719
import io.modelcontextprotocol.modern.JsonRpc.JSONRPCResponse;
@@ -36,6 +38,7 @@
3638
import org.junit.jupiter.api.Test;
3739
import org.junit.jupiter.params.ParameterizedTest;
3840
import org.junit.jupiter.params.provider.MethodSource;
41+
import reactor.core.publisher.Flux;
3942
import reactor.core.publisher.Mono;
4043
import reactor.test.StepVerifier;
4144

@@ -289,6 +292,53 @@ void resourceSubscribeIsNotAdvertisedWithoutSubscriptions() {
289292
assertThat(resourcesCapability(server)).containsEntry("subscribe", false);
290293
}
291294

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+
292342
private static McpServer resourcesServer(boolean supportsSubscribe, McpChangePublisher publisher) {
293343
McpAsyncResourceRepository repository = new McpAsyncResourceRepository() {
294344
@Override

0 commit comments

Comments
 (0)