|
| 1 | +# Cloud environment setup — why the "ci" MCP server needs a Setup Script |
| 2 | + |
| 3 | +This repo dogfoods its own MCP server: `.mcp.json` wires up a `ci` stdio |
| 4 | +server pointed at this workspace's own `ci-cli` binary. On Claude Code on |
| 5 | +the web, that binary does not exist until something compiles it — and |
| 6 | +where that compile happens determines whether the server ever connects. |
| 7 | + |
| 8 | +## The failure this document exists to prevent |
| 9 | + |
| 10 | +`ci-cli` is a Rust binary with a nontrivial dependency tree (tree-sitter |
| 11 | +grammars, stack-graphs, bundled SQLite). A cold `cargo build -p ci-cli` on |
| 12 | +a fresh checkout measures **~59s** in this environment. Claude Code's MCP |
| 13 | +client dials configured servers **concurrently with, not after,** |
| 14 | +`SessionStart` hooks — confirmed against the official docs: |
| 15 | + |
| 16 | +- [code.claude.com/docs/en/mcp](https://code.claude.com/docs/en/mcp) — |
| 17 | + MCP server startup is "non-blocking by default"; a failed initial |
| 18 | + connection gets at most 3 retries on transient errors (~7s of total |
| 19 | + backoff, as of v2.1.121), then the server is marked failed **for the |
| 20 | + rest of the session**, with no further retry. |
| 21 | +- [code.claude.com/docs/en/claude-code-on-the-web](https://code.claude.com/docs/en/claude-code-on-the-web) — |
| 22 | + `SessionStart` hooks "run after Claude Code launches, on every session," |
| 23 | + which is a different (and unordered, relative to MCP dialing) point in |
| 24 | + startup than a Setup Script, which runs "before Claude Code launches." |
| 25 | + |
| 26 | +A `SessionStart` hook that runs `cargo build` (this repo has one — |
| 27 | +`.claude/hooks/session-start-build-ci.sh`) can therefore **win** the race |
| 28 | +against a cold connection attempt, but cannot **guarantee** winning it — a |
| 29 | +59s build has no trouble outlasting a ~7s retry budget. A previous version |
| 30 | +of this repo's setup relied on that hook alone and believed it had fixed |
| 31 | +the problem; it hadn't — it had just usually won the race, until a session |
| 32 | +where it didn't. |
| 33 | + |
| 34 | +## The actual fix: a Cloud environment Setup Script |
| 35 | + |
| 36 | +Setup Scripts and `SessionStart` hooks look similar but solve different |
| 37 | +problems: |
| 38 | + |
| 39 | +| | Setup Script | `SessionStart` hook | |
| 40 | +|---|---|---| |
| 41 | +| Runs | Once, **before** Claude Code (and MCP dialing) launches at all | Every session, **after** Claude Code launches, concurrently with MCP dialing | |
| 42 | +| Configured in | Cloud environment settings UI (not in this repo) | `.claude/settings.json` (this repo, `.claude/hooks/session-start-build-ci.sh`) | |
| 43 | +| Output persistence | Filesystem snapshotted and reused for ~7 days, or until the script/network config changes | None — runs fresh every session | |
| 44 | + |
| 45 | +Only the Setup Script runs early enough to structurally rule out the race. |
| 46 | +Because it's environment-level config, it is **not stored in this repo** — |
| 47 | +that's exactly why it needs to be written down here, or the next person to |
| 48 | +hit this failure has no way to discover it. |
| 49 | + |
| 50 | +### What to paste in |
| 51 | + |
| 52 | +Open the environment settings dialog (cloud icon → environment selector → |
| 53 | +settings icon) for the environment used to run sessions against this repo, |
| 54 | +and put this in the **Setup script** field: |
| 55 | + |
| 56 | +```bash |
| 57 | +#!/bin/bash |
| 58 | +cd "$(dirname "$0")" 2>/dev/null || true |
| 59 | +cargo build --quiet -p ci-cli |
| 60 | +``` |
| 61 | + |
| 62 | +(Setup scripts run with the repo checked out as the working directory, so |
| 63 | +a plain `cargo build --quiet -p ci-cli` without the `cd` is equally fine — |
| 64 | +the `cd` guard above is only there in case that assumption ever changes.) |
| 65 | + |
| 66 | +This must build to the **same path** `.mcp.json` expects: |
| 67 | +`target/debug/ci` (debug, not `--release` — release compiles slower for no |
| 68 | +benefit here, since this binary is a local dev/dogfood tool, not a |
| 69 | +distributed artifact; keep this in sync with |
| 70 | +`.claude/hooks/session-start-build-ci.sh` and |
| 71 | +`.claude/hooks/ci-mcp-entrypoint.sh` if that ever changes). |
| 72 | + |
| 73 | +Keep it under Claude Code's ~5-minute Setup Script budget — 59s measured |
| 74 | +leaves a wide margin. |
| 75 | + |
| 76 | +### Optional extra margin: `MCP_TIMEOUT` |
| 77 | + |
| 78 | +`MCP_TIMEOUT` (milliseconds) controls the MCP client's own initial |
| 79 | +connection timeout. It is a process-level environment variable for the |
| 80 | +`claude` process itself (e.g. `MCP_TIMEOUT=10000 claude` locally) — **not** |
| 81 | +a per-server `.mcp.json` field, and **not** the same as `.mcp.json`'s |
| 82 | +per-server `timeout` field (that one bounds individual tool *calls* after |
| 83 | +connection, not the initial handshake). For cloud sessions, the |
| 84 | +equivalent lever is adding `MCP_TIMEOUT=120000` as an **environment |
| 85 | +variable** in the same environment settings dialog as the Setup Script. |
| 86 | +This is optional defense-in-depth (matters mainly for the very first |
| 87 | +session before any cache exists, or right after the ~7-day cache expiry) |
| 88 | +— the Setup Script is what actually fixes the steady state. |
| 89 | + |
| 90 | +## What's already handled in this repo (defense in depth, not a substitute) |
| 91 | + |
| 92 | +- `.claude/hooks/ci-mcp-entrypoint.sh` — `.mcp.json`'s actual entrypoint |
| 93 | + now. Execs the pre-built binary directly if present (no `cargo` |
| 94 | + involved, no risk of an unexpected rebuild reopening this exact race); |
| 95 | + only builds inline if the binary is missing outright. |
| 96 | +- `.claude/hooks/session-start-build-ci.sh` — still runs every session. |
| 97 | + Redundant with the Setup Script in the common case (no-op if `target/` |
| 98 | + is already warm), but it's what keeps the binary from going *stale* |
| 99 | + (e.g. after editing `ci`'s own source), and it's the only mechanism at |
| 100 | + all for local/non-cloud Claude Code, which has no Setup Script concept. |
| 101 | + |
| 102 | +None of this replaces the Setup Script for cloud sessions — it narrows the |
| 103 | +window and covers the cases the Setup Script can't (local dev, staleness), |
| 104 | +but only the Setup Script removes the compile step from the connection |
| 105 | +race entirely. |
| 106 | + |
| 107 | +## How to verify it worked |
| 108 | + |
| 109 | +At the start of a session, once indexing has had a moment to run: |
| 110 | + |
| 111 | +``` |
| 112 | +mcp__ci__repo_overview() |
| 113 | +``` |
| 114 | + |
| 115 | +If this resolves (rather than the tool being entirely absent from your |
| 116 | +tool list), the connection succeeded. If it's missing, check for a `ci-cli |
| 117 | +pre-build failed` message in the session's `SessionStart` hook output — |
| 118 | +that means the fallback hook itself failed (e.g. a real compile error), |
| 119 | +which is a different problem than the timing race this document covers. |
0 commit comments