Files
coder/docs/ai-coder
Nick Vigilante 77743afdb8 docs: rewrite extending-agents MCP and skills discovery for pushed context (#28576)
This PR rewrites the MCP and skills discovery sections of
`docs/ai-coder/agents/extending-agents.md` to match the current
pushed-context architecture.

The page documented a removed mechanism: per-chat-turn `.mcp.json` reads
with a 5-second discovery timeout, first-turn scans of `.agents/skills/`
only, and no mention of pinning.

What changed:

- Adds a "How the workspace shares context with chats" section: the
agent scans and pushes a context snapshot, chats pin one snapshot, a
push marks already-pinned chats out of date, and **Refresh context**
re-pins. Explains why an in-flight chat can keep an older snapshot.
- Documents the readiness gate: only an empty snapshot is published
until startup scripts finish.
- Documents the scan roots (working directory, `~/.coder`,
`~/.coder/skills`, `~/.claude/plugins/cache`, user-declared sources) and
that discovery is shallow.
- Documents all four skill container directories (`skills`,
`.agents/skills`, `.claude/skills`, `.codex/skills`) instead of only
`.agents/skills`.
- Adds snapshot limits and failure modes: 64 KiB per resource emitted as
oversize with no content, 2 MiB aggregate and 500 resource caps emitted
as excluded, invalid skill frontmatter.
- Replaces the 5-second discovery claim with the 30-second per-server
connect timeout and the fsnotify watcher with a 250 ms debounce; notes
that tool calls are proxied through the agent, so a pinned chat can list
tools it cannot execute while the workspace is unreachable.
- Keeps the values that are still correct: 60 s per tool call,
`SKILL.md` <= 64 KB, supporting files <= 512 KB truncated, personal
skills single file <= 64 KB and <= 100 per user, `read_skill_file` path
restrictions.

Linear: DOCS-723 https://linear.app/codercom/issue/DOCS-723

<details><summary>Analysis evidence</summary>

Every claim was verified against code on this branch's merge base.

**Push and pin model**

- `agent/agentcontext/push.go`: `RunPush` ships the current snapshot and
every subsequent snapshot on change; it skips version 0 (pre-ready).
- `coderd/agentapi/context.go`: `PushContextState` persists the snapshot
and calls `ContextDirtyMarker.HydrateAndMarkChatsDirty` inside the
transaction; the doc comment states it "hydrates chats for the agent
that have no pinned hash yet ... and flips already-pinned chats whose
hash differs".
- `coderd/x/chatd/context_hydration.go`: `HydrateAndMarkChatsDirty`
stamps NULL-hash chats via `HydrateAgentChatsContext` and dirties
differing chats via `MarkChatsContextDirtyByAgent`; "The pinned hash on
dirtied chats is intentionally left unchanged; the refresh endpoint
re-pins it." `RefreshChatContext` backs `PUT /chats/{chat}/context`
(route registered in `coderd/coderd.go`), and
`site/src/pages/AgentsPage/components/ChatPageContent.tsx` wires the
**Refresh context** button to it. This confirms the pinning semantics
claim: a chat mid-turn keeps its older snapshot until refreshed.
- `coderd/x/chatd/chatd.go` (`resolveWorkspaceMCPTools` /
`pinnedWorkspaceMCPTools`) builds the turn's MCP tools from
`chat_context_resources`, and `coderd/x/chatd/context_prompt.go` builds
the instruction block and skill index from the same pinned rows. No
per-turn live discovery remains.

**Readiness gate**

- `agent/agentcontext/manager.go`: `resolveAndBroadcast` returns early
while `!m.ready`; `agent/agentcontext/doc.go` describes the version-0
empty snapshot. `agent/agent.go` calls `a.contextManager.SetReady()`
after the startup-script lifecycle transition, then reloads MCP servers.
(The gate lives on `agentcontext.Manager`, not `agentmcp.Manager`.)

**Scan roots and shallow discovery**

- `agent/agentcontext/manager.go` `scanRootsLocked`: user sources, then
`defaultBuiltinRoots()`, then the working directory.
- `agent/agentcontext/defaults.go`: built-in roots are `~/.coder`,
`~/.coder/skills`, `~/.claude/plugins/cache`.
- `agent/agentcontext/doc.go` and `resolve.go`: instruction files and
`.mcp.json` are read only at a scan root's top level; the resolver never
descends or climbs.

**Skills**

- `agent/agentcontext/resolve.go` `skillContainerRelPaths`: `skills`,
`.agents/skills`, `.claude/skills`, `.codex/skills`;
`skillContainersFor` also treats a root named `skills` as a container,
which is what makes `~/.coder/skills` work.
- `readSkillMeta`: frontmatter name must match the directory basename
and match `workspacesdk.SkillNamePattern` (`^[a-z0-9]+(-[a-z0-9]+)*$`),
otherwise `StatusInvalid`; oversize files get `StatusOversize` with no
payload.
- `coderd/x/chatd/chattool/skill.go`: `maxSkillMetaBytes =
workspacesdk.MaxSkillMetaBytes` (64 KiB), `maxSkillFileBytes = 512 *
1024` with truncation, `validateSkillFilePath` rejects absolute paths,
`..`, and hidden components. `loadPinnedWorkspaceSkillContent` serves
the body from the pin; `bestEffortSkillFiles` needs a live connection.
- `coderd/x/skills/skills.go`: `MaxPersonalSkillSizeBytes` = 64 KiB,
`MaxPersonalSkillsPerUser` = 100.

**Caps**

- `agent/agentcontext/resolve.go`: `DefaultMaxResourceBytes` 64 KiB
(oversize, empty payload), `DefaultMaxSnapshotBytes` 2 MiB and
`DefaultMaxResources` 500 (excluded, payload cleared).
`coderd/x/chatd/context_prompt.go` `pinnedContextResources` surfaces
non-OK rows with status and error.

**MCP timeouts and watcher**

- `agent/x/agentmcp/manager.go`: `connectTimeout = 30 * time.Second`,
`toolCallTimeout = 60 * time.Second`. No 5-second discovery timeout
exists anywhere in `coderd/x/chatd` or `agent/x/agentmcp`.
- `agent/x/agentmcp/configwatcher.go`: `defaultWatchDebounce = 250 *
time.Millisecond`; the watcher fires on create, write, remove, and
rename. `agent/agentcontext/watcher.go` uses the same 250 ms window.
- `agent/agent.go`: `a.mcpManager.SetOnReload(a.contextManager.Trigger)`
re-resolves and re-pushes when the catalog changes; the shared engine
owns one connection set for discovery and execution.
- `coderd/x/chatd/chattool/mcpworkspace.go`: tool calls proxy back
through the agent connection, so execution requires a reachable
workspace.

**Validation**

- `pnpm install --frozen-lockfile`, `pnpm run format-docs` (no changes),
`pnpm run lint-docs` (0 errors), `vale` on the page (0 errors; 1
pre-existing `Coder.GerundHeading` warning on the untouched H1).

</details>

Generated by Coder Agents on behalf of @nickvigilante.


<details><summary>CI flake note</summary>

The CI run for commit a60db5adee concluded with `startup_failure`
(workflow never started; runner infrastructure), marking Flake Check,
Storybook, and gen as failed on a markdown-only change. Unrelated to the
changed paths; both runs re-triggered.

</details>
2026-08-26 21:32:42 +00:00
..