Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
8 changes: 6 additions & 2 deletions packages/agentenv-protocol/tests/test_tool_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,19 @@ 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"),
(lambda m: httpx.Response(200, json=_reply(m, {"content": [{"type": "text"}]})), "tools/call: unreadable result"),
(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:
Expand Down
Loading