|
9 | 9 | import java.util.Map; |
10 | 10 | import java.util.concurrent.atomic.AtomicReference; |
11 | 11 | import java.util.function.BiFunction; |
| 12 | +import java.util.function.Function; |
12 | 13 |
|
13 | 14 | import io.modelcontextprotocol.client.McpClient; |
14 | 15 | import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport; |
|
45 | 46 | import org.springframework.mock.web.MockHttpServletRequest; |
46 | 47 | import org.springframework.mock.web.MockHttpServletResponse; |
47 | 48 | import org.springframework.web.client.RestClient; |
| 49 | +import reactor.core.publisher.Mono; |
| 50 | +import reactor.test.StepVerifier; |
48 | 51 |
|
49 | 52 | import static io.modelcontextprotocol.server.transport.HttpServletStatelessServerTransport.APPLICATION_JSON; |
50 | 53 | import static io.modelcontextprotocol.server.transport.HttpServletStatelessServerTransport.TEXT_EVENT_STREAM; |
@@ -448,7 +451,7 @@ void testStructuredOutputOfObjectArrayValidationSuccess() { |
448 | 451 | "type", "object", |
449 | 452 | "properties", Map.of( |
450 | 453 | "name", Map.of("type", "string"), |
451 | | - "age", Map.of("type", "number")), |
| 454 | + "age", Map.of("type", "number")), |
452 | 455 | "required", List.of("name", "age"))); // @formatter:on |
453 | 456 |
|
454 | 457 | Tool calculatorTool = Tool.builder("getMembers") |
@@ -765,6 +768,48 @@ void testThrownMcpErrorAndJsonRpcError() throws Exception { |
765 | 768 | mcpServer.close(); |
766 | 769 | } |
767 | 770 |
|
| 771 | + @Test |
| 772 | + void testMissingHandlerReturnsMethodNotFoundError() { |
| 773 | + var mcpServer = McpServer.sync(mcpStatelessServerTransport) |
| 774 | + .serverInfo("test-server", "1.0.0") |
| 775 | + .capabilities(ServerCapabilities.builder().build()) |
| 776 | + .build(); |
| 777 | + var clientTransport = HttpClientStreamableHttpTransport.builder("http://localhost:" + PORT) |
| 778 | + .endpoint(CUSTOM_MESSAGE_ENDPOINT) |
| 779 | + .build(); |
| 780 | + |
| 781 | + try (var mcpClient = McpClient.sync(clientTransport).build()) { |
| 782 | + // Create a session using an MCP client |
| 783 | + McpSchema.InitializeResult initResult = mcpClient.initialize(); |
| 784 | + assertThat(initResult).isNotNull(); |
| 785 | + |
| 786 | + // Override the response handler in the client to capture responses |
| 787 | + AtomicReference<McpSchema.JSONRPCResponse> response = new AtomicReference<>(); |
| 788 | + var handler = (Function<Mono<McpSchema.JSONRPCMessage>, Mono<McpSchema.JSONRPCMessage>>) ( |
| 789 | + message) -> message.doOnNext(r -> { |
| 790 | + if (r instanceof McpSchema.JSONRPCResponse resp) { |
| 791 | + response.set(resp); |
| 792 | + } |
| 793 | + }); |
| 794 | + StepVerifier.create(clientTransport.connect(handler)).verifyComplete(); |
| 795 | + |
| 796 | + // Send a request for a non-existent method through the transport, bypassing |
| 797 | + // the client's capability checks |
| 798 | + StepVerifier |
| 799 | + .create(clientTransport.sendMessage(new McpSchema.JSONRPCRequest("foo/bar", "test-request-123"))) |
| 800 | + .verifyComplete(); |
| 801 | + |
| 802 | + // Wait until we've received the response |
| 803 | + await().atMost(Duration.ofSeconds(1)).until(() -> response.get() != null); |
| 804 | + |
| 805 | + assertThat(response.get().error().code()).isEqualTo(McpSchema.ErrorCodes.METHOD_NOT_FOUND); |
| 806 | + assertThat(response.get().error().message()).isEqualTo("Method not found: foo/bar"); |
| 807 | + } |
| 808 | + finally { |
| 809 | + mcpServer.closeGracefully(); |
| 810 | + } |
| 811 | + } |
| 812 | + |
768 | 813 | private double evaluateExpression(String expression) { |
769 | 814 | // Simple expression evaluator for testing |
770 | 815 | return switch (expression) { |
|
0 commit comments