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 Backport of #850 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Daniel Garnier-Moiroux <git@garnier.wf> Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
This commit is contained in:
parent
8e1c5d0cd4
commit
9ead4d154e
2 changed files with 62 additions and 2 deletions
|
|
@ -463,7 +463,7 @@ public class HttpClientSseClientTransport implements McpClientTransport {
|
|||
return Mono.deferContextual(ctx -> {
|
||||
var builder = this.requestBuilder.copy()
|
||||
.uri(requestUri)
|
||||
.header(HttpHeaders.CONTENT_TYPE, "application/json")
|
||||
.header(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8")
|
||||
.header(MCP_PROTOCOL_VERSION_HEADER_NAME, MCP_PROTOCOL_VERSION)
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body));
|
||||
var transportContext = ctx.getOrDefault(McpTransportContext.KEY, McpTransportContext.EMPTY);
|
||||
|
|
|
|||
|
|
@ -102,6 +102,8 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
|||
|
||||
private static final String APPLICATION_JSON = "application/json";
|
||||
|
||||
private static final String APPLICATION_JSON_UTF8 = "application/json; charset=utf-8";
|
||||
|
||||
private static final String TEXT_EVENT_STREAM = "text/event-stream";
|
||||
|
||||
public static int NOT_FOUND = 404;
|
||||
|
|
@ -477,7 +479,7 @@ public class HttpClientStreamableHttpTransport implements McpClientTransport {
|
|||
|
||||
var builder = requestBuilder.uri(uri)
|
||||
.header(HttpHeaders.ACCEPT, APPLICATION_JSON + ", " + TEXT_EVENT_STREAM)
|
||||
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
|
||||
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_UTF8)
|
||||
.header(HttpHeaders.CACHE_CONTROL, "no-cache")
|
||||
.header(HttpHeaders.PROTOCOL_VERSION,
|
||||
ctx.getOrDefault(McpAsyncClient.NEGOTIATED_PROTOCOL_VERSION,
|
||||
|
|
|
|||
Loading…
Reference in a new issue