Fix non-OK HTTP POST responses handling (#374)

Currently, JDK HttpClient SSE implementation incorrectly handles a
non-2xx HTTP response code for POST. Instead of immediately failing the
caller, it ignores the issue and the user awaits until the timeout kicks
in.

Set HttpClient to use HTTP/1.1 version explicitly for tests

A related problem happened with the WebClient Streamable HTTP
implementation, where the error was swallowed for non-400 responses.

Signed-off-by: Dariusz Jędrzejczyk <dariusz.jedrzejczyk@broadcom.com>
This commit is contained in:
Dariusz Jędrzejczyk 2025-07-04 16:48:25 +02:00 • committed by Christian Tzolov
commit 95df67ec05
2 changed files with 42 additions and 6 deletions

View file

@ -421,13 +421,17 @@ public class HttpClientSseClientTransport implements McpClientTransport {
}
return this.serializeMessage(message)
.flatMap(body -> sendHttpPost(messageEndpointUri, body))
.doOnNext(response -> {
.flatMap(body -> sendHttpPost(messageEndpointUri, body).handle((response, sink) -> {
if (response.statusCode() != 200 && response.statusCode() != 201 && response.statusCode() != 202
&& response.statusCode() != 206) {
logger.error("Error sending message: {}", response.statusCode());
sink.error(new RuntimeException(
"Sending message failed with a non-OK HTTP code: " + response.statusCode()));
}
})
else {
sink.next(response);
sink.complete();
}
}))
.doOnError(error -> {
if (!isClosing) {
logger.error("Error sending message: {}", error.getMessage());

View file

@ -59,7 +59,9 @@ class HttpClientSseClientTransportTests {
private Sinks.Many<ServerSentEvent<String>> events = Sinks.many().unicast().onBackpressureBuffer();
public TestHttpClientSseClientTransport(final String baseUri) {
super(HttpClient.newHttpClient(), HttpRequest.newBuilder(), baseUri, "/sse", new ObjectMapper());
super(HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build(),
HttpRequest.newBuilder().header("Content-Type", "application/json"), baseUri, "/sse",
new ObjectMapper());
}
public int getInboundMessageCount() {