Skip to content

Commit ed4f8e7

Browse files
committed
Standard header name used in mcp
Signed-off-by: JermaineHua <crazyhzm@apache.org>
1 parent 2f94434 commit ed4f8e7

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.util.function.Function;
3636
import java.util.function.Supplier;
3737

38+
import static io.modelcontextprotocol.spec.McpSchema.Headers.MCP_SESSION_ID;
39+
3840
/**
3941
* An implementation of the Streamable HTTP protocol as defined by the
4042
* <code>2025-03-26</code> version of the MCP specification.
@@ -129,7 +131,7 @@ private DefaultMcpTransportSession createTransportSession() {
129131
DefaultMcpTransportSession transportSession = this.activeSession.get();
130132
return transportSession.sessionId().isEmpty() ? Mono.empty()
131133
: webClient.delete().uri(this.endpoint).headers(httpHeaders -> {
132-
httpHeaders.add("mcp-session-id", transportSession.sessionId().get());
134+
httpHeaders.add(MCP_SESSION_ID, transportSession.sessionId().get());
133135
}).retrieve().toBodilessEntity().doOnError(e -> logger.info("Got response {}", e)).then();
134136
};
135137
return new DefaultMcpTransportSession(onClose);
@@ -185,7 +187,7 @@ private Mono<Disposable> reconnect(McpTransportStream<Disposable> stream) {
185187
.uri(this.endpoint)
186188
.accept(MediaType.TEXT_EVENT_STREAM)
187189
.headers(httpHeaders -> {
188-
transportSession.sessionId().ifPresent(id -> httpHeaders.add("mcp-session-id", id));
190+
transportSession.sessionId().ifPresent(id -> httpHeaders.add(MCP_SESSION_ID, id));
189191
if (stream != null) {
190192
stream.lastId().ifPresent(id -> httpHeaders.add("last-event-id", id));
191193
}
@@ -243,12 +245,11 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
243245
.uri(this.endpoint)
244246
.accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_JSON)
245247
.headers(httpHeaders -> {
246-
transportSession.sessionId().ifPresent(id -> httpHeaders.add("mcp-session-id", id));
248+
transportSession.sessionId().ifPresent(id -> httpHeaders.add(MCP_SESSION_ID, id));
247249
})
248250
.bodyValue(message)
249251
.exchangeToFlux(response -> {
250-
if (transportSession
251-
.markInitialized(response.headers().asHttpHeaders().getFirst("mcp-session-id"))) {
252+
if (transportSession.markInitialized(response.headers().asHttpHeaders().getFirst(MCP_SESSION_ID))) {
252253
// Once we have a session, we try to open an async stream for
253254
// the server to send notifications and requests out-of-band.
254255
reconnect(null).contextWrite(sink.contextView()).subscribe();

mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ public static final class ErrorCodes {
134134

135135
}
136136

137+
// ---------------------------
138+
// JSON-RPC Protocol Headers
139+
// ---------------------------
140+
/**
141+
* Standard header name used in MCP JSON-RPC request and responses.
142+
*/
143+
public static final class Headers {
144+
145+
public static final String MCP_SESSION_ID = "Mcp-Session-Id";
146+
147+
}
148+
137149
public sealed interface Request permits InitializeRequest, CallToolRequest, CreateMessageRequest, ElicitRequest,
138150
CompleteRequest, GetPromptRequest {
139151

0 commit comments

Comments
 (0)