install commands read MCP config files in the locale encoding, silently corrupting or crashing on non-UTF-8 platforms
Description and Issues
The install commands decode MCP config files with the platform locale instead of UTF-8, in two places that both bypass the (correct) readers in mcp_config.py.
1. fastmcp/cli/install/claude_desktop.py:118 silently corrupts the user's config.
content = config_file.read_text().strip()Path.read_text() with no encoding uses locale.getpreferredencoding(False) — cp936 on a zh-CN Windows machine. Claude Desktop writes that file as UTF-8, so any value whose UTF-8 bytes happen to decode under GBK comes back as different characters. That text is then used as the existing server's env and merged into the config that update_config_file writes back as proper UTF-8 — so the mojibake is persisted permanently, and the command reports success while doing it:
install returned True
original : 'Привет-ключ' (d09fd180d0b8d0b2d0b5d1822dd0bad0bbd18ed187)
after : 'Дĥڧ֧-ܧݧ' (e8a2a9e8a489e68788e88381e6a2b0e8a48c2de6b3bbe8b0a2e8a49ee8a494)For the user this looks like Claude Desktop "lost" an API key after a fastmcp install claude-desktop, with no error anywhere — the whole read sits inside a try/except Exception that also swallows the UnicodeDecodeError case, so a value GBK cannot decode turns into Failed to install server: 'gbk' codec can't decode byte 0xa8... instead of a working install.
2. fastmcp/cli/install/shared.py:82 crashes outright.
with open(config_path) as f:
data = json.load(f)This is inside process_common_args, and the handler immediately after it only catches (json.JSONDecodeError, ValidationError). A fastmcp.json spec containing text GBK cannot decode (a CJK path is enough) raises out of json.load uncaught, so the CLI dies with a traceback rather than the intended "Invalid configuration file" message.
Both files are JSON, hence UTF-8 by RFC 8259, and the repo already treats them that way everywhere else: MCPConfig.from_file (mcp_config.py:354) and MCPConfig.write_to_file (mcp_config.py:348) both pass encoding="utf-8". These two call sites look like they predate or skipped that convention.
Steps to Reproduce
Click to expandNeeds a non-UTF-8 locale to observe (here: Windows, cp936, Python 3.11). On a UTF-8 machine the reads agree with the writes and nothing is visible — which is why this has survived.
import json
from pathlib import Path
from fastmcp.cli.install.claude_desktop import install_claude_desktop
tmp = Path("repro"); tmp.mkdir(exist_ok=True)
server_file = tmp / "server.py"
server_file.write_text("from fastmcp import FastMCP\n\nmcp = FastMCP('demo')\n", encoding="utf-8")
config_dir = tmp / "claude"; config_dir.mkdir(exist_ok=True)
config = config_dir / "claude_desktop_config.json"
api_key = "Привет-ключ"
config.write_text(json.dumps({"mcpServers": {"demo": {
"command": "uvx", "args": ["some-package"], "env": {"API_KEY": api_key}}}},
ensure_ascii=False, indent=2), encoding="utf-8")
install_claude_desktop(server_file, "mcp", "demo", config_path=config_dir)
saved = json.loads(config.read_text(encoding="utf-8"))
print(saved["mcpServers"]["demo"]["env"]["API_KEY"] == api_key) # False; value is now mojibakeFor the second site, a fastmcp.json of {"source": {"path": "服务器.py"}} raises UnicodeDecodeError out of process_common_args.
Both are covered by two tests added in the PR; each fails before the change and passes after it.
Expected Behavior
Config files are read as UTF-8 regardless of the platform locale, matching how MCPConfig.from_file reads them and how they are written back.
Proposed fix
Pass encoding="utf-8" at the two read sites. Two lines, no behaviour change on UTF-8 platforms. Opening a PR with those two lines plus the regression tests, unless you would rather it wait for #5357 to land first.
Related: #5354 / PR #5357 is the same class of defect in generate-cli (writes instead of reads). I deliberately kept them separate rather than bundling, and am fine with either landing first.
Source: PrefectHQ/fastmcp