Skip to content

fix(mcp/mcp_proxy): pin file IO encoding to UTF-8#2826

Merged
vvlrff merged 3 commits into
ag2ai:mainfrom
genisis0x:fix/mcp-proxy-utf8
May 21, 2026
Merged

fix(mcp/mcp_proxy): pin file IO encoding to UTF-8#2826
vvlrff merged 3 commits into
ag2ai:mainfrom
genisis0x:fix/mcp-proxy-utf8

Conversation

@genisis0x
Copy link
Copy Markdown
Contributor

Description

autogen/mcp/mcp_proxy/mcp_proxy.py performs four file IO operations through the bare locale default:

  • main_path.open("r") then main_path.open("w") rewriting the post-fastapi-codegen main.py (mcp_proxy.py:295,302).
  • open(output_file, "w") saving the rendered server-configuration template (mcp_proxy.py:438).
  • Path(config_file).open("r") reading saved server configuration (mcp_proxy.py:442).

open(...) honors locale.getpreferredencoding(False). On Windows that resolves to cp1252 and any non-cp1252 glyph in an OpenAPI spec — internationalized model names, smart quotes in descriptions, emoji in example payloads — raises UnicodeEncodeError mid-write, killing the proxy generation step. Same class of bug as #1731 / PRs #2818 / #2819 / #2825.

This change pins encoding="utf-8" on all four call sites.

Tests

Added test/mcp/test_mcp_proxy_utf8.py — source-level regression check that asserts the kwarg appears on the four call sites. Runs on every CI lane (no MCP extras required).

$ pytest test/mcp/test_mcp_proxy_utf8.py -v
test_mcp_proxy_file_writes_pin_utf8 PASSED

Checklist

AI Disclosure

  • No AI tools were used
  • AI tools were used (details below)

mcp_proxy.py rewrote the generated main.py (post-fastapi-codegen patch)
and saved the rendered server-configuration template through bare
locale-default open()/Path.open() calls. On Windows that resolves to
cp1252 and any non-cp1252 glyph in an OpenAPI spec — internationalized
model names, smart quotes in descriptions, emoji in example payloads —
raised UnicodeEncodeError mid-write, killing the proxy generation step.

Pin encoding="utf-8" on the four call sites (main_path read/write,
rendered config write, config_file read). Adds source-level regression
coverage in test/mcp/test_mcp_proxy_utf8.py that runs on every CI lane
(no MCP extras required) so the kwarg cannot silently regress.
Copy link
Copy Markdown
Collaborator

@marklysze marklysze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct — four open() / Path.open() calls in mcp_proxy.py now carry encoding="utf-8". Especially important for OpenAPI spec payloads that commonly include non-ASCII characters in descriptions and examples. LGTM.

Copy link
Copy Markdown
Collaborator

@marklysze marklysze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three open(path, "w")open(path, "w", encoding="utf-8") patches in the MCP proxy code generator. The files touched (main.py read/write and the Jinja config dump) all handle code or config text that can contain Unicode. No separate test, but these are code-gen paths where an encoding mock test would add significant scaffolding for minimal coverage gain — the pattern is simple enough to approve on code review alone. Approved.

vvlrff added 2 commits May 21, 2026 22:42
Removed outdated regression comment regarding MCP proxy file writes and UTF-8 handling.
@vvlrff vvlrff enabled auto-merge May 21, 2026 19:57
@codecov
Copy link
Copy Markdown

codecov Bot commented May 21, 2026

Codecov Report

❌ Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
autogen/mcp/mcp_proxy/mcp_proxy.py 0.00% 4 Missing ⚠️
Files with missing lines Coverage Δ
autogen/mcp/mcp_proxy/mcp_proxy.py 22.64% <0.00%> (+22.64%) ⬆️

... and 553 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vvlrff vvlrff added this pull request to the merge queue May 21, 2026
Merged via the queue into ag2ai:main with commit f50307a May 21, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants