Repository navigation
fix(config): read MCP config files as UTF-8 - #4164
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: eda3cc1560
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| """Load configuration from JSON file.""" | ||
| if file_path.exists() and (content := file_path.read_text().strip()): | ||
| if file_path.exists() and ( | ||
| content := file_path.read_text(encoding="utf-8").strip() |
There was a problem hiding this comment.
Preserve backward compatibility for pre-existing config encodings
Decoding with encoding="utf-8" unconditionally makes from_file() fail on configs written by earlier FastMCP versions on non-UTF-8 locales (for example Windows cp1252 files containing é), which previously round-tripped successfully because both read and write used the locale default. After upgrading, those existing files will raise UnicodeDecodeError and cannot be loaded. Consider a compatibility path (e.g., UTF-8 first, then locale fallback) so this bugfix does not break existing user configs.
Useful? React with 👍 / 👎.
Description
MCPConfig.from_file()andMCPConfig.write_to_file()now passencoding="utf-8"when reading and writing JSON config files.This fixes a self-evident file IO bug: MCP config values can contain Unicode in server names, descriptions, arguments, or paths, while
Path.read_text()andPath.write_text()use the platform locale by default. On Windows that can decode or encode with cp1252 instead of UTF-8. This is the same class of issue as #4084, but in MCP config file IO rather than skill files.Contribution type
Checklist
uv run prek run --all-filesand all checks passFocused checks run:
uv run pytest tests/test_mcp_config.py::test_mcp_config_file_io_uses_utf8 -quv run pytest tests/cli/test_run.py::TestMCPConfig::test_run_mcp_config -quv run ruff check fastmcp_slim/fastmcp/mcp_config.py tests/test_mcp_config.pyuv run ruff format --check fastmcp_slim/fastmcp/mcp_config.py tests/test_mcp_config.pygit diff --check