Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

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;
Expand Down Expand Up @@ -469,7 +468,7 @@ private Mono<HttpResponse<String>> sendHttpPost(final String endpoint, final Str
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private Mono<Disposable> reconnect(McpTransportStream<Disposable> stream) {
}

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();
Expand Down Expand Up @@ -371,7 +371,7 @@ private BodyHandler<Void> toSendMessageBodySubscriber(FluxSink<ResponseEvent> si

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
Expand Down Expand Up @@ -420,9 +420,9 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
}

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);
Expand Down Expand Up @@ -459,15 +459,19 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,10 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
// 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
Expand Down