crawl4ai
crawl4ai copied to clipboard
fix(mcp): pass ensure_ascii=False to json.dumps in mcp_bridge.py
Summary
All three json.dumps() calls in deploy/docker/mcp_bridge.py were using the
default ensure_ascii=True, which escapes every non-ASCII codepoint (CJK, emoji,
accented characters) to \uXXXX sequences.
For CJK content this inflates token counts by 2.5–3x, increasing LLM costs and eating context budget unnecessarily.
The HTTP REST API already returns native UTF-8 — MCP tool results should behave the same way.
Fix
# Before
return [t.TextContent(type="text", text=json.dumps(err))]
return [t.TextContent(type="text", text=json.dumps(res, default=str))]
# After
return [t.TextContent(type="text", text=json.dumps(err, ensure_ascii=False))]
return [t.TextContent(type="text", text=json.dumps(res, default=str, ensure_ascii=False))]
Tests
Added tests/unit/test_mcp_bridge_ensure_ascii.py:
- Baseline sanity check for
ensure_ascii=Falsebehavior - AST-level check that every
json.dumpscall inmcp_bridge.pyincludesensure_ascii=False
Fixes #1962