fix(mcp,start): JSON-RPC notifications return 202; no tmux attach

- mcp_post: notifications (no `id` field) → HTTP 202 with empty body,
  per streamable-HTTP MCP transport spec. Fixes rmcp 'transport channel
  closed' on codex's notifications/initialized.
- start.sh: don't attach to tmux; print URL only. Backend + tmux session
  run in background. UI is the only viewing surface.
- scripts/stop.sh: convenience teardown.
This commit is contained in:
ai-chess
2026-05-14 23:41:11 +03:00
parent e48f01dcb5
commit 4c5b306a9a
4 changed files with 54 additions and 8 deletions
+6 -5
View File
@@ -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 ) & ( sleep 12 && curl -fsS -X POST http://127.0.0.1:7833/api/dispatch-first >/dev/null ) &
echo echo
echo "[ai-chess] open: http://127.0.0.1:7833/" echo "[ai-chess] открой в браузере: http://127.0.0.1:7833/"
echo "[ai-chess] attach: tmux attach -t chess" echo "[ai-chess] первый промпт улетит белому через ~12с (после старта агентов)"
echo "[ai-chess] first prompt fires in ~12s (after agents boot)" echo "[ai-chess] tmux работает в фоне; если очень хочется подцепиться руками:"
echo echo " tmux attach -t chess"
tmux attach -t chess echo "[ai-chess] остановить: ./scripts/stop.sh (или вручную:"
echo " tmux kill-session -t chess; pkill -f 'python -m ai_chess')"
+4
View File
@@ -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
+19 -3
View File
@@ -5,7 +5,7 @@ import json
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import Any 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.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
@@ -220,8 +220,24 @@ def build_app(state: state_mod.AppState) -> FastAPI:
except ValueError: except ValueError:
raise HTTPException(404, "unknown side") raise HTTPException(404, "unknown side")
body = await request.json() body = await request.json()
if isinstance(body, list): # batch # JSON-RPC notification (no "id" field) → MUST get HTTP 202 with no body
return [await handle_mcp(s, item) for item in body if isinstance(item, dict)] # 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) return await handle_mcp(s, body)
# --------- static UI --------- # --------- static UI ---------
+25
View File
@@ -73,3 +73,28 @@ def test_mcp_wrong_side_blocked(client):
}) })
text = r.json()["result"]["content"][0]["text"] text = r.json()["result"]["content"][0]["text"]
assert "not_your_turn" in 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