Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public Mono<McpSchema.JSONRPCResponse> handleRequest(McpTransportContext transpo
McpSchema.JSONRPCRequest request) {
McpStatelessRequestHandler<?> requestHandler = this.requestHandlers.get(request.method());
if (requestHandler == null) {
return Mono.error(new McpError("Missing handler for request type: " + request.method()));
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 -> new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, request.id(), result, null))
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,46 @@ void testThrownMcpErrorAndJsonRpcError() throws Exception {
mcpServer.close();
}

@Test
void testMissingHandlerReturnsMethodNotFoundError() throws Exception {
var mcpServer = McpServer.sync(mcpStatelessServerTransport)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().tools(true).build())
.build();

// "server/discover" has no registered handler, and neither do the capabilities
// this server does not advertise
McpSchema.JSONRPCRequest jsonrpcRequest = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION,
"server/discover", "test", null);

MockHttpServletRequest request = new MockHttpServletRequest("POST", CUSTOM_MESSAGE_ENDPOINT);
MockHttpServletResponse response = new MockHttpServletResponse();

byte[] content = JSON_MAPPER.writeValueAsBytes(jsonrpcRequest);
request.setContent(content);
request.addHeader("Content-Length", Integer.toString(content.length));
request.addHeader("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM);
request.addHeader("Content-Type", APPLICATION_JSON);
request.addHeader("Cache-Control", "no-cache");
request.addHeader(HttpHeaders.PROTOCOL_VERSION, ProtocolVersions.MCP_2025_03_26);

mcpStatelessServerTransport.service(request, response);

assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);

McpSchema.JSONRPCResponse jsonrpcResponse = JSON_MAPPER.readValue(response.getContentAsByteArray(),
McpSchema.JSONRPCResponse.class);

assertThat(jsonrpcResponse).isNotNull();
assertThat(jsonrpcResponse.id()).isEqualTo("test");
assertThat(jsonrpcResponse.result()).isNull();
assertThat(jsonrpcResponse.error()).isNotNull();
assertThat(jsonrpcResponse.error().code()).isEqualTo(ErrorCodes.METHOD_NOT_FOUND);
assertThat(jsonrpcResponse.error().message()).isEqualTo("Method not found: server/discover");

mcpServer.close();
}

// ---------------------------------------
// Bounded read
// ---------------------------------------
Expand Down