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. - As part of this PR, we also move hard-coded HTTP header names to the HttpHeaders interface. While they are not defined by the MCP spec, they are used by it and are core to implementing the protocol. Therefore, they have their place in a core interface. - Fixes #582 Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
This commit is contained in:
parent
12292ab598
commit
080098e48c
4 changed files with 48 additions and 17 deletions
|
|
@ -18,14 +18,13 @@ import java.util.function.Function;
|
|||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.modelcontextprotocol.json.McpJsonMapper;
|
||||
import io.modelcontextprotocol.json.TypeRef;
|
||||
|
||||
import io.modelcontextprotocol.client.transport.ResponseSubscribers.ResponseEvent;
|
||||
import io.modelcontextprotocol.client.transport.customizer.McpAsyncHttpClientRequestCustomizer;
|
||||
import io.modelcontextprotocol.client.transport.customizer.McpSyncHttpClientRequestCustomizer;
|
||||
import io.modelcontextprotocol.client.transport.ResponseSubscribers.ResponseEvent;
|
||||
import io.modelcontextprotocol.common.McpTransportContext;
|
||||
import io.modelcontextprotocol.json.McpJsonMapper;
|
||||
import io.modelcontextprotocol.json.TypeRef;
|
||||
import io.modelcontextprotocol.spec.HttpHeaders;
|
||||
import io.modelcontextprotocol.spec.McpClientTransport;
|
||||
import io.modelcontextprotocol.spec.McpSchema;
|
||||
import io.modelcontextprotocol.spec.McpSchema.JSONRPCMessage;
|
||||
|
|
@ -469,7 +468,7 @@ public class HttpClientSseClientTransport implements McpClientTransport {
|
|||
return Mono.deferContextual(ctx -> {
|
||||
var builder = this.requestBuilder.copy()
|
||||
.uri(requestUri)
|
||||
.header("Content-Type", "application/json")
|
||||
.header(HttpHeaders.CONTENT_TYPE, "application/json")
|
||||
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body));
|
||||
var transportContext = ctx.getOrDefault(McpTransportContext.KEY, McpTransportContext.EMPTY);
|
||||
|
|
|
|||
|
|
@ -246,7 +246,7 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
|||
}
|
||||
|
||||
var builder = requestBuilder.uri(uri)
|
||||
.header("Accept", TEXT_EVENT_STREAM)
|
||||
.header(HttpHeaders.ACCEPT, TEXT_EVENT_STREAM)
|
||||
.header("Cache-Control", "no-cache")
|
||||
.header(HttpHeaders.PROTOCOL_VERSION, MCP_PROTOCOL_VERSION)
|
||||
.GET();
|
||||
|
|
@ -371,7 +371,7 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
|||
|
||||
BodyHandler<Void> responseBodyHandler = responseInfo -> {
|
||||
|
||||
String contentType = responseInfo.headers().firstValue("Content-Type").orElse("").toLowerCase();
|
||||
String contentType = responseInfo.headers().firstValue(HttpHeaders.CONTENT_TYPE).orElse("").toLowerCase();
|
||||
|
||||
if (contentType.contains(TEXT_EVENT_STREAM)) {
|
||||
// For SSE streams, use line subscriber that returns Void
|
||||
|
|
@ -420,9 +420,9 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
|||
}
|
||||
|
||||
var builder = requestBuilder.uri(uri)
|
||||
.header("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM)
|
||||
.header("Content-Type", APPLICATION_JSON)
|
||||
.header("Cache-Control", "no-cache")
|
||||
.header(HttpHeaders.ACCEPT, APPLICATION_JSON + ", " + TEXT_EVENT_STREAM)
|
||||
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
|
||||
.header(HttpHeaders.CACHE_CONTROL, "no-cache")
|
||||
.header(HttpHeaders.PROTOCOL_VERSION, MCP_PROTOCOL_VERSION)
|
||||
.POST(HttpRequest.BodyPublishers.ofString(jsonBody));
|
||||
var transportContext = ctx.getOrDefault(McpTransportContext.KEY, McpTransportContext.EMPTY);
|
||||
|
|
@ -459,15 +459,19 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
|||
|
||||
String contentType = responseEvent.responseInfo()
|
||||
.headers()
|
||||
.firstValue("Content-Type")
|
||||
.firstValue(HttpHeaders.CONTENT_TYPE)
|
||||
.orElse("")
|
||||
.toLowerCase();
|
||||
|
||||
if (contentType.isBlank()) {
|
||||
logger.debug("No content type returned for POST in session {}", sessionRepresentation);
|
||||
String contentLength = responseEvent.responseInfo()
|
||||
.headers()
|
||||
.firstValue(HttpHeaders.CONTENT_LENGTH)
|
||||
.orElse(null);
|
||||
|
||||
if (contentType.isBlank() || "0".equals(contentLength)) {
|
||||
logger.debug("No body returned for POST in session {}", sessionRepresentation);
|
||||
// No content type means no response body, so we can just
|
||||
// return
|
||||
// an empty stream
|
||||
// return an empty stream
|
||||
deliveredSink.success();
|
||||
return Flux.empty();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,4 +26,31 @@ public interface HttpHeaders {
|
|||
*/
|
||||
String PROTOCOL_VERSION = "MCP-Protocol-Version";
|
||||
|
||||
/**
|
||||
* The HTTP Content-Length header.
|
||||
* @see <a href=
|
||||
* "https://httpwg.org/specs/rfc9110.html#field.content-length">RFC9110</a>
|
||||
*/
|
||||
String CONTENT_LENGTH = "Content-Length";
|
||||
|
||||
/**
|
||||
* The HTTP Content-Type header.
|
||||
* @see <a href=
|
||||
* "https://httpwg.org/specs/rfc9110.html#field.content-type">RFC9110</a>
|
||||
*/
|
||||
String CONTENT_TYPE = "Content-Type";
|
||||
|
||||
/**
|
||||
* The HTTP Accept header.
|
||||
* @see <a href= "https://httpwg.org/specs/rfc9110.html#field.accept">RFC9110</a>
|
||||
*/
|
||||
String ACCEPT = "Accept";
|
||||
|
||||
/**
|
||||
* The HTTP Cache-Control header.
|
||||
* @see <a href=
|
||||
* "https://httpwg.org/specs/rfc9111.html#field.cache-control">RFC9111</a>
|
||||
*/
|
||||
String CACHE_CONTROL = "Cache-Control";
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -293,9 +293,10 @@ public class WebClientStreamableHttpTransport implements McpClientTransport {
|
|||
// 200 OK for notifications
|
||||
if (response.statusCode().is2xxSuccessful()) {
|
||||
Optional<MediaType> contentType = response.headers().contentType();
|
||||
long contentLength = response.headers().contentLength().orElse(-1);
|
||||
// Existing SDKs consume notifications with no response body nor
|
||||
// content type
|
||||
if (contentType.isEmpty()) {
|
||||
if (contentType.isEmpty() || contentLength == 0) {
|
||||
logger.trace("Message was successfully sent via POST for session {}",
|
||||
sessionRepresentation);
|
||||
// signal the caller that the message was successfully
|
||||
|
|
|
|||
Loading…
Reference in a new issue