Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/mcp2cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ def _apply_head(data, n: int):
def _emit_json(data, pretty: bool = False) -> None:
"""Print *data* as JSON. Indented when *pretty* or stdout is a TTY, else compact."""
if pretty or sys.stdout.isatty():
print(json.dumps(data, indent=2))
print(json.dumps(data, indent=2, ensure_ascii=False))
else:
print(json.dumps(data))
print(json.dumps(data, ensure_ascii=False))


def output_result(
Expand Down Expand Up @@ -312,7 +312,7 @@ def output_result(
if isinstance(data, str):
print(data)
else:
print(json.dumps(data))
print(json.dumps(data, ensure_ascii=False))
return
if isinstance(data, str):
try:
Expand All @@ -323,7 +323,7 @@ def output_result(
if head is not None:
data = _apply_head(data, head)
if toon:
encoded = _toon_encode(json.dumps(data))
encoded = _toon_encode(json.dumps(data, ensure_ascii=False))
if encoded is not None:
print(encoded, end="")
return
Expand Down Expand Up @@ -2093,7 +2093,7 @@ def _bake_show(argv: list[str]) -> None:
else:
masked.append([name, val[:4] + "****" if len(val) > 4 else "****"])
display["auth_headers"] = masked
print(json.dumps(display, indent=2))
print(json.dumps(display, indent=2, ensure_ascii=False))


def _bake_remove(argv: list[str]) -> None:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def test_pretty_indented(self, capsys):
assert "\n " in out
assert json.loads(out) == {"a": 1}

def test_non_ascii_emitted_as_utf8(self, capsys):
output_result({"content": ["返回首页"]}, json_output=True)
out = capsys.readouterr().out
assert "\\u" not in out
assert json.loads(out) == {"content": ["返回首页"]}


# ---------------------------------------------------------------------------
# Unit tests: command serialization
Expand Down