Skip to content

Commit 7889ad6

Browse files
committed
WebClientStreamableHttpTransport and WebMvcStatelessServerTransport should use McpJsonMapper when handling messages for serialization / deserialization
1 parent 9e73877 commit 7889ad6

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ else if (isNotFound(response)) {
281281

282282
@Override
283283
public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
284+
String jsonText;
285+
try {
286+
jsonText = jsonMapper.writeValueAsString(message);
287+
}
288+
catch (IOException e) {
289+
return Mono.error(new RuntimeException("Failed to serialize message", e));
290+
}
284291
return Mono.create(sink -> {
285292
logger.debug("Sending message {}", message);
286293
// Here we attempt to initialize the client.
@@ -293,14 +300,15 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
293300

294301
Disposable connection = Flux.deferContextual(ctx -> webClient.post()
295302
.uri(this.endpoint)
303+
.contentType(MediaType.APPLICATION_JSON)
296304
.accept(MediaType.APPLICATION_JSON, MediaType.TEXT_EVENT_STREAM)
297305
.header(HttpHeaders.PROTOCOL_VERSION,
298306
ctx.getOrDefault(McpAsyncClient.NEGOTIATED_PROTOCOL_VERSION,
299307
this.latestSupportedProtocolVersion))
300308
.headers(httpHeaders -> {
301309
transportSession.sessionId().ifPresent(id -> httpHeaders.add(HttpHeaders.MCP_SESSION_ID, id));
302310
})
303-
.bodyValue(message)
311+
.bodyValue(jsonText)
304312
.exchangeToFlux(response -> {
305313
if (transportSession
306314
.markInitialized(response.headers().asHttpHeaders().getFirst(HttpHeaders.MCP_SESSION_ID))) {

mcp-spring/mcp-spring-webmvc/src/main/java/io/modelcontextprotocol/server/transport/WebMvcStatelessServerTransport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ private ServerResponse handlePost(ServerRequest request) {
118118
.handleRequest(transportContext, jsonrpcRequest)
119119
.contextWrite(ctx -> ctx.put(McpTransportContext.KEY, transportContext))
120120
.block();
121-
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(jsonrpcResponse);
121+
String json = jsonMapper.writeValueAsString(jsonrpcResponse);
122+
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(json);
122123
}
123124
catch (Exception e) {
124125
logger.error("Failed to handle request: {}", e.getMessage());

0 commit comments

Comments
 (0)