gabriel / musehub public
test_mcp_shared_sessions.py python
339 lines 15.4 KB
Raw
sha256:e6c07a0f17d2b30516cfe406c094c493c3aeaf8ea72db9ca098d24013bcc3bb9 Merge branch 'task/wave-hub-mcp-289' into dev Opus 5.5 minor ⚠ breaking 1 day ago
1 """MCP Streamable HTTP sessions are shared by every worker process (musehub#289).
2
3 Production runs several uvicorn workers and does not route requests by ``Mcp-Session-Id``. A session therefore lives
4 in the ``musehub_mcp_sessions`` table, keyed by the SHA-256 of its ID, and each worker keeps only a cache of the
5 sessions it has served plus the per-process push channel (GET /mcp streams, event buffer, pending elicitations).
6
7 A second worker is simulated by clearing the in-process cache (``session._SESSIONS``): a fresh worker process starts
8 with an empty cache and the same database. Every test runs against the real test PostgreSQL.
9 """
10 from __future__ import annotations
11
12 import asyncio
13 import hashlib
14 from collections.abc import AsyncGenerator, Iterator
15
16 import pytest
17 import pytest_asyncio
18 from httpx import ASGITransport, AsyncClient
19 from sqlalchemy import text
20 from sqlalchemy.ext.asyncio import AsyncSession
21 from starlette.requests import Request
22 from starlette.responses import StreamingResponse
23
24 from musehub.auth.request_signing import MSignContext, optional_signed_request
25 from musehub.main import app
26 from musehub.mcp import session as mcp_session
27 from tests.factories import create_repo
28
29 INIT = {
30 "jsonrpc": "2.0",
31 "id": 1,
32 "method": "initialize",
33 "params": {
34 "protocolVersion": "2025-11-25",
35 "capabilities": {"elicitation": {"form": {}, "url": {}}},
36 "clientInfo": {"name": "multi-worker-test", "version": "1"},
37 },
38 }
39 PING = {"jsonrpc": "2.0", "id": 2, "method": "ping"}
40
41
42 @pytest_asyncio.fixture
43 async def http(db_session: AsyncSession) -> AsyncGenerator[AsyncClient, None]:
44 async with AsyncClient(transport=ASGITransport(app=app), base_url="http://localhost") as c:
45 yield c
46
47
48 @pytest.fixture(autouse=True)
49 def _empty_cache() -> Iterator[None]:
50 mcp_session._SESSIONS.clear()
51 yield
52 mcp_session._SESSIONS.clear()
53
54
55 def _another_worker() -> None:
56 """The next request is served by a worker process that never saw this session."""
57 mcp_session._SESSIONS.clear()
58
59
60 def _key(sid: str) -> str:
61 return hashlib.sha256(sid.encode()).hexdigest()
62
63
64 async def _initialize(http: AsyncClient) -> str:
65 r = await http.post("/mcp", json=INIT)
66 assert r.status_code == 200, r.text
67 return r.headers["mcp-session-id"]
68
69
70 async def _post(http: AsyncClient, sid: str, body: object) -> int:
71 return (await http.post("/mcp", json=body, headers={"Mcp-Session-Id": sid})).status_code
72
73
74 async def _row(db: AsyncSession, sid: str) -> dict[str, object] | None:
75 res = await db.execute(text("SELECT * FROM musehub_mcp_sessions WHERE session_key = :k"), {"k": _key(sid)})
76 await db.commit()
77 row = res.mappings().first()
78 return dict(row) if row is not None else None
79
80
81 def _get_request(sid: str) -> Request:
82 return Request({
83 "type": "http", "method": "GET", "path": "/mcp", "query_string": b"",
84 "headers": [(b"accept", b"text/event-stream"), (b"mcp-session-id", sid.encode())],
85 })
86
87
88 # ── RED 1: a live session works on every worker ───────────────────────────────
89
90
91 async def test_a_session_issued_on_one_worker_answers_on_another(http: AsyncClient) -> None:
92 sid = await _initialize(http)
93 _another_worker()
94 assert await _post(http, sid, PING) == 200
95 _another_worker()
96 r = await http.post("/mcp", json={"jsonrpc": "2.0", "id": 3, "method": "tools/list"}, headers={"Mcp-Session-Id": sid})
97 assert r.status_code == 200 and r.json()["result"]["tools"]
98
99
100 async def test_the_row_records_what_initialize_established(db_session: AsyncSession) -> None:
101 agent = MSignContext(handle="bot-289", identity_id="id-289", is_agent=True, is_admin=False)
102 app.dependency_overrides[optional_signed_request] = lambda: agent
103 try:
104 async with AsyncClient(transport=ASGITransport(app=app), base_url="http://localhost") as http:
105 sid = await _initialize(http)
106 finally:
107 app.dependency_overrides.pop(optional_signed_request, None)
108 row = await _row(db_session, sid)
109 assert row is not None
110 assert row["session_key"] != sid # the bearer ID itself is never stored
111 assert row["user_handle"] == "bot-289" and row["is_agent"] is True and row["agent_name"] == "bot-289"
112 assert row["protocol_version"] == "2025-11-25"
113 assert row["client_capabilities"] == {"elicitation": {"form": {}, "url": {}}}
114 assert row["client_info"] == {"name": "multi-worker-test", "version": "1"}
115 assert row["expires_at"] > row["last_seen_at"] >= row["created_at"]
116
117 _another_worker()
118 hydrated = await mcp_session.resolve_session(sid, ("bot-289", True))
119 assert hydrated is not None and hydrated.user_id == "bot-289" and hydrated.is_agent
120 assert hydrated.supports_elicitation_url() and hydrated.protocol_version == "2025-11-25"
121
122
123 async def test_repo_focus_follows_the_session_to_another_worker(http: AsyncClient, db_session: AsyncSession) -> None:
124 repo = await create_repo(db_session, owner="focus-owner", slug="focus-repo", visibility="public")
125 sid = await _initialize(http)
126 call = {"jsonrpc": "2.0", "id": 4, "method": "tools/call",
127 "params": {"name": "musehub_set_context", "arguments": {"owner": repo.owner, "slug": repo.slug}}}
128 r = await http.post("/mcp", json=call, headers={"Mcp-Session-Id": sid})
129 assert r.status_code == 200 and not r.json()["result"].get("isError"), r.text
130 _another_worker()
131 assert await _post(http, sid, PING) == 200
132 assert mcp_session._SESSIONS[sid].repo_focus == (repo.owner, repo.slug)
133
134
135 async def test_get_on_a_worker_without_the_stream_opens_a_fresh_one(http: AsyncClient) -> None:
136 from musehub.api.routes.mcp import mcp_get
137
138 sid = await _initialize(http)
139 _another_worker()
140 resp = await mcp_get(_get_request(sid), None)
141 assert isinstance(resp, StreamingResponse) and resp.status_code == 200
142 assert sid in mcp_session._SESSIONS # this worker now holds the session for its push channel
143 assert (await mcp_get(_get_request("not-a-session"), None)).status_code == 404
144
145
146 # ── DELETE and expiry still end the session everywhere ───────────────────────
147
148
149 async def test_delete_on_another_worker_ends_the_session_everywhere(http: AsyncClient, db_session: AsyncSession) -> None:
150 sid = await _initialize(http)
151 worker_a = mcp_session._SESSIONS[sid]
152 stream: asyncio.Queue[str | None] = asyncio.Queue()
153 worker_a.sse_queues.append(stream)
154 saved = dict(mcp_session._SESSIONS)
155 _another_worker()
156 assert (await http.delete("/mcp", headers={"Mcp-Session-Id": sid})).status_code == 200
157 assert await _row(db_session, sid) is None
158 assert (await http.delete("/mcp", headers={"Mcp-Session-Id": sid})).status_code == 404
159
160 mcp_session._SESSIONS.update(saved) # back on worker A, which still caches the session and its stream
161 assert await _post(http, sid, PING) == 404
162 assert sid not in mcp_session._SESSIONS and stream.get_nowait() is None # its push channel was closed
163
164
165 async def test_an_expired_session_is_gone_on_every_worker(http: AsyncClient, db_session: AsyncSession) -> None:
166 sid = await _initialize(http)
167 await db_session.execute(text("UPDATE musehub_mcp_sessions SET expires_at = now() - interval '1 second' "
168 "WHERE session_key = :k"), {"k": _key(sid)})
169 await db_session.commit()
170 assert await _post(http, sid, PING) == 404 # the worker that issued it, cache and all
171 assert await _row(db_session, sid) is None # dropped on lookup
172 _another_worker()
173 assert await _post(http, sid, PING) == 404
174
175
176 async def test_last_seen_is_touched_at_most_once_per_interval(http: AsyncClient, db_session: AsyncSession) -> None:
177 sid = await _initialize(http)
178 first = await _row(db_session, sid)
179 assert first is not None
180 assert await _post(http, sid, PING) == 200
181 assert await _row(db_session, sid) == first # inside the interval: a read, no write
182
183 await db_session.execute(text("UPDATE musehub_mcp_sessions SET last_seen_at = last_seen_at - make_interval(secs => :s) "
184 "WHERE session_key = :k"), {"k": _key(sid), "s": mcp_session._TOUCH_INTERVAL_SECONDS + 1})
185 await db_session.commit()
186 backdated = await _row(db_session, sid)
187 assert backdated is not None
188 _another_worker()
189 assert await _post(http, sid, PING) == 200
190 touched = await _row(db_session, sid)
191 assert touched is not None
192 assert touched["last_seen_at"] > backdated["last_seen_at"] and touched["expires_at"] > first["expires_at"]
193
194
195 # ── The bounded sweep ─────────────────────────────────────────────────────────
196
197
198 async def test_sweep_removes_expired_rows_and_closes_streams_of_ended_sessions(http: AsyncClient, db_session: AsyncSession) -> None:
199 live, expired, deleted = [x async for x in _three(http)]
200 stream: asyncio.Queue[str | None] = asyncio.Queue()
201 mcp_session._SESSIONS[deleted].sse_queues.append(stream)
202 await db_session.execute(text("UPDATE musehub_mcp_sessions SET expires_at = now() - interval '1 second' "
203 "WHERE session_key = :k"), {"k": _key(expired)})
204 await db_session.execute(text("DELETE FROM musehub_mcp_sessions WHERE session_key = :k"), {"k": _key(deleted)})
205 await db_session.commit()
206
207 removed = await mcp_session.sweep_sessions()
208 assert removed == 1 # the one expired row
209 assert await _row(db_session, expired) is None and await _row(db_session, live) is not None
210 assert set(mcp_session._SESSIONS) == {live}
211 assert stream.get_nowait() is None
212
213
214 async def _three(http: AsyncClient) -> AsyncGenerator[str, None]:
215 for _ in range(3):
216 yield await _initialize(http)
217
218
219 async def test_sweep_is_bounded(http: AsyncClient, db_session: AsyncSession, monkeypatch: pytest.MonkeyPatch) -> None:
220 monkeypatch.setattr(mcp_session, "_SWEEP_BATCH", 2)
221 for _ in range(3):
222 await _initialize(http)
223 await db_session.execute(text("UPDATE musehub_mcp_sessions SET expires_at = now() - interval '1 second'"))
224 await db_session.commit()
225 assert await mcp_session.sweep_sessions() == 2
226 assert await mcp_session.sweep_sessions() == 1
227
228
229 # ── Sessions are bound to the principal that created them ────────────────────
230
231 ALICE = MSignContext(handle="alice-289", identity_id="id-alice", is_agent=False, is_admin=False)
232 BOB = MSignContext(handle="bob-289", identity_id="id-bob", is_agent=False, is_admin=False)
233 ALICE_AGENT = MSignContext(handle="alice-289", identity_id="id-alice", is_agent=True, is_admin=False)
234
235 _PAIRS = {
236 "another-handle": (ALICE, BOB),
237 "anonymous-with-signed-session": (ALICE, None),
238 "signed-with-anonymous-session": (None, ALICE),
239 "agent-flag-differs": (ALICE, ALICE_AGENT),
240 }
241
242
243 def _as(caller: MSignContext | None) -> None:
244 app.dependency_overrides[optional_signed_request] = lambda: caller
245
246
247 @pytest.fixture
248 def _callers() -> Iterator[None]:
249 yield
250 app.dependency_overrides.pop(optional_signed_request, None)
251
252
253 @pytest.mark.parametrize("pair", list(_PAIRS), ids=list(_PAIRS))
254 async def test_another_principal_is_answered_as_an_unknown_session_and_changes_nothing(
255 pair: str, http: AsyncClient, db_session: AsyncSession, _callers: None,
256 ) -> None:
257 from musehub.api.routes.mcp import mcp_get
258
259 owner, intruder = _PAIRS[pair]
260 repo = await create_repo(db_session, owner="focus-owner", slug=f"bound-{pair}", visibility="public")
261 _as(owner)
262 sid = await _initialize(http)
263 mine = mcp_session._SESSIONS[sid]
264 pending = mcp_session.create_pending_elicitation(mine, "elicit-289")
265 queues_before = list(mine.sse_queues)
266 row_before = await _row(db_session, sid)
267 unknown = (await http.post("/mcp", json=PING, headers={"Mcp-Session-Id": "no-such-session"}))
268
269 _as(intruder)
270 for body in (PING,
271 {"jsonrpc": "2.0", "id": 5, "method": "tools/call",
272 "params": {"name": "musehub_set_context", "arguments": {"owner": repo.owner, "slug": repo.slug}}},
273 {"jsonrpc": "2.0", "id": "elicit-289", "result": {"action": "accept", "content": {}}}):
274 r = await http.post("/mcp", json=body, headers={"Mcp-Session-Id": sid})
275 assert (r.status_code, r.json()) == (unknown.status_code, unknown.json()), body
276 got = await mcp_get(_get_request(sid), intruder)
277 assert got.status_code == 404 and not isinstance(got, StreamingResponse)
278 assert (await http.delete("/mcp", headers={"Mcp-Session-Id": sid})).status_code == 404
279
280 # nothing changed: same row (no touch, focus or delete), same cached session, future still pending, no stream
281 assert await _row(db_session, sid) == row_before
282 assert mcp_session._SESSIONS[sid] is mine and mine.repo_focus is None and mine.sse_queues == queues_before
283 assert not pending.done()
284 _another_worker()
285 got = await mcp_get(_get_request(sid), intruder)
286 assert got.status_code == 404 and sid not in mcp_session._SESSIONS # nothing rebuilt for the intruder
287
288 _as(owner)
289 assert await _post(http, sid, PING) == 200
290 assert (await http.delete("/mcp", headers={"Mcp-Session-Id": sid})).status_code == 200
291 pending.cancel()
292
293
294 async def test_a_signed_get_is_bound_like_any_request(http: AsyncClient, _callers: None) -> None:
295 from musehub.api.routes.mcp import mcp_get
296
297 _as(ALICE)
298 sid = await _initialize(http)
299 _another_worker()
300 assert isinstance(await mcp_get(_get_request(sid), ALICE), StreamingResponse)
301 assert (await mcp_get(_get_request(sid), BOB)).status_code == 404
302
303
304 # ── Elicitation replies ───────────────────────────────────────────────────────
305
306
307 async def test_an_elicitation_reply_resolves_or_cancels_the_pending_future(http: AsyncClient) -> None:
308 sid = await _initialize(http)
309 sess = mcp_session._SESSIONS[sid]
310 accepted = mcp_session.create_pending_elicitation(sess, "e-ok")
311 errored = mcp_session.create_pending_elicitation(sess, "e-err")
312 assert await _post(http, sid, {"jsonrpc": "2.0", "id": "e-ok", "result": {"action": "accept", "content": {}}}) == 202
313 assert accepted.result() == {"action": "accept", "content": {}}
314 assert await _post(http, sid, {"jsonrpc": "2.0", "id": "e-err", "error": {"code": -1, "message": "no"}}) == 202
315 assert errored.cancelled()
316
317
318 async def test_an_elicitation_reply_no_pending_request_matches_is_a_400(http: AsyncClient) -> None:
319 sid = await _initialize(http)
320 r = await http.post("/mcp", json={"jsonrpc": "2.0", "id": "e-gone", "result": {"action": "accept"}},
321 headers={"Mcp-Session-Id": sid})
322 assert r.status_code == 400
323 body = r.json()
324 assert body["id"] is None and body["error"]["code"] == -32600
325 assert body["error"]["data"]["error_code"] == "no_pending_request"
326
327
328 # ── Focus: an unsaved local change is not overwritten by a concurrent lookup ─
329
330
331 async def test_a_concurrent_lookup_keeps_an_unsaved_focus(http: AsyncClient, db_session: AsyncSession) -> None:
332 sid = await _initialize(http)
333 sess = mcp_session._SESSIONS[sid]
334 sess.set_repo_focus("o", "s") # set by a tools/call that has not saved it yet
335 assert await mcp_session.resolve_session(sid, mcp_session.ANONYMOUS) is sess # a concurrent request's lookup
336 assert sess.repo_focus == ("o", "s")
337 await mcp_session.save_focus(sess)
338 row = await _row(db_session, sid)
339 assert row is not None and (row["focus_owner"], row["focus_slug"]) == ("o", "s") and not sess.focus_dirty
File History 3 commits
sha256:e6c07a0f17d2b30516cfe406c094c493c3aeaf8ea72db9ca098d24013bcc3bb9 Merge branch 'task/wave-hub-mcp-289' into dev Opus 5.5 minor ⚠ 1 day ago
sha256:08c30c873257205e342a68f5d55803ff3c6dd6abd4747b5444c68e1da4b8da39 fix(mcp): bind sessions to their principal; honest cross-wo… Opus 5.5 minor ⚠ 1 day ago
sha256:5e59815d34816a1aa12717d0ac82362d6a9919e8b6e096e66ed49c47a028d76f fix(mcp): share Streamable HTTP sessions across worker proc… Opus 5.5 minor ⚠ 1 day ago