diff --git a/scripts/start.sh b/scripts/start.sh index d4e61b6..463c6c5 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -51,8 +51,9 @@ tmux split-window -v -p 15 -t chess:0 "tail -F $LOGS/backend.log" ( sleep 12 && curl -fsS -X POST http://127.0.0.1:7833/api/dispatch-first >/dev/null ) & echo -echo "[ai-chess] open: http://127.0.0.1:7833/" -echo "[ai-chess] attach: tmux attach -t chess" -echo "[ai-chess] first prompt fires in ~12s (after agents boot)" -echo -tmux attach -t chess +echo "[ai-chess] открой в браузере: http://127.0.0.1:7833/" +echo "[ai-chess] первый промпт улетит белому через ~12с (после старта агентов)" +echo "[ai-chess] tmux работает в фоне; если очень хочется подцепиться руками:" +echo " tmux attach -t chess" +echo "[ai-chess] остановить: ./scripts/stop.sh (или вручную:" +echo " tmux kill-session -t chess; pkill -f 'python -m ai_chess')" diff --git a/scripts/stop.sh b/scripts/stop.sh new file mode 100755 index 0000000..60df721 --- /dev/null +++ b/scripts/stop.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +# scripts/stop.sh — tear down the tmux session and backend +tmux kill-session -t chess 2>/dev/null && echo "[ai-chess] tmux session 'chess' killed" || true +pkill -f 'python -m ai_chess' 2>/dev/null && echo "[ai-chess] backend stopped" || true diff --git a/src/ai_chess/app.py b/src/ai_chess/app.py index a0240cc..c2296f1 100644 --- a/src/ai_chess/app.py +++ b/src/ai_chess/app.py @@ -5,7 +5,7 @@ import json from contextlib import asynccontextmanager from typing import Any -from fastapi import FastAPI, HTTPException, Request, WebSocket, WebSocketDisconnect +from fastapi import FastAPI, HTTPException, Request, Response, WebSocket, WebSocketDisconnect from fastapi.responses import FileResponse, JSONResponse from fastapi.staticfiles import StaticFiles @@ -220,8 +220,24 @@ def build_app(state: state_mod.AppState) -> FastAPI: except ValueError: raise HTTPException(404, "unknown side") body = await request.json() - if isinstance(body, list): # batch - return [await handle_mcp(s, item) for item in body if isinstance(item, dict)] + # JSON-RPC notification (no "id" field) → MUST get HTTP 202 with no body + # per streamable-HTTP MCP transport spec; otherwise rmcp (codex's client) + # treats the 200+body as an unexpected response and closes the channel. + if isinstance(body, list): + responses = [] + for item in body: + if not isinstance(item, dict): + continue + if "id" not in item: + await handle_mcp(s, item) + continue + responses.append(await handle_mcp(s, item)) + if not responses: + return Response(status_code=202) + return responses + if "id" not in body: + await handle_mcp(s, body) + return Response(status_code=202) return await handle_mcp(s, body) # --------- static UI --------- diff --git a/tests/test_app.py b/tests/test_app.py index 99e2089..815a2c2 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -73,3 +73,28 @@ def test_mcp_wrong_side_blocked(client): }) text = r.json()["result"]["content"][0]["text"] assert "not_your_turn" in text + + +def test_mcp_notification_returns_202_no_body(client): + # JSON-RPC notification (no "id" key) MUST return 202 with empty body — + # otherwise rmcp (codex's MCP client) treats 200+body as an unexpected + # response and closes the transport. + r = client.post("/mcp/white", json={ + "jsonrpc": "2.0", "method": "notifications/initialized", "params": {}, + }) + assert r.status_code == 202 + assert r.content == b"" + + +def test_mcp_batch_with_notification(client): + # Batch with one request + one notification — response should contain + # only the request's reply. + r = client.post("/mcp/white", json=[ + {"jsonrpc": "2.0", "id": 5, "method": "tools/list", "params": {}}, + {"jsonrpc": "2.0", "method": "notifications/initialized", "params": {}}, + ]) + assert r.status_code == 200 + body = r.json() + assert isinstance(body, list) + assert len(body) == 1 + assert body[0]["id"] == 5