fix(mcp): force UTF-8 encoding for Python stdio servers on Windows#909
fix(mcp): force UTF-8 encoding for Python stdio servers on Windows#909bj456736 wants to merge 1 commit into
Conversation
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
|
commit: |
There was a problem hiding this comment.
💡 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".
| if (merged['PYTHONUTF8'] === undefined) { | ||
| merged['PYTHONUTF8'] = '1'; |
There was a problem hiding this comment.
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 👍 / 👎.
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-8andPYTHONUTF8=1into 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:
mergeStdioEnvwhen absentCloses #886