Fix Windows MCP handshake crash under PYTHONUTF8 - #42
Open
Facetomyself wants to merge 1 commit into
Open
Conversation
tasklist/netstat emit OEM/GBK on non-English Windows. With UTF-8 mode, subprocess text=True decodes as UTF-8, the reader thread raises UnicodeDecodeError, check_output returns None, and out.strip() kills the stdio server before initialize. Decode console utilities with encoding=oem (UTF-8 elsewhere) and errors=replace. Treat empty/None stdout as no GUI instances. Accept localized LISTENING states. Never let rediscovery exceptions prevent MCP initialize so headless idalib tools still start.
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.
Problem
On Chinese (and other non-English) Windows, MCP clients such as Grok start Python in UTF-8 mode (
PYTHONUTF8=1).ida-multi-mcpscans for live IDA GUI processes withtasklist /FO CSVandnetstatbefore answering MCPinitialize.Those utilities emit OEM/GBK. With
subprocess.check_output(..., text=True)and noencoding=, CPython decodes as UTF-8. The stdout reader thread raisesUnicodeDecodeError(oftenbyte 0xb1),check_outputreturnsNone, andout.strip()crashes the process. The client reports:handshake failed: connection closed: initialize responseThis happens even when no IDA GUI is running. Headless
idalib_*tools never get a chance to start.Reproduced with Python 3.13 UTF-8 mode:
text=TrueontasklistreturnsNone; the raw bytes decode with GBK/OEM.Fix
encoding="oem"on Windows and UTF-8 elsewhere, alwayserrors="replace".Nonestdout as "no GUI instances" instead of crashing.LISTENING/侦听) and IPv6 local addresses.initialize.Tests
pytest tests/test_health.py: 18 passed, 3 skipped (Unix-only cases on Windows).Live stdio
initializeunderPYTHONUTF8=1now returnsserverInfo.name=ida-multi-mcpwith noUnicodeDecodeError.