Skip to content

Commit b678de6

Browse files
tzolovjkma
andcommitted
fix: improve error handling in HttpClientSseClientTransport and add test
- Include response body in error messages for better debugging - Change sendHttpPost return type from HttpResponse<Void> to HttpResponse<String> - Use BodyHandlers.ofString() instead of discarding response body - Add testErrorOnBogusMessage test to verify error handling for invalid JSON-RPC messages - Test validates that malformed messages return 400 status with descriptive error message Replace #361 Signed-off-by: Christian Tzolov <[email protected]> Co-authored-by: jkma <[email protected]>
1 parent d282d40 commit b678de6

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

mcp/src/main/java/io/modelcontextprotocol/client/transport/HttpClientSseClientTransport.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ public Mono<Void> sendMessage(JSONRPCMessage message) {
424424
.flatMap(body -> sendHttpPost(messageEndpointUri, body).handle((response, sink) -> {
425425
if (response.statusCode() != 200 && response.statusCode() != 201 && response.statusCode() != 202
426426
&& response.statusCode() != 206) {
427-
sink.error(new RuntimeException(
428-
"Sending message failed with a non-OK HTTP code: " + response.statusCode()));
427+
sink.error(new RuntimeException("Sending message failed with a non-OK HTTP code: "
428+
+ response.statusCode() + " - " + response.body()));
429429
}
430430
else {
431431
sink.next(response);
@@ -453,15 +453,15 @@ private Mono<String> serializeMessage(final JSONRPCMessage message) {
453453
});
454454
}
455455

456-
private Mono<HttpResponse<Void>> sendHttpPost(final String endpoint, final String body) {
456+
private Mono<HttpResponse<String>> sendHttpPost(final String endpoint, final String body) {
457457
final URI requestUri = Utils.resolveUri(baseUri, endpoint);
458458
final HttpRequest request = this.requestBuilder.copy()
459459
.uri(requestUri)
460460
.POST(HttpRequest.BodyPublishers.ofString(body))
461461
.build();
462462

463463
// TODO: why discard the body?
464-
return Mono.fromFuture(httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding()));
464+
return Mono.fromFuture(httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()));
465465
}
466466

467467
/**

mcp/src/test/java/io/modelcontextprotocol/client/transport/HttpClientSseClientTransportTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ void cleanup() {
105105
container.stop();
106106
}
107107

108+
@Test
109+
void testErrorOnBogusMessage() {
110+
// bogus message
111+
JSONRPCRequest bogusMessage = new JSONRPCRequest(null, null, "test-id", Map.of("key", "value"));
112+
113+
StepVerifier.create(transport.sendMessage(bogusMessage))
114+
.verifyErrorMessage(
115+
"Sending message failed with a non-OK HTTP code: 400 - Invalid message: {\"id\":\"test-id\",\"params\":{\"key\":\"value\"}}");
116+
}
117+
108118
@Test
109119
void testMessageProcessing() {
110120
// Create a test message

0 commit comments

Comments
 (0)