Skip to content

Commit 18c1cf6

Browse files
committed
Fix: Add UTF-8 charset to Content-Type header for Chinese tool names
When calling tools with Chinese names using HttpClientSseClientTransport or HttpClientStreamableHttpTransport, the JSON body was sent without explicit charset encoding. This caused Chinese characters to be corrupted on the server side because the default encoding wasn't UTF-8. This fix adds '; charset=UTF-8' to the Content-Type header in both transport classes, ensuring proper encoding of non-ASCII characters. Fixes modelcontextprotocol#260
1 parent 5e77762 commit 18c1cf6

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientSseClientTransport.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ private Mono<HttpResponse<String>> sendHttpPost(final String endpoint, final Str
445445
return Mono.deferContextual(ctx -> {
446446
var builder = this.requestBuilder.copy()
447447
.uri(requestUri)
448-
.header(HttpHeaders.CONTENT_TYPE, "application/json")
448+
.header(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8")
449449
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
450450
.POST(HttpRequest.BodyPublishers.ofString(body));
451451
var transportContext = ctx.getOrDefault(McpTransportContext.KEY, McpTransportContext.EMPTY);

‎mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
477477

478478
var builder = requestBuilder.uri(uri)
479479
.header(HttpHeaders.ACCEPT, APPLICATION_JSON + ", " + TEXT_EVENT_STREAM)
480-
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
480+
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON + "; charset=UTF-8")
481481
.header(HttpHeaders.CACHE_CONTROL, "no-cache")
482482
.header(HttpHeaders.PROTOCOL_VERSION,
483483
ctx.getOrDefault(McpAsyncClient.NEGOTIATED_PROTOCOL_VERSION,

0 commit comments

Comments
 (0)