gabriel / musehub public
test_mcp_url_elicitation.py python
113 lines 4.8 KB
Raw
sha256:f19d5e4a89766fc91b926cdf801255e12dcd968041c0ece8adf6627760788d22 merge: next build — MCP (#291 #292 #295 #324), profiles/sit… Opus 5.5 minor ⚠ breaking 1 day ago
1 """URL-mode elicitation completes through ``notifications/elicitation/complete`` (musehub#291).
2
3 ``elicit_url`` pends its future under the JSON-RPC ID of ``elicitation/create`` and advertises an ``elicitationId``; the
4 completion notification names the ``elicitationId``. These tests pin that the notification resolves the waiting tool, that
5 answering the request itself still works, and that decline / timeout behave as before. In-process, database-free.
6 """
7
8 from __future__ import annotations
9
10 import asyncio
11 import json
12
13 import pytest
14
15 from musehub.mcp import context, dispatcher, session
16
17 pytestmark = pytest.mark.no_db
18
19
20 async def _start(sess: session.MCPSession, eid: str | None = "eid-1") -> tuple[asyncio.Task[bool], dict[str, object]]:
21 q: asyncio.Queue[str | None] = asyncio.Queue()
22 sess.sse_queues.append(q)
23 task = asyncio.create_task(context.ToolCallContext("u", sess).elicit_url("https://x.invalid/", "connect", eid))
24 frame = await asyncio.wait_for(q.get(), 2)
25 assert frame is not None
26 return task, json.loads(frame.split("data: ", 1)[1])
27
28
29 def _session() -> session.MCPSession:
30 return session.create_session("u", {"elicitation": {"url": {}}})
31
32
33 @pytest.fixture(autouse=True)
34 def short_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
35 monkeypatch.setattr(context, "_ELICITATION_TIMEOUT_SECONDS", 1.0)
36
37
38 @pytest.mark.asyncio
39 async def test_complete_notification_resolves_the_waiting_tool_before_the_timeout() -> None:
40 sess = _session()
41 task, payload = await _start(sess)
42 assert payload["params"]["elicitationId"] == "eid-1" and payload["id"] != "eid-1" # type: ignore[index]
43 await dispatcher._dispatch("notifications/elicitation/complete", {"elicitationId": "eid-1"}, user_id=None, session=sess)
44 assert await asyncio.wait_for(task, 0.5) is True
45 assert sess.pending == {} and sess.url_elicitations == {}
46 session.delete_session(sess.session_id)
47
48
49 @pytest.mark.asyncio
50 async def test_the_complete_notification_over_http_returns_202_and_resolves() -> None:
51 from musehub.mcp.dispatcher import handle_request
52
53 sess = _session()
54 task, _ = await _start(sess, "eid-http")
55 out = await handle_request({"jsonrpc": "2.0", "method": "notifications/elicitation/complete",
56 "params": {"elicitationId": "eid-http"}}, session=sess)
57 assert out is None # a notification: the route answers 202
58 assert await asyncio.wait_for(task, 0.5) is True
59 session.delete_session(sess.session_id)
60
61
62 @pytest.mark.asyncio
63 async def test_answering_the_request_itself_still_works() -> None:
64 sess = _session()
65 task, payload = await _start(sess)
66 assert session.resolve_elicitation(sess, payload["id"], {"action": "accept"}) # type: ignore[arg-type]
67 assert await asyncio.wait_for(task, 0.5) is True
68 assert sess.url_elicitations == {}
69 session.delete_session(sess.session_id)
70
71
72 @pytest.mark.asyncio
73 async def test_decline_still_returns_false() -> None:
74 sess = _session()
75 task, payload = await _start(sess)
76 session.resolve_elicitation(sess, payload["id"], {"action": "decline"}) # type: ignore[arg-type]
77 assert await asyncio.wait_for(task, 0.5) is False
78 session.delete_session(sess.session_id)
79
80
81 @pytest.mark.asyncio
82 async def test_timeout_still_returns_false_and_forgets_the_id() -> None:
83 sess = _session()
84 task, _ = await _start(sess)
85 assert await asyncio.wait_for(task, 3) is False
86 assert sess.pending == {} and sess.url_elicitations == {}
87 assert session.complete_url_elicitation(sess, "eid-1") is False
88 session.delete_session(sess.session_id)
89
90
91 @pytest.mark.asyncio
92 async def test_an_unknown_id_or_another_sessions_id_resolves_nothing() -> None:
93 mine, other = _session(), _session()
94 task, _ = await _start(mine, "eid-mine")
95 await dispatcher._dispatch("notifications/elicitation/complete", {"elicitationId": "eid-nope"}, user_id=None, session=mine)
96 await dispatcher._dispatch("notifications/elicitation/complete", {"elicitationId": "eid-mine"}, user_id=None, session=other)
97 await asyncio.sleep(0.05)
98 assert not task.done()
99 await dispatcher._dispatch("notifications/elicitation/complete", {"elicitationId": "eid-mine"}, user_id=None, session=mine)
100 assert await asyncio.wait_for(task, 0.5) is True
101 session.delete_session(mine.session_id)
102 session.delete_session(other.session_id)
103
104
105 @pytest.mark.asyncio
106 async def test_an_auto_generated_id_is_advertised_and_completes() -> None:
107 sess = _session()
108 task, payload = await _start(sess, None)
109 eid = payload["params"]["elicitationId"] # type: ignore[index]
110 assert isinstance(eid, str) and eid
111 await dispatcher._dispatch("notifications/elicitation/complete", {"elicitationId": eid}, user_id=None, session=sess)
112 assert await asyncio.wait_for(task, 0.5) is True
113 session.delete_session(sess.session_id)
File History 3 commits
sha256:f19d5e4a89766fc91b926cdf801255e12dcd968041c0ece8adf6627760788d22 merge: next build — MCP (#291 #292 #295 #324), profiles/sit… Opus 5.5 minor ⚠ 1 day ago
sha256:87f4c4d976afad92db715748495c09600f4aafed5f924e6b4b1664aab5a86fad merge: rc26 — MCP wave (#291 #292 #295 #324) Opus 5.5 minor ⚠ 1 day ago
sha256:8f71621a510dbf00c5cef4b4a877ab154ea8c4af406d0fe7f0319a368b954f64 fix(mcp): notifications/elicitation/complete resolves the U… Opus 5.5 minor ⚠ 2 days ago