Skip to content

feat: implement MCP protocol version header support #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -65,6 +65,10 @@ public class WebClientStreamableHttpTransport implements McpClientTransport {

private static final Logger logger = LoggerFactory.getLogger(WebClientStreamableHttpTransport.class);

private static final String MCP_PROTOCOL_VERSION = "2025-06-18";

private static final String MCP_PROTOCOL_VERSION_HEADER_NAME = "MCP-Protocol-Version";

private static final String DEFAULT_ENDPOINT = "/mcp";

/**
Expand Down Expand Up @@ -127,12 +131,20 @@ public Mono<Void> connect(Function<Mono<McpSchema.JSONRPCMessage>, Mono<McpSchem

private DefaultMcpTransportSession createTransportSession() {
Function<String, Publisher<Void>> onClose = sessionId -> sessionId == null ? Mono.empty()
: webClient.delete().uri(this.endpoint).headers(httpHeaders -> {
httpHeaders.add("mcp-session-id", sessionId);
}).retrieve().toBodilessEntity().onErrorComplete(e -> {
logger.warn("Got error when closing transport", e);
return true;
}).then();
: webClient.delete()
.uri(this.endpoint)
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.headers(httpHeaders -> {
httpHeaders.add("mcp-session-id", sessionId);
httpHeaders.add(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION);
})
.retrieve()
.toBodilessEntity()
.onErrorComplete(e -> {
logger.warn("Got error when closing transport", e);
return true;
})
.then();
return new DefaultMcpTransportSession(onClose);
}

Expand Down Expand Up @@ -185,6 +197,7 @@ private Mono<Disposable> reconnect(McpTransportStream<Disposable> stream) {
Disposable connection = webClient.get()
.uri(this.endpoint)
.accept(MediaType.TEXT_EVENT_STREAM)
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.headers(httpHeaders -> {
transportSession.sessionId().ifPresent(id -> httpHeaders.add("mcp-session-id", id));
if (stream != null) {
Expand Down Expand Up @@ -245,6 +258,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
Disposable connection = webClient.post()
.uri(this.endpoint)
.accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON)
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.headers(httpHeaders -> {
transportSession.sessionId().ifPresent(id -> httpHeaders.add("mcp-session-id", id));
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public class WebFluxSseClientTransport implements McpClientTransport {

private static final Logger logger = LoggerFactory.getLogger(WebFluxSseClientTransport.class);

private static final String MCP_PROTOCOL_VERSION = "2024-11-05";

private static final String MCP_PROTOCOL_VERSION_HEADER_NAME = "MCP-Protocol-Version";

/**
* Event type for JSON-RPC messages received through the SSE connection. The server
* sends messages with this event type to transmit JSON-RPC protocol data.
Expand Down Expand Up @@ -249,6 +253,7 @@ public Mono<Void> sendMessage(JSONRPCMessage message) {
return webClient.post()
.uri(messageEndpointUri)
.contentType(MediaType.APPLICATION_JSON)
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.bodyValue(jsonText)
.retrieve()
.toBodilessEntity()
Expand Down Expand Up @@ -281,6 +286,7 @@ protected Flux<ServerSentEvent<String>> eventStream() {// @formatter:off
.get()
.uri(this.sseEndpoint)
.accept(MediaType.TEXT_EVENT_STREAM)
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.retrieve()
.bodyToFlux(SSE_TYPE)
.retryWhen(Retry.from(retrySignal -> retrySignal.handle(inboundRetryHandler)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
*/
public class HttpClientSseClientTransport implements McpClientTransport {

private static final String MCP_PROTOCOL_VERSION = "2024-11-05";

private static final String MCP_PROTOCOL_VERSION_HEADER_NAME = "MCP-Protocol-Version";

private static final Logger logger = LoggerFactory.getLogger(HttpClientSseClientTransport.class);

/** SSE event type for JSON-RPC messages */
Expand Down Expand Up @@ -330,6 +334,7 @@ public Mono<Void> connect(Function<Mono<JSONRPCMessage>, Mono<JSONRPCMessage>> h
.uri(Utils.resolveUri(this.baseUri, this.sseEndpoint))
.header("Accept", "text/event-stream")
.header("Cache-Control", "no-cache")
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.GET()
.build();

Expand Down Expand Up @@ -457,6 +462,7 @@ private Mono<HttpResponse<String>> sendHttpPost(final String endpoint, final Str
final URI requestUri = Utils.resolveUri(baseUri, endpoint);
final HttpRequest request = this.requestBuilder.copy()
.uri(requestUri)
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {

private static final Logger logger = LoggerFactory.getLogger(HttpClientStreamableHttpTransport.class);

private static final String MCP_PROTOCOL_VERSION = "2025-06-18";

private static final String MCP_PROTOCOL_VERSION_HEADER_NAME = "MCP-Protocol-Version";

private static final String DEFAULT_ENDPOINT = "/mcp";

/**
Expand Down Expand Up @@ -158,6 +162,7 @@ private Publisher<Void> createDelete(String sessionId) {
.uri(Utils.resolveUri(this.baseUri, this.endpoint))
.header("Cache-Control", "no-cache")
.header("mcp-session-id", sessionId)
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.DELETE()
.build();

Expand Down Expand Up @@ -222,6 +227,7 @@ private Mono<Disposable> reconnect(McpTransportStream<Disposable> stream) {
HttpRequest request = requestBuilder.uri(Utils.resolveUri(this.baseUri, this.endpoint))
.header("Accept", TEXT_EVENT_STREAM)
.header("Cache-Control", "no-cache")
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.GET()
.build();

Expand Down Expand Up @@ -361,6 +367,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sendMessage) {
.header("Accept", TEXT_EVENT_STREAM + ", " + APPLICATION_JSON)
.header("Content-Type", APPLICATION_JSON)
.header("Cache-Control", "no-cache")
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();

Expand Down
Loading