Files
coder/cli
Jeremy Ruppel 281b838856 feat: retry failed MCP servers on context resync (#30489)
Stacked on #30484.

The agent only reconnects workspace MCP servers when `.mcp.json`
changes, so a server that is down when the agent first connects (for
example, a dev server like Storybook started after the workspace is up)
stays unavailable until the config file is touched.

`agentcontext.Manager.Resync` now calls a new
`agentmcp.Manager.Reconnect` before resolving. `Reconnect` re-runs the
last MCP reload even when the config is unchanged, so servers that
failed to connect are retried; connected servers keep their session
because their config is unchanged. This makes `coder exp chat context
refresh` (no argument) and the agent's `POST /api/v0/context/resync`
retry failed servers. Resync is only called on explicit user request
today, so no background traffic is added.

Not covered here: a server that restarts after it connected keeps its
stale session, since its config is unchanged.

<details>
<summary>Plan and decisions</summary>

**Problem**

1. **Never connected.** A server that is down at the first reload is
listed as `failed to connect` until the config file changes, because
`startReloadIfNeeded` short-circuits when `SnapshotChanged` is false.
2. **Connected, then dead.** A server that restarts after connecting
keeps its stale `ClientSession`; `classifyServers` keeps it because its
config is unchanged. Out of scope for this PR.

**Current wiring**

- Reload triggers: once after startup scripts (`agent/agent.go`) and the
`.mcp.json` fsnotify watcher.
- After a reload changes the catalog, the agent pushes context and
coderd live-syncs MCP rows into every bound chat, so new tools reach
chats on their next turn without a chat refresh.
- `coder exp chat context refresh` (no argument) calls the agent socket
`ResyncContext`, which runs `agentcontext.Manager.Resync`. Before this
PR, Resync re-read the MCP catalog but did not reconnect.
- The UI "Refresh context" button calls `chatd.RefreshChatContext`,
which is DB-only (re-pin) and never reaches the agent.

**Options considered**

| Trigger | Latency | Cost | Decision |
| --- | --- | --- | --- |
| A. Periodic agent ticker while a server is unhealthy | Up to one
interval | Periodic dials to dead servers | Rejected |
| B. On-demand via agent Resync (CLI, agent HTTP route) | Immediate |
None unless invoked | This PR |
| C. On-demand via UI "Refresh context", running the same agent resync
as the CLI | Immediate | None unless clicked | Follow-up PR |
| D. Per-turn nudge from chatd | Next turn | One agent request per turn,
fleet-wide | Rejected |

**Implementation choices**

- `Reconnect` clears the stored config file snapshot and calls `Reload`,
rather than adding a force flag to the reload path. `SnapshotChanged`
then reports a change, the singleflight reload runs, and
`installServers` writes a fresh snapshot. False-positive reloads were
already documented as safe.
- No backoff or retry state.

**Follow-up (C)**

Make the existing "Refresh context" button run the same agent resync as
the CLI (coderd calls the agent's `/api/v0/context/resync`, then
re-pins), and render it when MCP server issues exist.

</details>

---

Generated by Coder Agents on behalf of @jeremyruppel.
2026-10-21 06:28:45 -07:00
..
…