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,9 +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(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 -> 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 2024-2025 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 @@ -4,6 +4,10 @@

package io.modelcontextprotocol.server;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -308,7 +312,7 @@ void testStructuredOutputOfObjectArrayValidationSuccess(String clientType) {
"type", "object",
"properties", Map.of(
"name", Map.of("type", "string"),
"age", Map.of("type", "number")),
"age", Map.of("type", "number")),
"required", List.of("name", "age"))); // @formatter:on

Tool calculatorTool = Tool.builder()
Expand Down Expand Up @@ -639,6 +643,42 @@ void testThrownMcpErrorAndJsonRpcError() throws Exception {
mcpServer.close();
}

@ParameterizedTest
@ValueSource(strings = { "tools/list", "resources/list", "prompts/list" })
void testMissingHandlerReturnsMethodNotFoundError(String method) throws Exception {
var mcpServer = McpServer.sync(mcpStatelessServerTransport)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().build())
.build();

HttpResponse<String> response;

try {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PORT + CUSTOM_MESSAGE_ENDPOINT))
.header("Content-Type", "application/json")
.header("Accept", "application/json, text/event-stream")
.POST(HttpRequest.BodyPublishers.ofString("""
{
"jsonrpc": "2.0",
"method": "%s",
"id": "test-request-123",
"params": {}
}
""".formatted(method)))
.build();

response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
}
finally {
mcpServer.closeGracefully();
}

final var responseBody = response.body();
assertThatJson(responseBody).inPath("error.code").isEqualTo(McpSchema.ErrorCodes.METHOD_NOT_FOUND);
assertThatJson(responseBody).inPath("error.message").isEqualTo("Method not found: " + method);
}

private double evaluateExpression(String expression) {
// Simple expression evaluator for testing
return switch (expression) {
Expand Down