|
60 | 60 | import reactor.core.publisher.Mono; |
61 | 61 | import reactor.test.StepVerifier; |
62 | 62 |
|
63 | | -import org.springframework.mock.web.MockHttpServletRequest; |
64 | | -import org.springframework.mock.web.MockHttpServletResponse; |
65 | | -import org.springframework.web.client.RestClient; |
66 | 63 | import static io.modelcontextprotocol.server.transport.HttpServletStatelessServerTransport.APPLICATION_JSON; |
67 | 64 | import static io.modelcontextprotocol.server.transport.HttpServletStatelessServerTransport.TEXT_EVENT_STREAM; |
68 | 65 | import static io.modelcontextprotocol.util.McpJsonMapperUtils.JSON_MAPPER; |
@@ -140,12 +137,18 @@ void testToolCallSuccess() { |
140 | 137 | Tool.builder("tool1", EMPTY_JSON_SCHEMA).title("tool1 description").build(), |
141 | 138 | (transportContext, request) -> { |
142 | 139 | // perform a blocking call to a remote service |
143 | | - String response = RestClient.create() |
144 | | - .get() |
145 | | - .uri("https://raw.githubusercontent.com/modelcontextprotocol/java-sdk/refs/heads/main/README.md") |
146 | | - .retrieve() |
147 | | - .body(String.class); |
148 | | - assertThat(response).isNotBlank(); |
| 140 | + try { |
| 141 | + HttpResponse<String> response = HttpClient.newHttpClient() |
| 142 | + .send(HttpRequest.newBuilder() |
| 143 | + .uri(URI.create( |
| 144 | + "https://raw.githubusercontent.com/modelcontextprotocol/java-sdk/refs/heads/main/README.md")) |
| 145 | + .GET() |
| 146 | + .build(), HttpResponse.BodyHandlers.ofString()); |
| 147 | + assertThat(response.body()).isNotBlank(); |
| 148 | + } |
| 149 | + catch (Exception e) { |
| 150 | + throw new RuntimeException(e); |
| 151 | + } |
149 | 152 | return callResponse; |
150 | 153 | }); |
151 | 154 |
|
@@ -714,34 +717,19 @@ void testThrownMcpErrorAndJsonRpcError() throws Exception { |
714 | 717 |
|
715 | 718 | mcpServer.addTool(toolSpec); |
716 | 719 |
|
717 | | - McpSchema.CallToolRequest callToolRequest = McpSchema.CallToolRequest.builder("test") |
718 | | - .arguments(Map.of()) |
| 720 | + var request = HttpRequest.newBuilder() |
| 721 | + .uri(URI.create("http://localhost:" + PORT + CUSTOM_MESSAGE_ENDPOINT)) |
| 722 | + .header("Content-Type", APPLICATION_JSON) |
| 723 | + .header("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM) |
| 724 | + .header(HttpHeaders.PROTOCOL_VERSION, ProtocolVersions.MCP_2025_03_26) |
| 725 | + .POST(HttpRequest.BodyPublishers.ofString(""" |
| 726 | + {"jsonrpc":"2.0","id":"test","method":"tools/call","params":{"name":"test","arguments":{}}}""")) |
719 | 727 | .build(); |
720 | | - McpSchema.JSONRPCRequest jsonrpcRequest = new McpSchema.JSONRPCRequest(McpSchema.METHOD_TOOLS_CALL, "test", |
721 | | - callToolRequest); |
722 | | - |
723 | | - MockHttpServletRequest request = new MockHttpServletRequest("POST", CUSTOM_MESSAGE_ENDPOINT); |
724 | | - MockHttpServletResponse response = new MockHttpServletResponse(); |
725 | | - |
726 | | - byte[] content = JSON_MAPPER.writeValueAsBytes(jsonrpcRequest); |
727 | | - request.setContent(content); |
728 | | - request.addHeader("Content-Type", "application/json"); |
729 | | - request.addHeader("Content-Length", Integer.toString(content.length)); |
730 | | - request.addHeader("Content-Length", Integer.toString(content.length)); |
731 | | - request.addHeader("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM); |
732 | | - request.addHeader("Content-Type", APPLICATION_JSON); |
733 | | - request.addHeader("Cache-Control", "no-cache"); |
734 | | - request.addHeader(HttpHeaders.PROTOCOL_VERSION, ProtocolVersions.MCP_2025_03_26); |
735 | 728 |
|
736 | | - mcpStatelessServerTransport.service(request, response); |
737 | | - |
738 | | - McpSchema.JSONRPCResponse jsonrpcResponse = JSON_MAPPER.readValue(response.getContentAsByteArray(), |
739 | | - McpSchema.JSONRPCResponse.class); |
| 729 | + var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); |
740 | 730 |
|
741 | | - assertThat(jsonrpcResponse).isNotNull(); |
742 | | - assertThat(jsonrpcResponse.error()).isNotNull(); |
743 | | - assertThat(jsonrpcResponse.error().code()).isEqualTo(ErrorCodes.INTERNAL_ERROR); |
744 | | - assertThat(jsonrpcResponse.error().message()).isEqualTo("testing"); |
| 731 | + assertThatJson(response.body()).inPath("error.code").isEqualTo(ErrorCodes.INTERNAL_ERROR); |
| 732 | + assertThatJson(response.body()).inPath("error.message").isEqualTo("testing"); |
745 | 733 | } |
746 | 734 |
|
747 | 735 | @Test |
|
0 commit comments