# Hooks and MCP servers

Three features have "hook" or "MCP" in their name. They do different jobs and are set up in different places.

| Feature | Set up with | What it does |
| --- | --- | --- |
| [Session hooks](https://fleetrc.loadsheet.dev/guide/hooks-and-mcp#session-hooks) | `fleetrc hook install` | Start a sync when a Claude Code or Codex session opens. |
| [MCP servers](https://fleetrc.loadsheet.dev/guide/hooks-and-mcp#mcp-servers) | `sources[].mcp` | Write MCP servers into each agent's config. |
| [Hooks in the manifest](https://fleetrc.loadsheet.dev/guide/hooks-and-mcp#hooks-in-the-manifest) | `sources[].agentHooks` and `hooks` | Sync hook files into agents, or run your commands after a sync. |

## Session hooks

`init` adds a `SessionStart` hook to Claude Code and Codex. When a session opens, the hook starts a background sync. Nothing else in the manifest controls it.

```sh
fleetrc hook install            # each agent whose folder exists
fleetrc hook install codex      # one agent, creating its folder if needed
fleetrc hook remove
```

| Agent | File |
| --- | --- |
| `claude-code` | `settings.json` in `~/.claude`, or in `CLAUDE_CONFIG_DIR` |
| `codex` | `hooks.json` in `~/.codex` |

Both commands keep your other hooks and fields, take `--dry-run` and `--json`, and exit `1` when a file does not parse. `init --no-hooks` skips the install, and so does `fleet init --no-hooks` on each host.

The hook runs `fleetrc hook run <agent>`. It reads the event from stdin, ignores anything but `SessionStart`, and starts `fleetrc sync` in the background only when:

- no sync was attempted in the last 15 minutes, and
- no sync is running.

The 15 minutes count from the last attempt, not the last success, so a sync that fails offline waits for the next window. The hook makes no network call, prints nothing and always exits `0`, so it never blocks a session. The sync it starts logs to `fleetrc-sync.log` in the state folder's `logs` directory and runs with the `PATH` saved when you ran `hook install`, not the agent's.

fleetrc installs no prompt hook. An old `UserPromptSubmit` entry (`fleetrc hook prompt`) starts no sync. In Claude Code it only prints a line naming skills that arrived after the session began. `hook install` replaces these entries with the session hook, and [`doctor`](https://fleetrc.loadsheet.dev/reference/doctor) warns about them.

> [!NOTE]
> Codex runs a new hook only after you approve it. Open `/hooks` in Codex once per machine and approve fleetrc's hook. Codex records approval with a hash of each hook, so fleetrc rewrites its own entry in place and leaves a no-op where it removes one that other hooks follow.

On a machine with no agent, such as a server, use the [timer](https://fleetrc.loadsheet.dev/guide/timer) instead.

## MCP servers

`sources[].mcp` is the path of a file in the source, in Claude Code's format. fleetrc writes each server into every agent's own config and removes it when you drop it from the file.

```json [mcp.json]
{
  "mcpServers": {
    "docs": {
      "command": "npx",
      "args": ["-y", "docs-mcp"],
      "env": { "DOCS_TOKEN": "${DOCS_TOKEN}" }
    },
    "search": {
      "type": "http",
      "url": "https://mcp.example.com",
      "headers": { "Authorization": "Bearer ${SEARCH_TOKEN}" }
    }
  }
}
```

```json [.fleetrc/manifest.json]
{
  "sources": [{ "id": "personal", "git": "me/agent-config", "mcp": "mcp.json" }]
}
```

A server is local (`command`, `args`, `env`, `cwd`) or remote (`url`, `headers`, `type` of `http` or `sse`). Add `"agents": ["claude-code", "cursor"]` to send it to fewer agents. The manifest's `agents` decides which agents get servers at all, from Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Grok and OpenCode.

Write secrets as `${VAR}` and set `VAR` where each agent starts. fleetrc translates the reference into each agent's syntax, such as `${env:VAR}` for Cursor and `{env:VAR}` for OpenCode. A literal value in a header, or in an `env` key that contains `TOKEN`, `KEY`, `SECRET`, `PASSWORD` or `AUTH`, fails that server and syncs the others.

An agent that cannot take a server as written skips it with the reason. Codex reads a variable only as a whole value, so `"Authorization": "Bearer ${SEARCH_TOKEN}"` works there and `"--token=${TOKEN}"` does not.

If you edit a synced entry by hand, the next sync holds it as it is and says so. Compare it with the manifest's version:

```sh
fleetrc diff mcp/claude-code/docs
```

Delete the held entry in the agent's config to take the manifest's. A server of the same name that fleetrc did not write is left alone and reported as foreign.

## Hooks in the manifest

### Agent hook files

`sources[].agentHooks` syncs hook definitions into agents, in each agent's own format. A string path is a Claude Code file. An object gives a path per agent: `claude-code`, `codex`, `grok`, `gemini-cli` or `cursor`.

```json [.fleetrc/manifest.json]
{
  "sources": [
    {
      "id": "personal",
      "git": "me/agent-config",
      "agentHooks": { "claude-code": "hooks/claude.json", "codex": "hooks/codex.json" }
    }
  ]
}
```

fleetrc translates nothing here. Claude Code's hooks go into a plugin at `~/.claude/skills/fleetrc`, so a skill named `fleetrc` is refused. Codex's go into `hooks.json`, Grok's into `~/.grok/hooks`, Gemini CLI's into `settings.json` and Cursor's into `hooks.json`, each beside your own. As with MCP, a hand-edited entry is held. In Codex, a new or changed group waits for approval in `/hooks`, and sync says so with a `review:` line.

### Commands after a sync

The root manifest's `hooks` run shell commands after a sync that changed matching files.

```json [.fleetrc/manifest.json]
{
  "hooks": [
    { "name": "pstack", "run": "node build.mjs", "on": ["personal:pstack/**"] }
  ]
}
```

| Field | Meaning |
| --- | --- |
| `run` | The shell command. Required. |
| `name` | Names the hook in the report. Defaults to `run`. |
| `on` | `<source id>:<glob>` patterns, matched against paths from the source's root. Default: any change. |

The command gets the sync report as JSON on stdin and two variables: `FLEETRC_HOOK`, the hook's name, and `FLEETRC_CHANGED`, the matched `<source>:<file>` lines, one per line. Output is kept in the report. A command that exits nonzero fails the sync, and one still running after 5 minutes is stopped.

## Root manifest only

`mcp`, `agentHooks`, `hooks`, `fleet`, `hub` and `report` run commands or name machines. A manifest you [extend](https://fleetrc.loadsheet.dev/guide/manifest) loses them, with a warning, so a team repository cannot run code on your machine.

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://fleetrc.loadsheet.dev/raw/guide.md)
- [Installation](https://fleetrc.loadsheet.dev/raw/guide/installation.md)
- [The manifest](https://fleetrc.loadsheet.dev/raw/guide/manifest.md)
- [Syncing](https://fleetrc.loadsheet.dev/raw/guide/sync.md)
- [Hooks and MCP servers](https://fleetrc.loadsheet.dev/raw/guide/hooks-and-mcp.md) (this page)
- [Fleet](https://fleetrc.loadsheet.dev/raw/guide/fleet.md)
- [Timer](https://fleetrc.loadsheet.dev/raw/guide/timer.md)
- [Agents](https://fleetrc.loadsheet.dev/raw/guide/agents.md)

Next: [Fleet](https://fleetrc.loadsheet.dev/raw/guide/fleet.md)

All pages: [llms.txt](https://fleetrc.loadsheet.dev/llms.txt)

Something on this page does not work as written? Tell the user, and draft an issue for them to post: [issue tracker](https://github.com/words-with-agents/fleetrc/issues). Report a security vulnerability privately, never in a public issue.
