Sprint 291: Add HTTP/SSE daemon mode to whetstone_mcp (Steps 1983-1987)

Eliminates the dual-process bug where multiple Claude Code sessions each
spawn their own whetstone_mcp stdio process with split in-memory state.
All sessions now share a single persistent daemon via MCP HTTP+SSE transport.

Changes:
- MCPHttpServer.h: HTTP+SSE transport (cpp-httplib via FetchContent).
  GET /sse streams events per session, POST /message routes JSON-RPC,
  GET /health for status. Thread-safe session map with mutex/cv/queue.
- MCPBridge.h: adds runHttp(port, workspace) method
- mcp_main.cpp: --daemon/--port flags, daemon.pid lockfile, duplicate guard
- CMakeLists.txt: FetchContent for cpp-httplib, step1983-1987 targets
- tools/start-whetstone-daemon.sh: idempotent startup, health-check poll
- 5/5 tests passing (step1987_test)

Note: Documents/.mcp.json switched to SSE transport in working directory
(not tracked in this repo). Daemon must be started before Claude Code sessions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Bill
2026-03-09 11:18:11 -06:00
parent a09fc55325
commit af6fa169b7
8 changed files with 603 additions and 7 deletions

80
tools/start-whetstone-daemon.sh Executable file
View File

@@ -0,0 +1,80 @@
#!/usr/bin/env bash
# start-whetstone-daemon.sh
#
# Ensures a single whetstone_mcp HTTP+SSE daemon is running on port 7600.
# Safe to call multiple times — exits immediately if already running.
#
# Usage:
# ./tools/start-whetstone-daemon.sh
# ./tools/start-whetstone-daemon.sh --port 7601
# ./tools/start-whetstone-daemon.sh --language python
#
# Output: prints daemon PID on stdout.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WHETSTONE_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
BINARY="$WHETSTONE_DIR/editor/build-native/whetstone_mcp"
WORKSPACE="$WHETSTONE_DIR"
PORT=7600
LANGUAGE="cpp"
LOG="$WHETSTONE_DIR/.whetstone/daemon.log"
PID_FILE="$WHETSTONE_DIR/.whetstone/daemon.pid"
# Parse optional overrides
while [[ $# -gt 0 ]]; do
case "$1" in
--port) PORT="$2"; shift 2 ;;
--language) LANGUAGE="$2"; shift 2 ;;
*) echo "Unknown arg: $1" >&2; exit 1 ;;
esac
done
# Check if daemon is already running
if [[ -f "$PID_FILE" ]]; then
EXISTING_PID=$(cat "$PID_FILE" 2>/dev/null || echo "")
if [[ -n "$EXISTING_PID" ]] && kill -0 "$EXISTING_PID" 2>/dev/null; then
echo "[whetstone-daemon] Already running (PID $EXISTING_PID)" >&2
echo "$EXISTING_PID"
exit 0
fi
fi
# Also check by port
if lsof -ti :"$PORT" >/dev/null 2>&1; then
echo "[whetstone-daemon] Port $PORT already in use — daemon may be running" >&2
lsof -ti :"$PORT"
exit 0
fi
if [[ ! -x "$BINARY" ]]; then
echo "[whetstone-daemon] Binary not found: $BINARY" >&2
echo "[whetstone-daemon] Build it with: cmake --build editor/build-native --target whetstone_mcp --parallel" >&2
exit 1
fi
mkdir -p "$WHETSTONE_DIR/.whetstone"
# Start daemon in background
nohup "$BINARY" \
--daemon \
--port "$PORT" \
--workspace "$WORKSPACE" \
--language "$LANGUAGE" \
> "$LOG" 2>&1 &
DAEMON_PID=$!
# Wait briefly for daemon to write its PID file and start listening
for i in $(seq 1 20); do
sleep 0.25
if curl -sf "http://127.0.0.1:$PORT/health" >/dev/null 2>&1; then
echo "[whetstone-daemon] Started (PID $DAEMON_PID, port $PORT)" >&2
echo "$DAEMON_PID"
exit 0
fi
done
echo "[whetstone-daemon] WARNING: daemon started (PID $DAEMON_PID) but /health not yet responding" >&2
echo "$DAEMON_PID"