|
| 1 | +/* |
| 2 | + * Copyright 2023-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.mcp.client.webflux.transport; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.OutputStream; |
| 21 | +import java.net.InetSocketAddress; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 24 | +import java.util.concurrent.CountDownLatch; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | + |
| 28 | +import com.sun.net.httpserver.HttpServer; |
| 29 | +import io.modelcontextprotocol.spec.McpClientTransport; |
| 30 | +import io.modelcontextprotocol.spec.McpSchema; |
| 31 | +import io.modelcontextprotocol.spec.ProtocolVersions; |
| 32 | +import org.junit.jupiter.api.AfterEach; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.api.Nested; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.Timeout; |
| 37 | +import reactor.test.StepVerifier; |
| 38 | + |
| 39 | +import org.springframework.web.reactive.function.client.WebClient; |
| 40 | + |
| 41 | +import static org.assertj.core.api.Assertions.assertThat; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests for body-level error handling in {@link WebClientStreamableHttpTransport}. |
| 45 | + * |
| 46 | + * <p> |
| 47 | + * Verifies that body-level errors (malformed SSE streams, malformed JSON responses) |
| 48 | + * produce immediate synthetic error responses instead of hanging until read timeout. |
| 49 | + * |
| 50 | + * @author James Kennedy |
| 51 | + * @see <a href="https://github.com/modelcontextprotocol/java-sdk/issues/889">#889</a> |
| 52 | + */ |
| 53 | +@Timeout(15) |
| 54 | +public class WebClientStreamableHttpTransportBodyErrorIT { |
| 55 | + |
| 56 | + private HttpServer server; |
| 57 | + |
| 58 | + private McpClientTransport transport; |
| 59 | + |
| 60 | + private final AtomicBoolean returnMalformedSse = new AtomicBoolean(false); |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + void startServer() throws IOException { |
| 64 | + this.server = HttpServer.create(new InetSocketAddress(0), 0); |
| 65 | + |
| 66 | + this.server.createContext("/mcp", exchange -> { |
| 67 | + String method = exchange.getRequestMethod(); |
| 68 | + |
| 69 | + if ("DELETE".equals(method)) { |
| 70 | + exchange.sendResponseHeaders(200, 0); |
| 71 | + exchange.close(); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if ("GET".equals(method)) { |
| 76 | + exchange.sendResponseHeaders(405, 0); |
| 77 | + exchange.close(); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (this.returnMalformedSse.get()) { |
| 82 | + exchange.getResponseHeaders().set("Content-Type", "text/event-stream"); |
| 83 | + exchange.sendResponseHeaders(200, 0); |
| 84 | + OutputStream os = exchange.getResponseBody(); |
| 85 | + os.write("event: message\ndata: {not valid json\n\n".getBytes(StandardCharsets.UTF_8)); |
| 86 | + os.flush(); |
| 87 | + exchange.close(); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + exchange.getResponseHeaders().set("Content-Type", "application/json"); |
| 92 | + String response = "{\"jsonrpc\":\"2.0\",\"result\":{},\"id\":\"init-id\"}"; |
| 93 | + exchange.sendResponseHeaders(200, response.length()); |
| 94 | + exchange.getResponseBody().write(response.getBytes(StandardCharsets.UTF_8)); |
| 95 | + exchange.close(); |
| 96 | + }); |
| 97 | + |
| 98 | + this.server.setExecutor(null); |
| 99 | + this.server.start(); |
| 100 | + |
| 101 | + String host = "http://localhost:" + this.server.getAddress().getPort(); |
| 102 | + this.transport = WebClientStreamableHttpTransport.builder(WebClient.builder().baseUrl(host)).build(); |
| 103 | + } |
| 104 | + |
| 105 | + @AfterEach |
| 106 | + void stopServer() { |
| 107 | + if (this.server != null) { |
| 108 | + this.server.stop(0); |
| 109 | + } |
| 110 | + StepVerifier.create(this.transport.closeGracefully()).verifyComplete(); |
| 111 | + } |
| 112 | + |
| 113 | + @Nested |
| 114 | + class SseStream { |
| 115 | + |
| 116 | + @Test |
| 117 | + void emitsSyntheticErrorResponseOnMalformedSseData() throws InterruptedException { |
| 118 | + CopyOnWriteArrayList<McpSchema.JSONRPCMessage> handlerMessages = new CopyOnWriteArrayList<>(); |
| 119 | + CountDownLatch errorResponseLatch = new CountDownLatch(1); |
| 120 | + connectTransportWithErrorCapture(handlerMessages, errorResponseLatch); |
| 121 | + returnMalformedSse.set(true); |
| 122 | + |
| 123 | + StepVerifier.create(transport.sendMessage(createRequest("req-123"))).verifyComplete(); |
| 124 | + |
| 125 | + assertThat(errorResponseLatch.await(5, TimeUnit.SECONDS)) |
| 126 | + .as("Handler should receive synthetic error response within 5 seconds") |
| 127 | + .isTrue(); |
| 128 | + assertSingleSyntheticError(handlerMessages, "req-123"); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + @Nested |
| 134 | + class JsonResponse { |
| 135 | + |
| 136 | + @Test |
| 137 | + void emitsSyntheticErrorResponseOnMalformedJson() throws InterruptedException { |
| 138 | + CopyOnWriteArrayList<McpSchema.JSONRPCMessage> handlerMessages = new CopyOnWriteArrayList<>(); |
| 139 | + CountDownLatch errorResponseLatch = new CountDownLatch(1); |
| 140 | + connectTransportWithErrorCapture(handlerMessages, errorResponseLatch); |
| 141 | + replaceServerWithMalformedJsonResponse(); |
| 142 | + |
| 143 | + StepVerifier.create(transport.sendMessage(createRequest("req-456"))).verifyComplete(); |
| 144 | + |
| 145 | + assertThat(errorResponseLatch.await(5, TimeUnit.SECONDS)) |
| 146 | + .as("Handler should receive synthetic error response within 5 seconds") |
| 147 | + .isTrue(); |
| 148 | + assertSingleSyntheticError(handlerMessages, "req-456"); |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + @Nested |
| 154 | + class Notification { |
| 155 | + |
| 156 | + @Test |
| 157 | + void doesNotEmitSyntheticResponseOnBodyError() throws InterruptedException { |
| 158 | + CopyOnWriteArrayList<McpSchema.JSONRPCMessage> handlerMessages = new CopyOnWriteArrayList<>(); |
| 159 | + StepVerifier.create(transport.connect(msg -> msg.doOnNext(handlerMessages::add))).verifyComplete(); |
| 160 | + returnMalformedSse.set(true); |
| 161 | + McpSchema.JSONRPCNotification notification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION, |
| 162 | + "notifications/cancelled", null); |
| 163 | + |
| 164 | + StepVerifier.create(transport.sendMessage(notification)).verifyComplete(); |
| 165 | + |
| 166 | + Thread.sleep(500); |
| 167 | + assertThat(handlerMessages.stream() |
| 168 | + .filter(m -> m instanceof McpSchema.JSONRPCResponse resp && resp.error() != null) |
| 169 | + .toList()).isEmpty(); |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + private void connectTransportWithErrorCapture(CopyOnWriteArrayList<McpSchema.JSONRPCMessage> handlerMessages, |
| 175 | + CountDownLatch errorResponseLatch) { |
| 176 | + StepVerifier.create(this.transport.connect(msg -> msg.doOnNext(m -> { |
| 177 | + handlerMessages.add(m); |
| 178 | + if (m instanceof McpSchema.JSONRPCResponse resp && resp.error() != null) { |
| 179 | + errorResponseLatch.countDown(); |
| 180 | + } |
| 181 | + }))).verifyComplete(); |
| 182 | + } |
| 183 | + |
| 184 | + private McpSchema.JSONRPCRequest createRequest(String requestId) { |
| 185 | + return new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, McpSchema.METHOD_INITIALIZE, requestId, |
| 186 | + new McpSchema.InitializeRequest(ProtocolVersions.MCP_2025_03_26, |
| 187 | + McpSchema.ClientCapabilities.builder().roots(true).build(), |
| 188 | + new McpSchema.Implementation("Test Client", "1.0.0"))); |
| 189 | + } |
| 190 | + |
| 191 | + private void replaceServerWithMalformedJsonResponse() { |
| 192 | + this.server.removeContext("/mcp"); |
| 193 | + this.server.createContext("/mcp", exchange -> { |
| 194 | + String method = exchange.getRequestMethod(); |
| 195 | + |
| 196 | + if ("DELETE".equals(method)) { |
| 197 | + exchange.sendResponseHeaders(200, 0); |
| 198 | + exchange.close(); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + if ("GET".equals(method)) { |
| 203 | + exchange.sendResponseHeaders(405, 0); |
| 204 | + exchange.close(); |
| 205 | + return; |
| 206 | + } |
| 207 | + |
| 208 | + exchange.getResponseHeaders().set("Content-Type", "application/json"); |
| 209 | + byte[] malformed = "{not valid json".getBytes(StandardCharsets.UTF_8); |
| 210 | + exchange.sendResponseHeaders(200, malformed.length); |
| 211 | + exchange.getResponseBody().write(malformed); |
| 212 | + exchange.close(); |
| 213 | + }); |
| 214 | + } |
| 215 | + |
| 216 | + private void assertSingleSyntheticError(CopyOnWriteArrayList<McpSchema.JSONRPCMessage> handlerMessages, |
| 217 | + String expectedRequestId) { |
| 218 | + var errorResponses = handlerMessages.stream() |
| 219 | + .filter(m -> m instanceof McpSchema.JSONRPCResponse resp && resp.error() != null) |
| 220 | + .map(m -> (McpSchema.JSONRPCResponse) m) |
| 221 | + .toList(); |
| 222 | + |
| 223 | + assertThat(errorResponses).hasSize(1); |
| 224 | + McpSchema.JSONRPCResponse errorResponse = errorResponses.get(0); |
| 225 | + assertThat(errorResponse.id()).isEqualTo(expectedRequestId); |
| 226 | + assertThat(errorResponse.error().code()).isEqualTo(McpSchema.ErrorCodes.INTERNAL_ERROR); |
| 227 | + assertThat(errorResponse.error().message()).contains("Transport error"); |
| 228 | + } |
| 229 | + |
| 230 | +} |
0 commit comments