Repository navigation
Expand file tree
/
Copy pathSubscriptionsFeatureTests.java
More file actions
277 lines (238 loc) · 11.7 KB
/
Copy pathSubscriptionsFeatureTests.java
File metadata and controls
277 lines (238 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright 2026-2026 the original author or authors.
*/
package io.modelcontextprotocol.modern.server;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.modelcontextprotocol.common.McpTransportContext;
import io.modelcontextprotocol.json.McpJsonDefaults;
import io.modelcontextprotocol.modern.JsonRpc.JSONRPCNotification;
import io.modelcontextprotocol.modern.JsonRpc.JSONRPCRequest;
import io.modelcontextprotocol.modern.JsonRpc.JSONRPCResponse;
import io.modelcontextprotocol.modern.McpSchema;
import io.modelcontextprotocol.modern.McpSchema.CacheScope;
import io.modelcontextprotocol.modern.McpSchema.MetaKeys;
import io.modelcontextprotocol.modern.McpSchema.ReadResourceOutcome;
import io.modelcontextprotocol.modern.McpSchema.ReadResourceRequest;
import io.modelcontextprotocol.modern.McpSchema.SubscriptionFilter;
import io.modelcontextprotocol.modern.McpSchema.SubscriptionsAcknowledgedParams;
import io.modelcontextprotocol.modern.server.feature.McpAsyncResourceRepository;
import io.modelcontextprotocol.modern.server.feature.McpChangeFeed;
import io.modelcontextprotocol.modern.server.feature.McpChangePublisher;
import io.modelcontextprotocol.modern.server.feature.ResourcesFeature;
import io.modelcontextprotocol.modern.server.feature.ResourcesPage;
import io.modelcontextprotocol.modern.server.feature.ServerChange;
import io.modelcontextprotocol.modern.server.feature.ToolsFeature;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import static io.modelcontextprotocol.modern.server.ModernTestFixtures.PERMISSIVE_VALIDATOR;
import static io.modelcontextprotocol.modern.server.ModernTestFixtures.SERVER_INFO;
import static io.modelcontextprotocol.modern.server.ModernTestFixtures.emptyTools;
import static io.modelcontextprotocol.modern.server.ModernTestFixtures.meta;
import static io.modelcontextprotocol.modern.server.ModernTestFixtures.respond;
import static org.assertj.core.api.Assertions.assertThat;
class SubscriptionsFeatureTests {
@Test
void ackIsFirstAndReflectsHonouredSubset() {
McpChangeFeed feed = new McpChangeFeed();
McpServer server = McpServer.builder()
.serverInfo(SERVER_INFO)
.feature(ToolsFeature.ofAsync(emptyTools(), McpJsonDefaults.getMapper(), PERMISSIVE_VALIDATOR, 0L,
CacheScope.PRIVATE))
.subscriptions(feed)
.build();
Map<String, Object> params = new HashMap<>();
params.put("_meta", meta());
params.put("notifications", Map.of("toolsListChanged", true, "promptsListChanged", true));
JSONRPCRequest request = new JSONRPCRequest("subscriptions/listen", 7, params);
var invocation = (McpTransportResponse.Streaming) server.handle(McpTransportContext.EMPTY, request).block();
// The feed has no buffered replay, so broadcast only after the listen stream has
// actually subscribed - otherwise the change is dropped before anyone is
// listening, same as a real client connecting after a change already fired.
StepVerifier.create(invocation.messages()).assertNext(msg -> {
JSONRPCNotification ack = (JSONRPCNotification) msg;
assertThat(ack.method()).isEqualTo(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED);
SubscriptionFilter notifications = ((SubscriptionsAcknowledgedParams) ack.params()).notifications();
assertThat(notifications.toolsListChanged()).isTrue();
assertThat(notifications.promptsListChanged()).isNull();
})
.then(() -> feed.broadcast(new ServerChange.ToolsListChanged()))
.assertNext(msg -> assertThat(((JSONRPCNotification) msg).method())
.isEqualTo(McpSchema.METHOD_NOTIFICATION_TOOLS_LIST_CHANGED))
.then(() -> feed.broadcast(new ServerChange.PromptsListChanged()))
.then(server::closeGracefully)
.assertNext(msg -> assertThat(msg).isInstanceOf(JSONRPCResponse.class))
.verifyComplete();
}
@Test
void unrequestedTypeIsNeverEmitted() {
McpChangeFeed feed = new McpChangeFeed();
McpServer server = McpServer.builder()
.serverInfo(SERVER_INFO)
.feature(ToolsFeature.ofAsync(emptyTools(), McpJsonDefaults.getMapper(), PERMISSIVE_VALIDATOR, 0L,
CacheScope.PRIVATE))
.subscriptions(feed)
.build();
Map<String, Object> params = new HashMap<>();
params.put("_meta", meta());
params.put("notifications", Map.of("toolsListChanged", true));
JSONRPCRequest request = new JSONRPCRequest("subscriptions/listen", 1, params);
var invocation = (McpTransportResponse.Streaming) server.handle(McpTransportContext.EMPTY, request).block();
StepVerifier.create(invocation.messages())
.expectNextMatches(msg -> ((JSONRPCNotification) msg).method()
.equals(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED))
.then(() -> feed.broadcast(new ServerChange.PromptsListChanged())) // not
// requested
.then(() -> feed.broadcast(new ServerChange.ToolsListChanged()))
.expectNextMatches(msg -> ((JSONRPCNotification) msg).method()
.equals(McpSchema.METHOD_NOTIFICATION_TOOLS_LIST_CHANGED))
.then(server::closeGracefully)
.expectNextMatches(msg -> msg instanceof JSONRPCResponse)
.verifyComplete();
}
@Test
void subscriptionIdMatchesRequestId() {
McpChangeFeed feed = new McpChangeFeed();
McpServer server = McpServer.builder()
.serverInfo(SERVER_INFO)
.feature(ToolsFeature.ofAsync(emptyTools(), McpJsonDefaults.getMapper(), PERMISSIVE_VALIDATOR, 0L,
CacheScope.PRIVATE))
.subscriptions(feed)
.build();
Map<String, Object> params = new HashMap<>();
params.put("_meta", meta());
params.put("notifications", Map.of("toolsListChanged", true));
JSONRPCRequest request = new JSONRPCRequest("subscriptions/listen", 42, params);
var invocation = (McpTransportResponse.Streaming) server.handle(McpTransportContext.EMPTY, request).block();
server.closeGracefully();
StepVerifier.create(invocation.messages()).assertNext(msg -> {
JSONRPCNotification ack = (JSONRPCNotification) msg;
assertThat(subscriptionIdOf(ack)).isEqualTo(42);
}).assertNext(msg -> assertThat(msg).isInstanceOf(JSONRPCResponse.class)).verifyComplete();
}
@Test
void changeEmittedWithNoListenerIsDropped() {
McpChangeFeed feed = new McpChangeFeed();
McpServer server = toolsServer(feed);
feed.broadcast(new ServerChange.ToolsListChanged());
StepVerifier.create(listen(server, 1).messages())
.expectNextMatches(msg -> ((JSONRPCNotification) msg).method()
.equals(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED))
.then(server::closeGracefully)
.expectNextMatches(msg -> msg instanceof JSONRPCResponse)
.verifyComplete();
}
@Test
void listenAfterEarlierListenEndedReceivesChanges() {
McpChangeFeed feed = new McpChangeFeed();
McpServer server = toolsServer(feed);
// Receiving a change proves the first stream subscribed to the feed before it
// goes away.
StepVerifier.create(listen(server, 1).messages())
.expectNextMatches(msg -> ((JSONRPCNotification) msg).method()
.equals(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED))
.then(() -> feed.broadcast(new ServerChange.ToolsListChanged()))
.expectNextMatches(msg -> ((JSONRPCNotification) msg).method()
.equals(McpSchema.METHOD_NOTIFICATION_TOOLS_LIST_CHANGED))
.thenCancel()
.verify();
StepVerifier.create(listen(server, 2).messages())
.expectNextMatches(msg -> ((JSONRPCNotification) msg).method()
.equals(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED))
.then(() -> feed.broadcast(new ServerChange.ToolsListChanged()))
.expectNextMatches(msg -> ((JSONRPCNotification) msg).method()
.equals(McpSchema.METHOD_NOTIFICATION_TOOLS_LIST_CHANGED))
.then(server::closeGracefully)
.expectNextMatches(msg -> msg instanceof JSONRPCResponse)
.verifyComplete();
}
@Test
void resourceSubscribeIsAdvertisedAndHonouredWhenRepositorySupportsIt() {
McpServer server = resourcesServer(true, new McpChangeFeed());
assertThat(resourcesCapability(server)).containsEntry("subscribe", true).containsEntry("listChanged", true);
StepVerifier.create(listenToResource(server).messages())
.assertNext(msg -> assertThat(acknowledgedNotifications((JSONRPCNotification) msg).resourceSubscriptions())
.containsExactly("test://a"))
.then(server::closeGracefully)
.expectNextMatches(msg -> msg instanceof JSONRPCResponse)
.verifyComplete();
}
@Test
void resourceSubscribeIsNeitherAdvertisedNorHonouredByDefault() {
McpServer server = resourcesServer(false, new McpChangeFeed());
assertThat(resourcesCapability(server)).containsEntry("subscribe", false).containsEntry("listChanged", true);
StepVerifier.create(listenToResource(server).messages())
.assertNext(msg -> assertThat(acknowledgedNotifications((JSONRPCNotification) msg).resourceSubscriptions())
.isNull())
.then(server::closeGracefully)
.expectNextMatches(msg -> msg instanceof JSONRPCResponse)
.verifyComplete();
}
@Test
void resourceSubscribeIsNotAdvertisedWithoutSubscriptions() {
McpServer server = resourcesServer(true, null);
assertThat(resourcesCapability(server)).containsEntry("subscribe", false);
}
private static McpServer resourcesServer(boolean supportsSubscribe, McpChangePublisher publisher) {
McpAsyncResourceRepository repository = new McpAsyncResourceRepository() {
@Override
public Mono<ResourcesPage> list(McpRequestContext ctx, String cursor) {
return Mono.just(ResourcesPage.of(List.of()));
}
@Override
public Mono<McpAsyncResponse<ReadResourceOutcome>> read(McpRequestContext ctx,
ReadResourceRequest request) {
return Mono.empty();
}
@Override
public boolean supportsSubscribe() {
return supportsSubscribe;
}
};
McpServer.Builder builder = McpServer.builder()
.serverInfo(SERVER_INFO)
.feature(ResourcesFeature.ofAsync(repository, McpJsonDefaults.getMapper(), 0L, CacheScope.PRIVATE));
if (publisher != null) {
builder.subscriptions(publisher);
}
return builder.build();
}
@SuppressWarnings("unchecked")
private static Map<String, Object> resourcesCapability(McpServer server) {
JSONRPCRequest request = new JSONRPCRequest(McpSchema.METHOD_SERVER_DISCOVER, 1, Map.of("_meta", meta()));
Map<String, Object> result = (Map<String, Object>) respond(server, request).block().result();
Map<String, Object> capabilities = (Map<String, Object>) result.get("capabilities");
return (Map<String, Object>) capabilities.get("resources");
}
private static McpTransportResponse.Streaming listenToResource(McpServer server) {
Map<String, Object> params = new HashMap<>();
params.put("_meta", meta());
params.put("notifications", Map.of("resourceSubscriptions", List.of("test://a")));
JSONRPCRequest request = new JSONRPCRequest("subscriptions/listen", 1, params);
return (McpTransportResponse.Streaming) server.handle(McpTransportContext.EMPTY, request).block();
}
private static SubscriptionFilter acknowledgedNotifications(JSONRPCNotification ack) {
assertThat(ack.method()).isEqualTo(McpSchema.METHOD_NOTIFICATION_SUBSCRIPTIONS_ACKNOWLEDGED);
return ((SubscriptionsAcknowledgedParams) ack.params()).notifications();
}
private static McpServer toolsServer(McpChangePublisher publisher) {
return McpServer.builder()
.serverInfo(SERVER_INFO)
.feature(ToolsFeature.ofAsync(emptyTools(), McpJsonDefaults.getMapper(), PERMISSIVE_VALIDATOR, 0L,
CacheScope.PRIVATE))
.subscriptions(publisher)
.build();
}
private static McpTransportResponse.Streaming listen(McpServer server, int id) {
Map<String, Object> params = new HashMap<>();
params.put("_meta", meta());
params.put("notifications", Map.of("toolsListChanged", true));
JSONRPCRequest request = new JSONRPCRequest("subscriptions/listen", id, params);
return (McpTransportResponse.Streaming) server.handle(McpTransportContext.EMPTY, request).block();
}
private static Object subscriptionIdOf(JSONRPCNotification notification) {
return ((SubscriptionsAcknowledgedParams) notification.params()).meta().get(MetaKeys.SUBSCRIPTION_ID);
}
}