Skip to content

Fix UTF-8 encoding for non-ASCII tool names in HTTP client transports#850

Open
rameshreddy-adutla wants to merge 1 commit intomodelcontextprotocol:mainfrom
rameshreddy-adutla:fix/260-utf8-encoding-chinese-names
Open

Fix UTF-8 encoding for non-ASCII tool names in HTTP client transports#850
rameshreddy-adutla wants to merge 1 commit intomodelcontextprotocol:mainfrom
rameshreddy-adutla:fix/260-utf8-encoding-chinese-names

Conversation

@rameshreddy-adutla
Copy link

Summary

Fixes #260

Tool names containing non-ASCII characters (e.g., Chinese 天气预报) are corrupted when sent via HttpClientSseClientTransport and HttpClientStreamableHttpTransport because the Content-Type header is set to application/json without specifying the charset.

While Java's BodyPublishers.ofString() encodes the body as UTF-8 by default, the missing charset declaration in the header can cause the server to interpret the request body using a different encoding (e.g., ISO-8859-1 per HTTP/1.1 defaults), resulting in garbled tool names like 天æ°é¢Dæ¥.

Changes

  • HttpClientSseClientTransport.java: Set Content-Type: application/json; charset=utf-8 in POST requests.
  • HttpClientStreamableHttpTransport.java: Added APPLICATION_JSON_UTF8 constant for POST request Content-Type; kept APPLICATION_JSON for response content-type matching.

Testing

All 680 tests pass (1 pre-existing Docker failure unrelated to this change).

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 modelcontextprotocol#260

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to call tools with Chinese names using HttpClientSseClientTransport and HttpClientStreamableHttpTransport

1 participant