Skip to content

Commit 75934a3

Browse files
committed
refactor: add notification handler to stateless servers.
1 parent 30f1adf commit 75934a3

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

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

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

134134
this.protocolVersions = new ArrayList<>(mcpTransport.protocolVersions());
135135

136-
McpStatelessServerHandler handler = new DefaultMcpStatelessServerHandler(requestHandlers, Map.of());
136+
Map<String, McpStatelessNotificationHandler> notificationHandlers = prepareNotificationHandlers();
137+
McpStatelessServerHandler handler = new DefaultMcpStatelessServerHandler(requestHandlers, notificationHandlers);
137138
mcpTransport.setMcpHandler(handler);
138139
}
139140

141+
private Map<String, McpStatelessNotificationHandler> prepareNotificationHandlers() {
142+
Map<String, McpStatelessNotificationHandler> notificationHandlers = new HashMap<>();
143+
144+
notificationHandlers.put(McpSchema.METHOD_NOTIFICATION_INITIALIZED, (exchange, params) -> Mono.empty());
145+
notificationHandlers.put(McpSchema.METHOD_NOTIFICATION_ROOTS_LIST_CHANGED, (exchange, params) -> Mono.empty());
146+
147+
return notificationHandlers;
148+
}
149+
140150
// ---------------------------------------
141151
// Lifecycle Management
142152
// ---------------------------------------

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,64 @@ void testThrownMcpErrorAndJsonRpcError() throws Exception {
765765
mcpServer.close();
766766
}
767767

768+
@Test
769+
void testInitializedNotificationCallReturnsAccepted() throws Exception {
770+
var mcpServer = McpServer.sync(mcpStatelessServerTransport)
771+
.serverInfo("test-server", "1.0.0")
772+
.capabilities(ServerCapabilities.builder().build())
773+
.build();
774+
775+
McpSchema.JSONRPCNotification notification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION,
776+
McpSchema.METHOD_NOTIFICATION_INITIALIZED, null);
777+
778+
MockHttpServletRequest request = new MockHttpServletRequest("POST", CUSTOM_MESSAGE_ENDPOINT);
779+
MockHttpServletResponse response = new MockHttpServletResponse();
780+
781+
byte[] content = JSON_MAPPER.writeValueAsBytes(notification);
782+
request.setContent(content);
783+
request.addHeader("Content-Type", "application/json");
784+
request.addHeader("Content-Length", Integer.toString(content.length));
785+
request.addHeader("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM);
786+
request.addHeader("Cache-Control", "no-cache");
787+
request.addHeader(HttpHeaders.PROTOCOL_VERSION, ProtocolVersions.MCP_2025_03_26);
788+
789+
mcpStatelessServerTransport.service(request, response);
790+
791+
assertThat(response.getStatus()).isEqualTo(202);
792+
assertThat(response.getContentAsByteArray()).isEmpty();
793+
794+
mcpServer.close();
795+
}
796+
797+
@Test
798+
void testRootsListChangedNotificationCallReturnsAccepted() throws Exception {
799+
var mcpServer = McpServer.sync(mcpStatelessServerTransport)
800+
.serverInfo("test-server", "1.0.0")
801+
.capabilities(ServerCapabilities.builder().build())
802+
.build();
803+
804+
McpSchema.JSONRPCNotification notification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION,
805+
McpSchema.METHOD_NOTIFICATION_ROOTS_LIST_CHANGED, null);
806+
807+
MockHttpServletRequest request = new MockHttpServletRequest("POST", CUSTOM_MESSAGE_ENDPOINT);
808+
MockHttpServletResponse response = new MockHttpServletResponse();
809+
810+
byte[] content = JSON_MAPPER.writeValueAsBytes(notification);
811+
request.setContent(content);
812+
request.addHeader("Content-Type", "application/json");
813+
request.addHeader("Content-Length", Integer.toString(content.length));
814+
request.addHeader("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM);
815+
request.addHeader("Cache-Control", "no-cache");
816+
request.addHeader(HttpHeaders.PROTOCOL_VERSION, ProtocolVersions.MCP_2025_03_26);
817+
818+
mcpStatelessServerTransport.service(request, response);
819+
820+
assertThat(response.getStatus()).isEqualTo(202);
821+
assertThat(response.getContentAsByteArray()).isEmpty();
822+
823+
mcpServer.close();
824+
}
825+
768826
private double evaluateExpression(String expression) {
769827
// Simple expression evaluator for testing
770828
return switch (expression) {

0 commit comments

Comments
 (0)