Skip to content

Commit ea6447d

Browse files
committed
WebClientStreamableHttpTransport and WebMvcStatelessServerTransport should use McpJsonMapper when handling messages for serialization / deserialization
1 parent f22e195 commit ea6447d

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ 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+
} catch (IOException e) {
288+
return Mono.error(new RuntimeException("Failed to serialize message", e));
289+
}
284290
return Mono.create(sink -> {
285291
logger.debug("Sending message {}", message);
286292
// Here we attempt to initialize the client.
@@ -293,14 +299,15 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
293299

294300
Disposable connection = Flux.deferContextual(ctx -> webClient.post()
295301
.uri(this.endpoint)
302+
.contentType(MediaType.APPLICATION_JSON)
296303
.accept(MediaType.APPLICATION_JSON, MediaType.TEXT_EVENT_STREAM)
297304
.header(HttpHeaders.PROTOCOL_VERSION,
298305
ctx.getOrDefault(McpAsyncClient.NEGOTIATED_PROTOCOL_VERSION,
299306
this.latestSupportedProtocolVersion))
300307
.headers(httpHeaders -> {
301308
transportSession.sessionId().ifPresent(id -> httpHeaders.add(HttpHeaders.MCP_SESSION_ID, id));
302309
})
303-
.bodyValue(message)
310+
.bodyValue(jsonText)
304311
.exchangeToFlux(response -> {
305312
if (transportSession
306313
.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)