Skip to content

Commit 906c33c

Browse files
committed
refactor: add notification handler to stateless servers.
1 parent 29dc250 commit 906c33c

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

mcp-core/src/main/java/io/modelcontextprotocol/server/McpStatelessAsyncServer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,20 @@ public class McpStatelessAsyncServer {
128128

129129
this.protocolVersions = new ArrayList<>(mcpTransport.protocolVersions());
130130

131-
McpStatelessServerHandler handler = new DefaultMcpStatelessServerHandler(requestHandlers, Map.of());
131+
Map<String, McpStatelessNotificationHandler> notificationHandlers = prepareNotificationHandlers();
132+
McpStatelessServerHandler handler = new DefaultMcpStatelessServerHandler(requestHandlers, notificationHandlers);
132133
mcpTransport.setMcpHandler(handler);
133134
}
134135

136+
private Map<String, McpStatelessNotificationHandler> prepareNotificationHandlers() {
137+
Map<String, McpStatelessNotificationHandler> notificationHandlers = new HashMap<>();
138+
139+
notificationHandlers.put(McpSchema.METHOD_NOTIFICATION_INITIALIZED, (exchange, params) -> Mono.empty());
140+
notificationHandlers.put(McpSchema.METHOD_NOTIFICATION_ROOTS_LIST_CHANGED, (exchange, params) -> Mono.empty());
141+
142+
return notificationHandlers;
143+
}
144+
135145
// ---------------------------------------
136146
// Lifecycle Management
137147
// ---------------------------------------

mcp-test/src/test/java/io/modelcontextprotocol/server/HttpServletStatelessIntegrationTests.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,64 @@ void testThrownMcpErrorAndJsonRpcError() throws Exception {
639639
mcpServer.close();
640640
}
641641

642+
@Test
643+
void testInitializedNotificationCallReturnsAccepted() throws Exception {
644+
var mcpServer = McpServer.sync(mcpStatelessServerTransport)
645+
.serverInfo("test-server", "1.0.0")
646+
.capabilities(ServerCapabilities.builder().build())
647+
.build();
648+
649+
McpSchema.JSONRPCNotification notification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION,
650+
McpSchema.METHOD_NOTIFICATION_INITIALIZED, null);
651+
652+
MockHttpServletRequest request = new MockHttpServletRequest("POST", CUSTOM_MESSAGE_ENDPOINT);
653+
MockHttpServletResponse response = new MockHttpServletResponse();
654+
655+
byte[] content = JSON_MAPPER.writeValueAsBytes(notification);
656+
request.setContent(content);
657+
request.addHeader("Content-Type", "application/json");
658+
request.addHeader("Content-Length", Integer.toString(content.length));
659+
request.addHeader("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM);
660+
request.addHeader("Cache-Control", "no-cache");
661+
request.addHeader(HttpHeaders.PROTOCOL_VERSION, ProtocolVersions.MCP_2025_03_26);
662+
663+
mcpStatelessServerTransport.service(request, response);
664+
665+
assertThat(response.getStatus()).isEqualTo(202);
666+
assertThat(response.getContentAsByteArray()).isEmpty();
667+
668+
mcpServer.close();
669+
}
670+
671+
@Test
672+
void testRootsListChangedNotificationCallReturnsAccepted() throws Exception {
673+
var mcpServer = McpServer.sync(mcpStatelessServerTransport)
674+
.serverInfo("test-server", "1.0.0")
675+
.capabilities(ServerCapabilities.builder().build())
676+
.build();
677+
678+
McpSchema.JSONRPCNotification notification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION,
679+
McpSchema.METHOD_NOTIFICATION_ROOTS_LIST_CHANGED, null);
680+
681+
MockHttpServletRequest request = new MockHttpServletRequest("POST", CUSTOM_MESSAGE_ENDPOINT);
682+
MockHttpServletResponse response = new MockHttpServletResponse();
683+
684+
byte[] content = JSON_MAPPER.writeValueAsBytes(notification);
685+
request.setContent(content);
686+
request.addHeader("Content-Type", "application/json");
687+
request.addHeader("Content-Length", Integer.toString(content.length));
688+
request.addHeader("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM);
689+
request.addHeader("Cache-Control", "no-cache");
690+
request.addHeader(HttpHeaders.PROTOCOL_VERSION, ProtocolVersions.MCP_2025_03_26);
691+
692+
mcpStatelessServerTransport.service(request, response);
693+
694+
assertThat(response.getStatus()).isEqualTo(202);
695+
assertThat(response.getContentAsByteArray()).isEmpty();
696+
697+
mcpServer.close();
698+
}
699+
642700
private double evaluateExpression(String expression) {
643701
// Simple expression evaluator for testing
644702
return switch (expression) {

0 commit comments

Comments
 (0)