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

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();
}
}