Skip to content

Commit df4cb10

Browse files
Fix UTF-8 encoding for non-ASCII tool names in HTTP client transports
Both HttpClientSseClientTransport and HttpClientStreamableHttpTransport set Content-Type to 'application/json' without specifying the charset. While Java's BodyPublishers.ofString() uses UTF-8 by default, the missing charset in the header can cause the server to interpret the request body using a different encoding (e.g., ISO-8859-1), corrupting non-ASCII characters such as Chinese tool names. Explicitly set Content-Type to 'application/json; charset=utf-8' in POST requests on both client transports. Fixes #260 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 26304a7 commit df4cb10

2 files changed

Lines changed: 4 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
@@ -456,7 +456,7 @@ private Mono<HttpResponse<String>> sendHttpPost(final String endpoint, final Str
456456
return Mono.deferContextual(ctx -> {
457457
var builder = this.requestBuilder.copy()
458458
.uri(requestUri)
459-
.header(HttpHeaders.CONTENT_TYPE, "application/json")
459+
.header(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8")
460460
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
461461
.POST(HttpRequest.BodyPublishers.ofString(body));
462462
var transportContext = ctx.getOrDefault(McpTransportContext.KEY, McpTransportContext.EMPTY);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
9999

100100
private static final String APPLICATION_JSON = "application/json";
101101

102+
private static final String APPLICATION_JSON_UTF8 = "application/json; charset=utf-8";
103+
102104
private static final String TEXT_EVENT_STREAM = "text/event-stream";
103105

104106
public static int NOT_FOUND = 404;
@@ -454,7 +456,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
454456

455457
var builder = requestBuilder.uri(uri)
456458
.header(HttpHeaders.ACCEPT, APPLICATION_JSON + ", " + TEXT_EVENT_STREAM)
457-
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
459+
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_UTF8)
458460
.header(HttpHeaders.CACHE_CONTROL, "no-cache")
459461
.header(HttpHeaders.PROTOCOL_VERSION,
460462
ctx.getOrDefault(McpAsyncClient.NEGOTIATED_PROTOCOL_VERSION,

0 commit comments

Comments
 (0)