fix(mcp): handle null JSON-RPC id without aborting the handshake

`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.
This commit is contained in:
Brad Shannon 2026-06-17 17:55:50 -04:00
parent 071ed99a32
commit 4a2ba64985

View file

@ -128,7 +128,7 @@ async def handle_mcp_message(
# Handle result # Handle result
if "result" in payload: if "result" in payload:
result = payload["result"] result = payload["result"]
msg_id = int(payload.get("id", 0)) msg_id = int(payload.get("id") or 0)
# Check for tool call response first # Check for tool call response first
if msg_id in mcp_client.call_results: if msg_id in mcp_client.call_results:
@ -228,7 +228,7 @@ async def handle_mcp_message(
error_msg = error_data.get("message", "未知错误") error_msg = error_data.get("message", "未知错误")
logger.bind(tag=TAG).error(f"收到MCP错误响应: {error_msg}") 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: if msg_id in mcp_client.call_results:
await mcp_client.reject_call_result( await mcp_client.reject_call_result(
msg_id, Exception(f"MCP错误: {error_msg}") msg_id, Exception(f"MCP错误: {error_msg}")