Skip to content

Commit c4bf139

Browse files
committed
Client: handle server responses with Content-Length: 0
- When the client sends `notification/initalized`, servers must respond with HTTP 202 and an empty body. We checked for the absence of a Content-Type header to verify whether the body was empty. - However, some servers will send an empty body with a Content-Type header, and that header may have an unsupported, default type such as `text/html` or `text/plain`. - Now we we also use the Content-Length header to check for an empty body. This header is optional in HTTP/2, so we do not make it our primary mechanism for detecting empty bodies. - Fixes #582 Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent 12292ab commit c4bf139

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,15 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
463463
.orElse("")
464464
.toLowerCase();
465465

466-
if (contentType.isBlank()) {
466+
String contentLength = responseEvent.responseInfo()
467+
.headers()
468+
.firstValue("Content-Length")
469+
.orElse(null);
470+
471+
if (contentType.isBlank() || "0".equals(contentLength)) {
467472
logger.debug("No content type returned for POST in session {}", sessionRepresentation);
468473
// No content type means no response body, so we can just
469-
// return
470-
// an empty stream
474+
// return an empty stream
471475
deliveredSink.success();
472476
return Flux.empty();
473477
}

mcp-spring/mcp-spring-webflux/src/main/java/io/modelcontextprotocol/client/transport/WebClientStreamableHttpTransport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,10 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
293293
// 200 OK for notifications
294294
if (response.statusCode().is2xxSuccessful()) {
295295
Optional<MediaType> contentType = response.headers().contentType();
296+
long contentLength = response.headers().contentLength().orElse(-1);
296297
// Existing SDKs consume notifications with no response body nor
297298
// content type
298-
if (contentType.isEmpty()) {
299+
if (contentType.isEmpty() || contentLength == 0) {
299300
logger.trace("Message was successfully sent via POST for session {}",
300301
sessionRepresentation);
301302
// signal the caller that the message was successfully

0 commit comments

Comments
 (0)