Skip to content

fix(mcp): return JSON-RPC errors for malformed frames instead of exiting - #3945

Open
tonydzi wants to merge 1 commit into
modelscope:mainfrom
tonydzi:fix-mcp-malformed-frames
Open

tonydzi wants to merge 1 commit into
modelscope:mainfrom
tonydzi:fix-mcp-malformed-frames

Conversation

@tonydzi

@tonydzi tonydzi commented Nov 24, 2026

Copy link
Copy Markdown

Fixes #3944

What was broken

The example stdio MCP server (examples/mcp_server/funasr_mcp.py) exited with status 1 on a line that is not valid JSON, and on a tools/call frame whose params is an array. A single bad frame killed the process, so later valid frames were never served.

What I changed

  • main() catches JSON parse errors and answers with JSON-RPC -32700 (id null), then keeps reading stdin.
  • Non-object frames get -32600.
  • handle_request() returns -32602 when params (or tools/call arguments) is not an object. Notifications (no id) get no reply.
  • New send_error() helper for protocol-level errors. Tool-level errors still use send_tool_error.

How I checked

Also covered: requests without an id (notifications) get no reply, a deeply nested line (RecursionError) and invalid UTF-8 bytes are treated as parse errors.

Without the fix, the 6 new tests fail (python3 -m unittest examples.mcp_server.test_funasr_mcp.FunASRMCPMalformedFrameTest gives FAILED).
With the fix, the whole file passes: python3 -m unittest examples.mcp_server.test_funasr_mcp gives Ran 16 tests ... OK.
python3 -m compileall examples/mcp_server is clean. The two repro commands from the issue now print a JSON-RPC error and exit 0. No model or audio needed.

Assisted-by: claude-code/claude-sonnet-5-5
Machine: MacBook-Anton
Account: tonydzi
Operator: anton

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed JSON-RPC frames crash the FunASR MCP example server

1 participant