fix(output): emit UTF-8 JSON instead of \u escapes - #67
Open
syf2211 wants to merge 1 commit into
Open
Conversation
Set ensure_ascii=False on user-facing json.dumps() calls so non-ASCII MCP tool output (CJK, emoji, etc.) is serialized as UTF-8 rather than \uXXXX escapes, reducing token waste and improving readability. Fixes knowsuchagency#62
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Set
ensure_ascii=Falseon user-facingjson.dumps()calls so non-ASCII characters in MCP tool output (CJK, emoji, accented Latin, etc.) are serialized as UTF-8 instead of\uXXXXescape sequences.Fixes #62
Motivation
Python's default
ensure_ascii=Trueexpands every non-ASCII character from 1–2 UTF-8 bytes to 6 ASCII bytes (\uXXXX). For CJK-heavy MCP tool responses this means 4–6× more tokens when output is consumed by an LLM, plus harder-to-read stdout for debugging.Changes
_emit_json()— pretty and compact JSON output pathsoutput_result()—--rawdict fallback and--toonpre-encoding_bake_show()— baked tool config displaytest_non_ascii_emitted_as_utf8intests/test_json.pyInternal persistence paths (cache, usage tracking, OAuth meta, baked config files, session daemon IPC) are intentionally unchanged to preserve cache-key stability and cross-platform file safety.
Tests
Notes
On legacy non-UTF-8 stdout encodings,
print()could raiseUnicodeEncodeErrorwhere escaped output would not. This trade-off is expected for a CLI targeting UTF-8 JSON consumers and matches the issue intent.