Skip to content

Commit fbe5256

Browse files
Reject subscriptions/listen without notifications
notifications is required in SubscriptionsListenRequestParams. Check the raw params before decoding, as CompletionsFeature does, and fail with -32602 instead of opening an empty subscription. An explicit empty filter is still accepted. Fixes #1196
1 parent 615a88b commit fbe5256

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,18 @@ public Set<String> methods() {
7171

7272
@Override
7373
public Mono<McpAsyncResponse<Result>> handle(McpRequestContext ctx, Object params) {
74+
// The JSON creator fills a missing filter in, so check the raw params.
75+
if (!(params instanceof Map<?, ?> map) || map.get("notifications") == null) {
76+
return Mono.error(McpException.invalidParams("params.notifications is required"));
77+
}
7478
SubscriptionsListenRequest request;
7579
try {
76-
request = params == null ? null : this.jsonMapper.convertValue(params, SubscriptionsListenRequest.class);
80+
request = this.jsonMapper.convertValue(params, SubscriptionsListenRequest.class);
7781
}
7882
catch (RuntimeException ex) {
7983
return Mono.error(McpException.invalidParams("Malformed SubscriptionsListenRequest"));
8084
}
81-
SubscriptionFilter requested = request == null || request.notifications() == null ? SubscriptionFilter.EMPTY
82-
: request.notifications();
85+
SubscriptionFilter requested = request.notifications();
8386
return Mono.just(McpAsyncResponse.streaming(notifier -> listen(ctx, intersect(requested), notifier)));
8487
}
8588

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,41 @@ void resourceSubscribeIsNotAdvertisedWithoutSubscriptions() {
211211
assertThat(resourcesCapability(server)).containsEntry("subscribe", false);
212212
}
213213

214+
@Test
215+
void listenWithoutNotificationsIsRejectedAsInvalidParams() {
216+
McpServer server = toolsServer(new McpChangeFeed());
217+
JSONRPCRequest request = new JSONRPCRequest("subscriptions/listen", 1, Map.of("_meta", meta()));
218+
219+
assertThat(server.handle(McpTransportContext.EMPTY, request).block()).isInstanceOfSatisfying(
220+
McpTransportResponse.Error.class,
221+
error -> assertThat(error.response().error().code()).isEqualTo(McpSchema.ErrorCodes.INVALID_PARAMS));
222+
}
223+
224+
@Test
225+
void listenWithNullNotificationsIsRejectedAsInvalidParams() {
226+
McpServer server = toolsServer(new McpChangeFeed());
227+
Map<String, Object> params = new HashMap<>();
228+
params.put("_meta", meta());
229+
params.put("notifications", null);
230+
JSONRPCRequest request = new JSONRPCRequest("subscriptions/listen", 1, params);
231+
232+
assertThat(server.handle(McpTransportContext.EMPTY, request).block()).isInstanceOfSatisfying(
233+
McpTransportResponse.Error.class,
234+
error -> assertThat(error.response().error().code()).isEqualTo(McpSchema.ErrorCodes.INVALID_PARAMS));
235+
}
236+
237+
@Test
238+
void listenWithEmptyNotificationsIsAccepted() {
239+
McpServer server = toolsServer(new McpChangeFeed());
240+
Map<String, Object> params = new HashMap<>();
241+
params.put("_meta", meta());
242+
params.put("notifications", Map.of());
243+
JSONRPCRequest request = new JSONRPCRequest("subscriptions/listen", 1, params);
244+
245+
assertThat(server.handle(McpTransportContext.EMPTY, request).block())
246+
.isInstanceOf(McpTransportResponse.Streaming.class);
247+
}
248+
214249
private static McpServer resourcesServer(boolean supportsSubscribe, McpChangePublisher publisher) {
215250
McpAsyncResourceRepository repository = new McpAsyncResourceRepository() {
216251
@Override

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.BufferedReader;
88
import java.io.IOException;
9+
import java.io.InputStream;
910
import java.io.InputStreamReader;
1011
import java.io.OutputStream;
1112
import java.net.Socket;
@@ -260,6 +261,19 @@ void unknownToolIsInvalidParams() throws Exception {
260261
assertThat(errorCode(response)).isEqualTo(ErrorCodes.INVALID_PARAMS);
261262
}
262263

264+
@Test
265+
void listenWithoutNotificationsIsInvalidParams() throws Exception {
266+
HttpRequest request = post("subscriptions/listen", Map.of("_meta", meta())).build();
267+
HttpResponse<InputStream> response = HttpClient.newHttpClient()
268+
.send(request, HttpResponse.BodyHandlers.ofInputStream());
269+
270+
try (InputStream body = response.body()) {
271+
assertThat(response.statusCode()).isEqualTo(400);
272+
assertThat(new String(body.readAllBytes(), StandardCharsets.UTF_8))
273+
.contains("\"code\":" + ErrorCodes.INVALID_PARAMS);
274+
}
275+
}
276+
263277
@Test
264278
void streamingBodyExceptionIsAnsweredInStream() throws Exception {
265279
HttpResponse<String> response = callTool("fails-streaming");

0 commit comments

Comments
 (0)