|
| 1 | +"""MCP session lifecycle — end-to-end over a real Dash server. |
| 2 | +
|
| 3 | +Exercises the full MCP session flow (initialize → operate → hot-reload |
| 4 | +recovery) against a live ``dash_duo`` server using real HTTP requests. |
| 5 | +Unit-level checks (status codes, header mechanics) live in |
| 6 | +``tests/unit/mcp/test_mcp_session.py``; these tests verify the broader |
| 7 | +behavioral contract. |
| 8 | +""" |
| 9 | + |
| 10 | +import requests |
| 11 | + |
| 12 | +from dash import Dash, Input, Output, html |
| 13 | + |
| 14 | +from tests.integration.mcp.conftest import _mcp_post |
| 15 | + |
| 16 | + |
| 17 | +def _mcp_post_with_session( |
| 18 | + server_url, method, params=None, request_id=1, session_id=None |
| 19 | +): |
| 20 | + """Like ``_mcp_post`` but forwards an ``Mcp-Session-Id`` header.""" |
| 21 | + headers = {"Content-Type": "application/json"} |
| 22 | + if session_id is not None: |
| 23 | + headers["Mcp-Session-Id"] = session_id |
| 24 | + return requests.post( |
| 25 | + f"{server_url}/_mcp", |
| 26 | + json={ |
| 27 | + "jsonrpc": "2.0", |
| 28 | + "method": method, |
| 29 | + "id": request_id, |
| 30 | + "params": params or {}, |
| 31 | + }, |
| 32 | + headers=headers, |
| 33 | + timeout=5, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def test_mcpse_e2e001_full_session_lifecycle(dash_duo): |
| 38 | + """Initialize → tools/list → tools/call with session headers throughout.""" |
| 39 | + app = Dash(__name__) |
| 40 | + app.layout = html.Div([html.Div(id="inp"), html.Div(id="out")]) |
| 41 | + |
| 42 | + @app.callback(Output("out", "children"), Input("inp", "children")) |
| 43 | + def echo(v): |
| 44 | + return f"echo: {v}" |
| 45 | + |
| 46 | + dash_duo.start_server(app) |
| 47 | + url = dash_duo.server.url |
| 48 | + |
| 49 | + init = _mcp_post_with_session(url, "initialize") |
| 50 | + assert init.status_code == 200 |
| 51 | + sid = init.headers.get("Mcp-Session-Id") |
| 52 | + assert sid |
| 53 | + |
| 54 | + notif = _mcp_post_with_session( |
| 55 | + url, "notifications/initialized", session_id=sid, request_id=None |
| 56 | + ) |
| 57 | + assert notif.status_code == 202 |
| 58 | + |
| 59 | + tools_resp = _mcp_post_with_session(url, "tools/list", session_id=sid, request_id=2) |
| 60 | + assert tools_resp.status_code == 200 |
| 61 | + tools = tools_resp.json()["result"]["tools"] |
| 62 | + assert any("echo" in t["name"] for t in tools) |
| 63 | + |
| 64 | + tool_name = next(t["name"] for t in tools if "echo" in t["name"]) |
| 65 | + call_resp = _mcp_post_with_session( |
| 66 | + url, |
| 67 | + "tools/call", |
| 68 | + params={"name": tool_name, "arguments": {"v": "hello"}}, |
| 69 | + session_id=sid, |
| 70 | + request_id=3, |
| 71 | + ) |
| 72 | + assert call_resp.status_code == 200 |
| 73 | + assert call_resp.headers.get("Mcp-Session-Id") == sid |
| 74 | + |
| 75 | + |
| 76 | +def test_mcpse_e2e002_stale_session_recovers_with_notifications(dash_duo): |
| 77 | + """Simulate a hot-reload hash change and verify transparent recovery.""" |
| 78 | + app = Dash(__name__) |
| 79 | + app.layout = html.Div([html.Div(id="inp"), html.Div(id="out")]) |
| 80 | + |
| 81 | + @app.callback(Output("out", "children"), Input("inp", "children")) |
| 82 | + def echo(v): |
| 83 | + return f"echo: {v}" |
| 84 | + |
| 85 | + dash_duo.start_server(app) |
| 86 | + url = dash_duo.server.url |
| 87 | + |
| 88 | + app._hot_reload.hash = "original_hash" |
| 89 | + |
| 90 | + init = _mcp_post_with_session(url, "initialize") |
| 91 | + sid = init.headers["Mcp-Session-Id"] |
| 92 | + assert sid == "original_hash" |
| 93 | + |
| 94 | + resp = _mcp_post_with_session(url, "tools/list", session_id=sid, request_id=2) |
| 95 | + assert resp.status_code == 200 |
| 96 | + |
| 97 | + app._hot_reload.hash = "new_hash" |
| 98 | + |
| 99 | + resp = _mcp_post_with_session(url, "tools/list", session_id=sid, request_id=3) |
| 100 | + assert resp.status_code == 200 |
| 101 | + new_sid = resp.headers["Mcp-Session-Id"] |
| 102 | + assert new_sid == "new_hash" |
| 103 | + |
| 104 | + data = resp.json() |
| 105 | + assert isinstance(data, list) |
| 106 | + assert len(data) == 3 |
| 107 | + assert data[0]["method"] == "notifications/tools/list_changed" |
| 108 | + assert data[1]["method"] == "notifications/resources/list_changed" |
| 109 | + assert "result" in data[2] |
| 110 | + assert "tools" in data[2]["result"] |
| 111 | + |
| 112 | + resp = _mcp_post_with_session(url, "tools/list", session_id=new_sid, request_id=4) |
| 113 | + assert resp.status_code == 200 |
| 114 | + data = resp.json() |
| 115 | + assert isinstance(data, dict) |
| 116 | + assert "result" in data |
| 117 | + |
| 118 | + |
| 119 | +def test_mcpse_e2e003_capabilities_advertise_list_changed(dash_duo): |
| 120 | + """Server capabilities include listChanged for tools and resources.""" |
| 121 | + app = Dash(__name__) |
| 122 | + app.layout = html.Div(id="root") |
| 123 | + dash_duo.start_server(app) |
| 124 | + |
| 125 | + resp = _mcp_post(dash_duo.server.url, "initialize") |
| 126 | + caps = resp.json()["result"]["capabilities"] |
| 127 | + assert caps["tools"]["listChanged"] is True |
| 128 | + assert caps["resources"]["listChanged"] is True |
0 commit comments