-
Notifications
You must be signed in to change notification settings - Fork 904
Fix error behavior on unregistered handlers in stateless server handler #800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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() | ||
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't do a parameterized test, instead make a request with method |
||
| 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()); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use a client transport instead of raw HTTP requests. |
||
| 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) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.