From 4a2ba649856ce2995b9bd6d6be817eebc36d6dc1 Mon Sep 17 00:00:00 2001 From: Brad Shannon Date: Wed, 17 Jun 2026 17:55:50 -0400 Subject: [PATCH 1/2] fix(mcp): handle null JSON-RPC id without aborting the handshake MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `id` is optional in JSON-RPC and may legitimately be `null`. The previous `int(payload.get("id", 0))` only defaults when the key is absent — a present `"id": null` reaches `int(None)` and raises TypeError. The exception propagates out of the message handler, the MCP handshake never completes, and the session is left with ready=False (no device tools) and no clear error. Use `or 0` so both the missing-key and explicit-null cases collapse to 0. Applies to the result and error branches. --- .../core/providers/tools/device_mcp/mcp_handler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py b/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py index 76994cabb..58a53fab7 100644 --- a/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py +++ b/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py @@ -128,7 +128,7 @@ async def handle_mcp_message( # Handle result if "result" in payload: result = payload["result"] - msg_id = int(payload.get("id", 0)) + msg_id = int(payload.get("id") or 0) # Check for tool call response first if msg_id in mcp_client.call_results: @@ -228,7 +228,7 @@ async def handle_mcp_message( error_msg = error_data.get("message", "未知错误") logger.bind(tag=TAG).error(f"收到MCP错误响应: {error_msg}") - msg_id = int(payload.get("id", 0)) + msg_id = int(payload.get("id") or 0) if msg_id in mcp_client.call_results: await mcp_client.reject_call_result( msg_id, Exception(f"MCP错误: {error_msg}") From 1e5edbf84baf275a37b0265f64c005f03cc7ee1c Mon Sep 17 00:00:00 2001 From: Brad Shannon Date: Thu, 18 Jun 2026 11:34:14 -0400 Subject: [PATCH 2/2] test(mcp): regression tests for null JSON-RPC id handling --- main/xiaozhi-server/test/test_mcp_null_id.py | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 main/xiaozhi-server/test/test_mcp_null_id.py diff --git a/main/xiaozhi-server/test/test_mcp_null_id.py b/main/xiaozhi-server/test/test_mcp_null_id.py new file mode 100644 index 000000000..1a882fc0f --- /dev/null +++ b/main/xiaozhi-server/test/test_mcp_null_id.py @@ -0,0 +1,44 @@ +"""Regression tests for null JSON-RPC ``id`` handling in the device MCP handler. + +JSON-RPC permits ``id`` to be ``null``. Before the fix, ``int(payload.get("id", 0))`` +only defaulted when the key was *absent*; a present ``"id": null`` reached +``int(None)`` and raised ``TypeError``, aborting the MCP handshake and leaving the +session with ``ready=False``. These tests assert the handler tolerates a null id. + +Self-contained: no conftest required. +""" + +import asyncio +import pathlib +import sys + +sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) + +from core.providers.tools.device_mcp.mcp_handler import ( # noqa: E402 + MCPClient, + handle_mcp_message, +) + + +class _DummyConn: + """Minimal stand-in; the null-id code paths below never touch the connection.""" + + +def test_result_payload_with_null_id_does_not_raise(): + client = MCPClient() + # Regression: a result payload whose id is JSON null must not crash. + asyncio.run(handle_mcp_message(_DummyConn(), client, {"result": {}, "id": None})) + + +def test_error_payload_with_null_id_does_not_raise(): + client = MCPClient() + # The error branch parses the id the same way and must also tolerate null. + asyncio.run( + handle_mcp_message(_DummyConn(), client, {"error": {"message": "boom"}, "id": None}) + ) + + +def test_missing_id_still_defaults_to_zero(): + client = MCPClient() + # The original missing-key behaviour must be preserved. + asyncio.run(handle_mcp_message(_DummyConn(), client, {"result": {}}))