Skip to content

fix: add ensure_ascii=False to json.dumps in MCP server#425

Open
barry3406 wants to merge 1 commit intoMemPalace:developfrom
barry3406:fix/detect-room-empty-content
Open

fix: add ensure_ascii=False to json.dumps in MCP server#425
barry3406 wants to merge 1 commit intoMemPalace:developfrom
barry3406:fix/detect-room-empty-content

Conversation

@barry3406
Copy link
Copy Markdown

Fixes #359

json.dumps in the MCP server didn't set ensure_ascii=False, so non-ASCII text (Chinese, Japanese, etc.) would either get escaped to \uXXXX or crash outright when lone surrogate characters showed up from certain MCP client encoding paths.

Before:

json.dumps(result, indent=2)          # line 907 — tool result
json.dumps(response)                  # line 937 — stdout writer

Chinese text → UnicodeEncodeError or garbled \uXXXX output.

After:

json.dumps(result, indent=2, ensure_ascii=False)
json.dumps(response, ensure_ascii=False)

Chinese text → preserved as-is in the JSON output.

Added two tests in test_mcp_server.py:

  • test_tool_result_preserves_unicode — end-to-end through handle_request with Chinese text
  • test_main_loop_writes_unicode — verifies the serialization doesn't crash

All 35 tests pass.

json.dumps without ensure_ascii=False escapes non-ASCII characters to
\uXXXX sequences, which breaks when the input contains lone surrogate
characters from certain encoding round-trips (e.g. Chinese text through
some MCP clients). The serialization crashes instead of returning valid
JSON.

Applied to both the tool result serialization and the main loop stdout
writer. Added tests verifying non-ASCII text survives the round-trip.

Fixes MemPalace#359
Copy link
Copy Markdown

@web3guru888 web3guru888 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean fix, +1. We use ensure_ascii=False in our integration for exactly this reason — scientific terms with Greek letters (α, β, γ), mathematical symbols (∑, ∇), and CJK references get mangled into \uXXXX escape sequences without it.

The change covers both the right spots: the tool result serialization in handle_request and the stdout writer in main(). Good that the tests cover round-trip serialization with actual CJK characters rather than just asserting no exception.

One consideration for downstream: any MCP client that was parsing the escaped \uXXXX sequences will now see raw UTF-8 bytes instead. This is correct per the JSON spec (both are valid), but if anyone was doing naive string matching on the escaped form, they'd need to update. Probably not a concern in practice.

🔭 Reviewed as part of the MemPalace-AGI integration project — autonomous research with perfect memory. Community interaction updates are posted regularly on the dashboard.

JerryLiu720 pushed a commit to JerryLiu720/mempalace that referenced this pull request Apr 10, 2026
This PR addresses Windows encoding issues and improves non-ASCII
character handling throughout the MCP server.

**Changes:**

1. **stdin/stdout UTF-8 enforcement (MemPalace#503)**
   - Windows defaults to cp936/GBK, causing crashes with CJK content
   - Wrapped stdin with 'surrogateescape' error handler (tolerant input)
   - Wrapped stdout with 'replace' error handler (safe output)
   - Added hasattr() checks for edge cases (IDE/test environments)

2. **MCP tool result serialization**
   - Added ensure_ascii=False to tool result json.dumps() (line 907)
   - Preserves Unicode characters in tool responses

3. **JSON-RPC response serialization (MemPalace#359)**
   - Added ensure_ascii=False to main loop json.dumps() (line 949)
   - Prevents Chinese characters from being escaped to \uXXXX

**Technical Details:**

- stdin uses 'surrogateescape': handles malformed input gracefully
- stdout uses 'replace': prevents lone surrogates in JSON-RPC responses
  (per recommendation from MemPalace#503 discussion to avoid breaking client parsers)
- All json.dumps() calls now preserve Unicode with ensure_ascii=False

Fixes MemPalace#503
Related to MemPalace#359 (already has PR MemPalace#425 with tests)

Made-with: Cursor
@bensig bensig changed the base branch from main to develop April 11, 2026 22:22
JerryLiu720 pushed a commit to JerryLiu720/mempalace that referenced this pull request Apr 13, 2026
This PR addresses Windows encoding issues and improves non-ASCII
character handling throughout the MCP server.

**Changes:**

1. **stdin/stdout UTF-8 enforcement (MemPalace#503)**
   - Windows defaults to cp936/GBK, causing crashes with CJK content
   - Wrapped stdin with 'surrogateescape' error handler (tolerant input)
   - Wrapped stdout with 'replace' error handler (safe output)
   - Added hasattr() checks for edge cases (IDE/test environments)

2. **MCP tool result serialization**
   - Added ensure_ascii=False to tool result json.dumps() (line 907)
   - Preserves Unicode characters in tool responses

3. **JSON-RPC response serialization (MemPalace#359)**
   - Added ensure_ascii=False to main loop json.dumps() (line 949)
   - Prevents Chinese characters from being escaped to \uXXXX

**Technical Details:**

- stdin uses 'surrogateescape': handles malformed input gracefully
- stdout uses 'replace': prevents lone surrogates in JSON-RPC responses
  (per recommendation from MemPalace#503 discussion to avoid breaking client parsers)
- All json.dumps() calls now preserve Unicode with ensure_ascii=False

Fixes MemPalace#503
Related to MemPalace#359 (already has PR MemPalace#425 with tests)

Made-with: Cursor
@igorls igorls added area/mcp MCP server and tools bug Something isn't working labels Apr 14, 2026
JerryLiu720 pushed a commit to JerryLiu720/mempalace that referenced this pull request Apr 14, 2026
This PR addresses Windows encoding issues and improves non-ASCII
character handling throughout the MCP server.

**Changes:**

1. **stdin/stdout UTF-8 enforcement (MemPalace#503)**
   - Windows defaults to cp936/GBK, causing crashes with CJK content
   - Wrapped stdin with 'surrogateescape' error handler (tolerant input)
   - Wrapped stdout with 'replace' error handler (safe output)
   - Added hasattr() checks for edge cases (IDE/test environments)

2. **MCP tool result serialization**
   - Added ensure_ascii=False to tool result json.dumps() (line 907)
   - Preserves Unicode characters in tool responses

3. **JSON-RPC response serialization (MemPalace#359)**
   - Added ensure_ascii=False to main loop json.dumps() (line 949)
   - Prevents Chinese characters from being escaped to \uXXXX

**Technical Details:**

- stdin uses 'surrogateescape': handles malformed input gracefully
- stdout uses 'replace': prevents lone surrogates in JSON-RPC responses
  (per recommendation from MemPalace#503 discussion to avoid breaking client parsers)
- All json.dumps() calls now preserve Unicode with ensure_ascii=False

Fixes MemPalace#503
Related to MemPalace#359 (already has PR MemPalace#425 with tests)

Made-with: Cursor
JerryLiu720 pushed a commit to JerryLiu720/mempalace that referenced this pull request Apr 15, 2026
This PR addresses Windows encoding issues and improves non-ASCII
character handling throughout the MCP server.

**Changes:**

1. **stdin/stdout UTF-8 enforcement (MemPalace#503)**
   - Windows defaults to cp936/GBK, causing crashes with CJK content
   - Wrapped stdin with 'surrogateescape' error handler (tolerant input)
   - Wrapped stdout with 'replace' error handler (safe output)
   - Added hasattr() checks for edge cases (IDE/test environments)

2. **MCP tool result serialization**
   - Added ensure_ascii=False to tool result json.dumps() (line 907)
   - Preserves Unicode characters in tool responses

3. **JSON-RPC response serialization (MemPalace#359)**
   - Added ensure_ascii=False to main loop json.dumps() (line 949)
   - Prevents Chinese characters from being escaped to \uXXXX

**Technical Details:**

- stdin uses 'surrogateescape': handles malformed input gracefully
- stdout uses 'replace': prevents lone surrogates in JSON-RPC responses
  (per recommendation from MemPalace#503 discussion to avoid breaking client parsers)
- All json.dumps() calls now preserve Unicode with ensure_ascii=False

Fixes MemPalace#503
Related to MemPalace#359 (already has PR MemPalace#425 with tests)

Made-with: Cursor
JerryLiu720 pushed a commit to JerryLiu720/mempalace that referenced this pull request Apr 15, 2026
This PR addresses Windows encoding issues and improves non-ASCII
character handling throughout the MCP server.

**Changes:**

1. **stdin/stdout UTF-8 enforcement (MemPalace#503)**
   - Windows defaults to cp936/GBK, causing crashes with CJK content
   - Wrapped stdin with 'surrogateescape' error handler (tolerant input)
   - Wrapped stdout with 'replace' error handler (safe output)
   - Added hasattr() checks for edge cases (IDE/test environments)

2. **MCP tool result serialization**
   - Added ensure_ascii=False to tool result json.dumps() (line 907)
   - Preserves Unicode characters in tool responses

3. **JSON-RPC response serialization (MemPalace#359)**
   - Added ensure_ascii=False to main loop json.dumps() (line 949)
   - Prevents Chinese characters from being escaped to \uXXXX

**Technical Details:**

- stdin uses 'surrogateescape': handles malformed input gracefully
- stdout uses 'replace': prevents lone surrogates in JSON-RPC responses
  (per recommendation from MemPalace#503 discussion to avoid breaking client parsers)
- All json.dumps() calls now preserve Unicode with ensure_ascii=False

Fixes MemPalace#503
Related to MemPalace#359 (already has PR MemPalace#425 with tests)

Made-with: Cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/mcp MCP server and tools bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

【bug】json.dumps(response) does not set ensure_ascii=False

3 participants