Skip to content

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

Merged
Kehrlann merged 1 commit intomodelcontextprotocol:mainfrom
rameshreddy-adutla:fix/260-utf8-encoding-chinese-names
Apr 8, 2026
Merged

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

Conversation

@rameshreddy-adutla
Copy link
Copy Markdown
Contributor

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).

@Kehrlann Kehrlann force-pushed the fix/260-utf8-encoding-chinese-names branch from df4cb10 to 97576aa Compare April 8, 2026 12:22
@Kehrlann
Copy link
Copy Markdown
Contributor

Kehrlann commented Apr 8, 2026

Hey @rameshreddy-adutla . Thank you for your contribution.

Would you mind adding a test for this?

Edit: I have added tests myself.

@Kehrlann Kehrlann added area/client P2 Moderate issues affecting some users, edge cases, potentially valuable feature waiting for user Waiting for user feedback or more details labels Apr 8, 2026
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>
Co-authored-by: Daniel Garnier-Moiroux <git@garnier.wf>
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
@Kehrlann Kehrlann force-pushed the fix/260-utf8-encoding-chinese-names branch from 97576aa to 775a73d Compare April 8, 2026 12:47
@Kehrlann Kehrlann added bug Something isn't working and removed waiting for user Waiting for user feedback or more details labels Apr 8, 2026
@Kehrlann Kehrlann self-requested a review April 8, 2026 13:02
@Kehrlann Kehrlann merged commit 8fd9903 into modelcontextprotocol:main Apr 8, 2026
21 checks passed
Kehrlann added a commit that referenced this pull request Apr 8, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/client bug Something isn't working P2 Moderate issues affecting some users, edge cases, potentially valuable feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to call tools with Chinese names using HttpClientSseClientTransport and HttpClientStreamableHttpTransport

2 participants