Fix error behavior on unregistered handlers in stateless server handler (#800)

Instead of throwing an McpError, it now returns with a JSON-RPC method not found error (-32601), aligned with the stateful implementation.

Closes #784
This commit is contained in:
Mathias Geat 2026-07-08 14:42:06 +02:00 • committed by GitHub
commit 5d3decebd6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 89 additions and 4 deletions

View file

@ -32,9 +32,9 @@ class DefaultMcpStatelessServerHandler implements McpStatelessServerHandler {
McpSchema.JSONRPCRequest request) {
McpStatelessRequestHandler<?> requestHandler = this.requestHandlers.get(request.method());
if (requestHandler == null) {
return Mono.error(McpError.builder(McpSchema.ErrorCodes.METHOD_NOT_FOUND)
.message("Missing handler for request type: " + request.method())
.build());
return Mono.just(new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, request.id(), null,
new McpSchema.JSONRPCResponse.JSONRPCError(McpSchema.ErrorCodes.METHOD_NOT_FOUND,
"Method not found: " + request.method(), null)));
}
return requestHandler.handle(transportContext, request.params())
.map(result -> McpSchema.JSONRPCResponse.result(request.id(), result))

View file

@ -0,0 +1,40 @@
/*
* Copyright 2026-2026 the original author or authors.
*/
package io.modelcontextprotocol.server;
import io.modelcontextprotocol.common.McpTransportContext;
import io.modelcontextprotocol.spec.McpSchema;
import org.junit.jupiter.api.Test;
import reactor.test.StepVerifier;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
class DefaultMcpStatelessServerHandlerTests {
@Test
void testHandleRequestWithUnregisteredMethod() {
// no request/initialization handlers
DefaultMcpStatelessServerHandler handler = new DefaultMcpStatelessServerHandler(Collections.emptyMap(),
Collections.emptyMap());
// unregistered method
McpSchema.JSONRPCRequest request = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, "resources/list",
"test-id-123", null);
StepVerifier.create(handler.handleRequest(McpTransportContext.EMPTY, request)).assertNext(response -> {
assertThat(response).isNotNull();
assertThat(response.jsonrpc()).isEqualTo(McpSchema.JSONRPC_VERSION);
assertThat(response.id()).isEqualTo("test-id-123");
assertThat(response.result()).isNull();
assertThat(response.error()).isNotNull();
assertThat(response.error().code()).isEqualTo(McpSchema.ErrorCodes.METHOD_NOT_FOUND);
assertThat(response.error().message()).isEqualTo("Method not found: resources/list");
}).verifyComplete();
}
}