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
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,8 @@ def serialize_item(item: Any) -> dict[str, Any]:
else:
return {}

return json.dumps([serialize_item(item) for item in value])
return _to_json_str([serialize_item(item) for item in value])


def _to_json_str(obj: Any, indent: int | None = None) -> str:
return json.dumps(obj, ensure_ascii=False, indent=indent)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from mcp import types as mcp_types

from .._base import _to_json_str

_ELICITATION_CHOICE_SHORTHANDS = {"a": "accept", "d": "decline", "c": "cancel"}


Expand Down Expand Up @@ -82,7 +84,7 @@ async def elicit(self, params: mcp_types.ElicitRequestParams) -> mcp_types.Elici
prompt = "\n".join(
[
"Input Schema:",
json.dumps(params.requestedSchema, indent=2),
_to_json_str(params.requestedSchema, indent=2),
"Please enter a JSON string following the above schema: ",
]
)
Expand Down
34 changes: 34 additions & 0 deletions python/packages/autogen-ext/tests/tools/test_mcp_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from autogen_ext.tools.mcp._base import _to_json_str
from autogen_ext.tools.mcp._stdio import StdioMcpToolAdapter
from mcp.types import TextContent, Tool


def test_to_json_str_no_escape_japanese() -> None:
"""Verify that Japanese text is not escaped in _to_json_str."""
data = {"text": "日本語"}
serialized = _to_json_str(data)

# Check that the literal Japanese characters are in the string
assert "日本語" in serialized
# Check that the escaped unicode sequence is NOT in the string
assert "\\u65e5\\u672c\\u8a9e" not in serialized


def test_mcp_tool_adapter_return_value_no_escape_japanese() -> None:
"""Verify that McpToolAdapter.return_value_as_string does not escape Japanese text."""
# Mock parameters
from autogen_ext.tools.mcp import StdioServerParams

server_params = StdioServerParams(command="echo", args=["test"])
tool = Tool(name="test_tool", description="A test tool", inputSchema={"type": "object", "properties": {}})

adapter = StdioMcpToolAdapter(server_params=server_params, tool=tool)

content = [TextContent(type="text", text="日本語")]
serialized = adapter.return_value_as_string(content)

# Check that the literal Japanese characters are in the string
assert "日本語" in serialized
# Check that the escaped unicode sequence is NOT in the string
assert "\\u65e5\\u672c\\u8a9e" not in serialized