Fix UTF-8 encoding for non-ASCII tool names in HTTP client transports#850
Open
rameshreddy-adutla wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #260
Tool names containing non-ASCII characters (e.g., Chinese
天气预报) are corrupted when sent viaHttpClientSseClientTransportandHttpClientStreamableHttpTransportbecause theContent-Typeheader is set toapplication/jsonwithout 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: SetContent-Type: application/json; charset=utf-8in POST requests.HttpClientStreamableHttpTransport.java: AddedAPPLICATION_JSON_UTF8constant for POST request Content-Type; keptAPPLICATION_JSONfor response content-type matching.Testing
All 680 tests pass (1 pre-existing Docker failure unrelated to this change).