diff --git a/main.py b/main.py index 4445914..c9f1d7a 100644 --- a/main.py +++ b/main.py @@ -213,6 +213,9 @@ async def health(request): @asynccontextmanager async def lifespan(app_instance): + global session_manager + if getattr(session_manager, "_has_started", False): + session_manager = StreamableHTTPSessionManager(mcp._mcp_server, stateless=True, json_response=True) async with session_manager.run(): yield diff --git a/tests/test_server.py b/tests/test_server.py index 683cb4f..9062783 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -28,66 +28,68 @@ EXPECTED_TOOLS = { "idee_etiqueter", } -def test_health(): - with TestClient(app) as client: - r = client.get("/health") - assert r.status_code == 200 - data = r.json() - assert data.get("status") == "ok" - assert data.get("service") == "taf-mcp" - assert data.get("tools") == 6 +@pytest.fixture(scope="module") +def client(): + with TestClient(app) as c: + yield c -def test_auth_rejection(): - with TestClient(app) as client: - # Sans header auth - r1 = client.post("/mcp", headers={"Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"}) - assert r1.status_code == 401 - - # Mauvais token - r2 = client.post("/mcp", headers={"Authorization": "Bearer faux-jeton", "Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"}) - assert r2.status_code == 401 +def test_health(client): + r = client.get("/health") + assert r.status_code == 200 + data = r.json() + assert data.get("status") == "ok" + assert data.get("service") == "taf-mcp" + assert data.get("tools") == 6 -def test_mcp_session_and_tools_list(): - with TestClient(app) as client: - # Initialize - r_init = client.post( - "/mcp", - headers=AUTH_HEADERS, - json={ - "jsonrpc": "2.0", - "id": 1, - "method": "initialize", - "params": { - "protocolVersion": "2025-03-26", - "capabilities": {}, - "clientInfo": {"name": "test-client", "version": "1.0"} - } +def test_auth_rejection(client): + # Sans header auth + r1 = client.post("/mcp", headers={"Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"}) + assert r1.status_code == 401 + + # Mauvais token + r2 = client.post("/mcp", headers={"Authorization": "Bearer faux-jeton", "Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"}) + assert r2.status_code == 401 + +def test_mcp_session_and_tools_list(client): + # Initialize + r_init = client.post( + "/mcp", + headers=AUTH_HEADERS, + json={ + "jsonrpc": "2.0", + "id": 1, + "method": "initialize", + "params": { + "protocolVersion": "2025-03-26", + "capabilities": {}, + "clientInfo": {"name": "test-client", "version": "1.0"} } - ) - assert r_init.status_code == 200 - sid = r_init.headers.get("mcp-session-id") - assert sid is not None and len(sid) > 0 + } + ) + assert r_init.status_code == 200 + sid = r_init.headers.get("mcp-session-id") + assert sid is not None and len(sid) > 0 - # Initialized notification - headers_sid = {**AUTH_HEADERS, "mcp-session-id": sid} - r_notif = client.post( - "/mcp", - headers=headers_sid, - json={"jsonrpc": "2.0", "method": "notifications/initialized"} - ) - assert r_notif.status_code in (200, 202) + # Initialized notification + headers_sid = {**AUTH_HEADERS, "mcp-session-id": sid} + r_notif = client.post( + "/mcp", + headers=headers_sid, + json={"jsonrpc": "2.0", "method": "notifications/initialized"} + ) + assert r_notif.status_code in (200, 202) - # Tools list - r_tools = client.post( - "/mcp", - headers=headers_sid, - json={"jsonrpc": "2.0", "id": 2, "method": "tools/list"} - ) - assert r_tools.status_code == 200 - data = r_tools.json() - tools = data.get("result", {}).get("tools", []) - tool_names = {t["name"] for t in tools} - assert tool_names == EXPECTED_TOOLS + # Tools list + r_tools = client.post( + "/mcp", + headers=headers_sid, + json={"jsonrpc": "2.0", "id": 2, "method": "tools/list"} + ) + assert r_tools.status_code == 200 + data = r_tools.json() + tools = data.get("result", {}).get("tools", []) + tool_names = {t["name"] for t in tools} + assert tool_names == EXPECTED_TOOLS def test_rule_a_text_intouchable(): from main import idee_recadrer, idee_statut, idee_etiqueter @@ -106,7 +108,6 @@ def test_rule_a_text_intouchable(): def test_rule_b_taches_refused(monkeypatch): import main - # Mock get_all_tasks renvoyant une tâche de l'espace 'taches' monkeypatch.setattr(main, "get_all_tasks", lambda: [ {"id": "task-taches-1", "text": "Faire les courses", "space": "taches", "deleted": False} ])