Skip to content

fix(mcp): force UTF-8 encoding for Python stdio servers on Windows#909

Open
bj456736 wants to merge 1 commit into
MoonshotAI:mainfrom
bj456736:fix/mcp-stdio-utf8-python-windows
Open

fix(mcp): force UTF-8 encoding for Python stdio servers on Windows#909
bj456736 wants to merge 1 commit into
MoonshotAI:mainfrom
bj456736:fix/mcp-stdio-utf8-python-windows

Conversation

@bj456736

Copy link
Copy Markdown
Contributor

Python MCP servers on Windows crash with +UnicodeEncodeError+ when printing non-ASCII characters (e.g. ✓ U+2713) because the default console code page (e.g. cp1252) cannot encode them.

The MCP SDK's StdioClientTransport spawns child processes with a filtered environment that strips most parent variables. When stdout/stderr is a pipe rather than a console, Python falls back to the system's ANSI code page, which breaks any Unicode output.

Fix: inject PYTHONIOENCODING=utf-8 and PYTHONUTF8=1 into the merged env for every stdio MCP client when not already explicitly set by the user. This ensures JSON-RPC over stdio is always UTF-8 regardless of platform.

Changes:

  • Add UTF-8 env vars in mergeStdioEnv when absent
  • Add unit tests for injection, inheritance, and override behavior

Closes #886

Python MCP servers on Windows crash with UnicodeEncodeError when printing
non-ASCII characters (e.g. ✓ U+2713) because the default console code page
(e.g. cp1252) cannot encode them.

The MCP SDK's StdioClientTransport spawns child processes with a filtered
environment that strips most parent variables. When stdout/stderr is a pipe
rather than a console, Python falls back to the system's ANSI code page,
which breaks any Unicode output.

Fix: inject PYTHONIOENCODING=utf-8 and PYTHONUTF8=1 into the merged env
for every stdio MCP client when not already explicitly set by the user.
This ensures JSON-RPC over stdio is always UTF-8 regardless of platform.

- Add UTF-8 env vars in mergeStdioEnv when absent
- Add unit tests for injection, inheritance, and override behavior

Closes MoonshotAI#886
@changeset-bot

changeset-bot Bot commented Jun 19, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 6c7b5cc

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jun 19, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@6c7b5cc
npx https://pkg.pr.new/@moonshot-ai/kimi-code@6c7b5cc

commit: 6c7b5cc

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6c7b5cc083

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +248 to +249
if (merged['PYTHONUTF8'] === undefined) {
merged['PYTHONUTF8'] = '1';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid forcing Python UTF-8 mode globally

For Windows Python MCP servers that read locale-encoded files with the default text encoding, this sets process-wide UTF-8 mode and changes behavior outside stdio; for example a server that previously read cp1252 user files via open(path) can now fail with a decode error. The stdio UnicodeEncodeError described here is already addressed by PYTHONIOENCODING=utf-8, so defaulting PYTHONUTF8=1 creates a broader compatibility regression unless the user knows to override it per server.

Useful? React with 👍 / 👎.

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.

MCP stdio server crashes on Windows with UnicodeEncodeError: 'charmap' codec can't encode character '\u2713'

1 participant