crawl4ai icon indicating copy to clipboard operation
crawl4ai copied to clipboard

fix(mcp): pass ensure_ascii=False to json.dumps in mcp_bridge.py

Open devteamaegis opened this issue 6 months ago • 0 comments

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=False behavior
  • AST-level check that every json.dumps call in mcp_bridge.py includes ensure_ascii=False

Fixes #1962

devteamaegis avatar May 28 '26 16:05 devteamaegis