diff --git a/packages/agentenv-protocol/src/agentenv_protocol/tool_client.py b/packages/agentenv-protocol/src/agentenv_protocol/tool_client.py index 77cfab019..398401156 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/tool_client.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/tool_client.py @@ -150,9 +150,11 @@ async def _exchange(self, message: dict) -> dict: raise ToolSessionError(f"{message['method']}: the event stream ended before the reply") if content_type.startswith("application/json"): reply = json.loads(await response.aread()) - if isinstance(reply, dict): - return reply - raise ToolSessionError(f"{message['method']}: the reply is not a JSON-RPC object") + if not isinstance(reply, dict): + raise ToolSessionError(f"{message['method']}: the reply is not a JSON-RPC object") + if reply.get("id") != message["id"]: + raise ToolSessionError(f"{message['method']}: the reply ID does not match the request") + return reply except ValueError as e: raise ToolSessionError(f"{message['method']}: unreadable reply: {e}") from e raise ToolSessionError(f"{message['method']}: unexpected content type {content_type!r}") diff --git a/packages/agentenv-protocol/tests/test_tool_client.py b/packages/agentenv-protocol/tests/test_tool_client.py index 410219f4c..61d56b4a3 100644 --- a/packages/agentenv-protocol/tests/test_tool_client.py +++ b/packages/agentenv-protocol/tests/test_tool_client.py @@ -315,6 +315,10 @@ async def test_a_session_the_server_dropped_raises_and_says_the_state_may_be_gon (lambda m: httpx.Response(200, headers={"content-type": "text/plain"}, content=b"hi"), "tools/call: unexpected content type 'text/plain'"), (lambda m: httpx.Response(200, json=[_reply(m, {})]), "tools/call: the reply is not a JSON-RPC object"), + (lambda m: httpx.Response(200, json=_reply({"id": m["id"] + 1}, {"content": []})), + "tools/call: the reply ID does not match the request"), + (lambda m: httpx.Response(200, json={"jsonrpc": "2.0", "result": {"content": []}}), + "tools/call: the reply ID does not match the request"), (lambda m: httpx.Response(200, json={"jsonrpc": "2.0", "id": m["id"], "result": "ok"}), "tools/call: the reply has no result object"), (lambda m: httpx.Response(200, json=_reply(m, {"content": "not a list"})), "tools/call: unreadable result"), @@ -322,8 +326,8 @@ async def test_a_session_the_server_dropped_raises_and_says_the_state_may_be_gon (lambda m: httpx.Response(200, json=_reply(m, {"content": [{"type": "text", "text": 7}]})), "tools/call: unreadable result"), ], ids=["jsonrpc-error", "http-500", "bad-json", "bad-event-json", "no-reply-in-stream", "unterminated-event", - "wrong-content-type", "batch-reply", "result-not-object", "result-wrong-shape", "text-block-without-text", - "text-block-with-a-number"]) + "wrong-content-type", "batch-reply", "wrong-reply-id", "missing-reply-id", "result-not-object", + "result-wrong-shape", "text-block-without-text", "text-block-with-a-number"]) async def test_a_failure_at_the_mcp_level_raises_a_tool_session_error(scripted, answer, match): scripted.answers["tools/call"] = answer async with ToolSession(mcp_url=_URL) as session: