Skip to content

Commit 97576aa

Browse files
rameshreddy-adutlaCopilot
authored andcommitted
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 5e77762 commit 97576aa

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
102102

103103
private static final String APPLICATION_JSON = "application/json";
104104

105+
private static final String APPLICATION_JSON_UTF8 = "application/json; charset=utf-8";
106+
105107
private static final String TEXT_EVENT_STREAM = "text/event-stream";
106108

107109
public static int NOT_FOUND = 404;
@@ -477,7 +479,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
477479

478480
var builder = requestBuilder.uri(uri)
479481
.header(HttpHeaders.ACCEPT, APPLICATION_JSON + ", " + TEXT_EVENT_STREAM)
480-
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
482+
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_UTF8)
481483
.header(HttpHeaders.CACHE_CONTROL, "no-cache")
482484
.header(HttpHeaders.PROTOCOL_VERSION,
483485
ctx.getOrDefault(McpAsyncClient.NEGOTIATED_PROTOCOL_VERSION,

0 commit comments

Comments
 (0)