modelcontextprotocol / modelcontextprotocol/java-sdk

StdioClientTransport missing explicit UTF-8 charset in InputStreamReader (same issue as #295, but on client side)

未关闭 适合新手
#898 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug P2 ready for work
主要语言
Java
星标
3.7k
派生
1.1k
平均合并
1 天 15 小时
30 天内合并 PR
9

描述

Bug description

StdioClientTransport has the same encoding mismatch issue that was identified in #295 and fixed for StdioServerTransportProvider in #826 — but the fix was only applied to the server side. The client transport still lacks explicit UTF-8 charset
specification when reading from the subprocess.

In startInboundProcessing, the InputStreamReader is created without specifying a charset:

https://github.com/modelcontextprotocol/java-sdk/blob/main/mcp-core/src/main/java/io/modelcontextprotocol/client/transport/StdioClientTransport.java#L249

try (BufferedReader processReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {

Similarly, in startErrorProcessing:

https://github.com/modelcontextprotocol/java-sdk/blob/main/mcp-core/src/main/java/io/modelcontextprotocol/client/transport/StdioClientTransport.java#L182-L183

try (BufferedReader processErrorReader = new BufferedReader(
        new InputStreamReader(process.getErrorStream()))) {

Meanwhile, startOutboundProcessing already correctly specifies UTF-8:

os.write(jsonMessage.getBytes(StandardCharsets.UTF_8));
os.write("\n".getBytes(StandardCharsets.UTF_8));

This is the exact same inconsistency that #295 reported for StdioServerTransportProvider, and that #826 fixed — only on the server side.

Steps to reproduce

  1. Start a JVM with default charset set to something other than UTF-8 (e.g., -Dfile.encoding=COMPAT on Windows with Japanese locale, which resolves to MS932/Shift_JIS)
  2. Connect to an MCP server via StdioClientTransport
  3. Call a tool that returns multi-byte UTF-8 characters (e.g., Japanese, Chinese, Korean, emoji) in its response

Expected behavior

Multi-byte characters in the server's JSON-RPC response should be decoded correctly, since the MCP stdio transport specification requires UTF-8.

Actual behavior

The InputStreamReader uses Charset.defaultCharset() instead of UTF-8. When the default charset is not UTF-8, the response bytes are decoded with the wrong charset, corrupting multi-byte characters. This corruption can also break the JSON structure itself,
resulting in JsonParseException:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('' (code 92)): was expecting double-quote to start field name

For example, with MS932 as the default charset, the last byte of certain UTF-8 characters (0x8B, etc.) is interpreted as a MS932 lead byte, which then consumes the following byte — potentially a JSON structural character like \ (0x5C). This shifts the parser
state and breaks JSON parsing entirely.

Environment

  • MCP Java SDK version: 1.1.1
  • Java version: 21
  • OS: Windows 11 (Japanese locale, default charset MS932 with -Dfile.encoding=COMPAT)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 mcp-core/src/main/java/io/modelcontextprotocol/client/transport/StdioClientTransport.java 开始,阅读 startErrorProcessing 和 startInboundProcessing,并结合 startOutboundProcessing 中现有的 UTF-8 处理。对比 #826 中相关的服务端修复,并验证当 JVM 默认字符集不是 UTF-8 时,响应和错误仍能被正确解码。

由索引模型根据 Issue 内容生成。

评估

技术栈
java
领域
backend
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
冷清
描述清晰度
描述清楚
新手友好度
74/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。