fix: resolve Windows UTF-8 encoding issue for international characters#3
Merged
fix: resolve Windows UTF-8 encoding issue for international characters#3
Conversation
Fixes issue #1 where UTF-8 characters (Chinese, etc.) were corrupted when passed through MCP on Windows systems. Changes: - Use explicit UTF-8 encoding for subprocess I/O on Windows instead of system default code page (cp1252, cp936, etc.) - Set PYTHONUTF8=1 and PYTHONIOENCODING=utf-8 environment variables - Encode input as UTF-8 bytes and decode output with error handling - Add .ps1 to Windows executable extensions - Add fallback checks for common Windows installation paths - Improve error diagnostics with platform-specific messages - Add FileNotFoundError handling with actionable guidance Tested: - Python syntax validation passes - All version numbers updated to 1.2.3 - Changelog updated with v1.2.2 and v1.2.3 entries Resolves: #1
Addresses PR review feedback: the detected codex path (.ps1, .exe, etc.) was not being used in the actual subprocess execution - it was only used for the pre-flight check. Changes: - Add _build_codex_exec_command() helper that returns the proper command list based on the detected executable type - PowerShell scripts (.ps1) are now executed via: powershell -ExecutionPolicy Bypass -File <path> exec - Windows .exe/.bat/.cmd files use the resolved absolute path - All three MCP tools now use _build_codex_exec_command() instead of hardcoded ["codex", "exec"] - Updated CHANGELOG with the new helper function This ensures that when Codex is installed as a PowerShell script, it will actually be executed correctly instead of failing with FileNotFoundError.
There was a problem hiding this comment.
Pull request overview
This pull request fixes a Windows UTF-8 encoding issue where international characters (Chinese, etc.) were corrupted when using the MCP server on Windows. The fix explicitly sets UTF-8 encoding for subprocess I/O and adds enhanced Windows CLI detection with additional installation paths and PowerShell support.
- Implements UTF-8 encoding for subprocess I/O on Windows to replace system default code page handling
- Enhances Windows CLI detection by adding
.ps1extension support and checking common installation paths - Improves error diagnostics with FileNotFoundError handling and platform-specific metadata
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/mcp_server.py | Core UTF-8 encoding fix in _run_codex_command(), enhanced Windows CLI detection in _get_codex_command(), new _build_codex_exec_command() helper, and improved FileNotFoundError handling |
| src/init.py | Version bump from 1.2.2 to 1.2.3 with updated description |
| pyproject.toml | Version bump from 1.2.2 to 1.2.3 |
| CHANGELOG.md | Added entries for v1.2.3 and v1.2.2 with detailed changelog of UTF-8 fixes and Windows compatibility improvements |
Comments suppressed due to low confidence (1)
src/mcp_server.py:699
- The general Exception handler in the batch function is missing the exception_type and platform fields in its metadata, which are included in the same handler for consult_codex (line 420) and consult_codex_with_stdin (line 555). For consistency and better debugging, the metadata should include these fields: "platform": platform.system() and "exception_type": type(e).name.
except Exception as e:
results.append({
"status": "error",
"index": i,
"query": query[:100] + "..." if len(query) > 100 else query,
"error": f"Error executing query: {str(e)}",
"metadata": {}
})
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Problem
Issue #1 reported that UTF-8 characters passed through MCP were corrupted on Windows. This was caused by the subprocess module using the system's default code page (e.g.,
cp1252,cp936) instead of UTF-8.Solution
UTF-8 Encoding Fix
PYTHONUTF8=1andPYTHONIOENCODING=utf-8environment variableserrors='replace'Enhanced Windows CLI Detection
.ps1to Windows executable extensions%LOCALAPPDATA%\Programs\codex\codex.exe%APPDATA%\npm\codex.cmd%USERPROFILE%\.cargo\bin\codex.exeImproved Error Diagnostics
FileNotFoundErrorspecific handlingplatformandexception_typefieldsTest Plan
python -m py_compile)Files Changed
src/mcp_server.py- Core fix for UTF-8 encoding and error handlingsrc/__init__.py- Version bump to 1.2.3pyproject.toml- Version bump to 1.2.3CHANGELOG.md- Added v1.2.2 and v1.2.3 entriesCloses #1