# Extending zelk

> Where your tools, MCP servers, subagents and hooks plug into the loop, and what each is promised and what it gives up.

You extend zelk in four ways: a Go tool of your own, an MCP server, a
subagent, a hook. Each plugs into the loop at a different place, and the
place decides what zelk can promise about it. A tool zelk can trust to
repeat is recovered one way after a crash; a tool it can't is recovered
another. A hook that decides before a call starts has no call to clean up
after.

The diagram is the loop's ring from [The loop](/concepts/loop/), with each
extension drawn where it attaches. The scenes below it play a hook that says
no and one that asks you, an MCP call, a subagent, and a crash in the middle
of an MCP call.

The ring is the loop’s: fold, ask, record intent, do it, record result. Each extension plugs in at a different place:

| Extension | Plugs in at | What it gets, or gives up |
|---|---|---|
| Gate hook | between ask and record intent | decides before the call’s intent exists; its decision is recorded and never re-run on replay |
| Go tool | do it | shares a wave and is run again after a crash only as far as it declared read-only, idempotent or an idempotency key |
| MCP server | do it | never read-only, so it runs alone; a call cut off by a crash is recorded as unknown |
| Subagent | do it | a session of its own, with a log and a lease; the parent’s log holds the call and two records that link the logs |

Each scene is the parent’s log as it grows:

- **A hook says no**. A gate hook decides before the call has an intent. The fold asks for the hook, not the call. The hook’s decision is recorded, and deny turns the call back with a refusal as its result. No intent is ever written, so a crash leaves nothing to reconcile, and a replay reads the recorded decision instead of running the hook again.
   1. `command_accepted` user_message: clear the build directory
   2. `effect_started` call_provider
   3. `effect_finished` wants: Bash rm -rf build
   4. `effect_started` run_hook no-rm
   5. `hook_decided` no-rm: deny
   6. `effect_finished` run_hook no-rm
   7. `permission_decided` deny: a PreToolUse hook denied it
   8. `effect_started` call_provider
   9. `effect_finished` end_turn
   10. `run_finished` success
- **A hook asks you**. Ask sends the call out to a person. The call waits as an approval and nothing runs. If you allow it, its intent is recorded and it goes on. If you deny it, it ends as a refusal with no intent, as deny does. A hook that answers defer asks the same way, and lets the session suspend while it waits.
   1. `command_accepted` user_message: ship the fix
   2. `effect_started` call_provider
   3. `effect_finished` wants: Bash git push
   4. `effect_started` run_hook confirm-push
   5. `hook_decided` confirm-push: ask
   6. `effect_finished` run_hook confirm-push
   7. `effect_started` await_input: approve Bash
   8. `command_accepted` answer: allow
   9. `effect_started` run_tool Bash git push
   10. `effect_finished` Bash git push → ok
   11. `effect_started` call_provider
   12. `effect_finished` end_turn
   13. `run_finished` success
- **An MCP call**. A tool from an MCP server never shares a wave. order_status is a Go tool that declared itself read-only, and zelk trusts the host’s own tools, so it gets a wave. The MCP tool says it is read-only too, but zelk never relies on a server’s word: it runs alone, behind its own gate. The hook’s allow still meets the permission rules.
   1. `command_accepted` user_message: is order 7 shipped?
   2. `effect_started` call_provider
   3. `effect_finished` wants: order_status 7, mcp__docs__search order 7
   4. `effect_started` run_tool order_status 7
   5. `effect_finished` order_status 7 → ok
   6. `effect_started` run_hook audit
   7. `hook_decided` audit: allow
   8. `effect_finished` run_hook audit
   9. `effect_started` run_tool mcp__docs__search order 7
   10. `effect_finished` mcp__docs__search order 7 → ok
   11. `effect_started` call_provider
   12. `effect_finished` end_turn
   13. `run_finished` success
- **A subagent**. A subagent is a session of its own. The Task call starts it, runs alone like any call that may change things, and waits. The child goes round the same loop with its own log. When it ends, its report comes back as the call’s result, and the parent’s log gains only the call and two records that link the logs.
   1. `command_accepted` user_message: rename Count to Total
   2. `effect_started` call_provider
   3. `effect_finished` wants: Task general
   4. `effect_started` run_tool Task general
   5. `child_started` child session
   6. `child_finished` success
   7. `effect_finished` Task general → ok
   8. `effect_started` call_provider
   9. `effect_finished` end_turn
   10. `run_finished` success
   The child’s own log:
   - `session_started` own log
   - `command_accepted` user_message: find where Count is used
   - `effect_started` call_provider
   - `effect_finished` wants: Grep Count
   - `effect_started` run_tool Grep Count
   - `effect_finished` Grep Count → ok
   - `effect_started` call_provider
   - `effect_finished` end_turn
   - `run_finished` success
- **A crash mid-MCP-call**. A call cut off by a crash is unknown, not run again. The log ends at the intent. A new process finds the call open and can’t tell whether the server acted, so it records the outcome as unknown and tells the model to check. A Go tool that declared a read-only, idempotent call, or enforces an idempotency key, is run again instead.
   1. `command_accepted` user_message: open an issue for the bug
   2. `effect_started` call_provider
   3. `effect_finished` wants: mcp__tracker__create_issue bug
   4. `effect_started` run_tool mcp__tracker__create_issue bug
   5. `effect_reconciled` unknown: verify before retrying
   6. `effect_started` call_provider
   7. `effect_finished` end_turn
   8. `run_finished` success

Two places matter. A **gate hook** sits between asking for a call and
recording its intent. Everything else sits at **do it**: a Go tool, an MCP
tool and a subagent are three ways to run a tool call.

## Your own Go tools

A Go tool declares what it is. `tool.Define` takes the schema from the
input type, and options say whether the tool is read-only, idempotent, or
keyed by an idempotency key ([Custom tools](/go/tools/) lists them). zelk
checks the model's input against the schema before the handler runs.

The declarations are promises, and zelk acts on them:

- **Read-only** lets the call share a wave with other read-only calls and
  run in plan mode. Any other call runs alone.
- **Read-only and idempotent** lets recovery run the call again after a
  crash, since a repeat can't change anything.
- **An idempotency key** lets recovery run the call again with the same key,
  once the tool deduplicates by it. The key is the same on every attempt.

A tool that declares none of these is left `unknown` after a crash: zelk
can't tell whether it ran. zelk believes the declarations of tools written
in Go by the host, so a tool that claims to be read-only and isn't breaks
the promise.

## MCP servers

An MCP server is a separate program that offers tools over the Model
Context Protocol. Its tools join the tool list as `mcp__server__tool`, and
rules and hook matchers name them like any other. zelk never trusts a
server's claim that a tool is read-only, so an MCP call runs alone, asks in
`default` mode until a rule allows it, and is recorded `unknown` if a crash
cuts it off. A stdio server runs under the sandbox like any command; what an
HTTP server does on its own host is beyond any sandbox.
[MCP servers](/concepts/mcp/) explains the protocol and each of these.

## Subagents

A subagent is a child session that a `Task` call starts, with its own log,
its own lease and, if the host set one, its own share of the budget. Its
authority only narrows from its parent's, at most five run at once, and a
child can't start children of its own. Its questions reach you through its
parent. [Subagents](/concepts/subagents/) explains how a tree of sessions
runs, asks and recovers.

## Hooks

A hook runs your code at a fixed point in a session. A **gate** hook, such
as `PreToolUse`, decides before a call has an intent, so a denied call never
starts; its decision is recorded and never run again on replay. An **async**
hook only observes, best effort. [Hooks](/concepts/hooks/) lists every
event and what each decision and failure does.

## When to use which

| You want to | Use | It plugs in at | Watch for |
|---|---|---|---|
| Give the model an action only your code can do | A Go tool | do it | Your declarations decide waves and recovery, so they must be true |
| Use tools someone else wrote, in any language | An MCP server | do it | A call runs alone, asks by default and ends `unknown` after a crash |
| Hand off a piece of work with its own context | A subagent | do it, as a `Task` call | A child has less authority than its parent, and can't start children |
| Refuse, rewrite or ask about a call by policy | A gate hook | before the intent | A `hooks.json` hook that fails lets the call go unless it blocks on failure |
| Watch what happens without changing it | An async or observe hook | after the fact | It is best effort and may not run |

## Where to go deeper

- [MCP servers](/concepts/mcp/), [Subagents](/concepts/subagents/) and
  [Hooks](/concepts/hooks/): each extension on its own page
- [Custom tools](/go/tools/), [Skills and subagents](/go/skills-subagents/),
  [MCP servers](/cli/mcp/) and [Hooks](/cli/hooks/): how to write and
  configure each
- [The loop](/concepts/loop/): the ring, its waves and its subagents
- [Durability and recovery](/concepts/durability/): how recovery settles a
  call a crash left open
- [Permissions](/concepts/permissions/): where hooks sit in each decision
- [The tools contract](/internals/tools/), [the hooks
  contract](/internals/hooks/) and [the embedding
  contract](/internals/embedding/): the exact rules
- The [durability contract](/internals/durability/#h1) gives the hook
  protocol and how a tree of sessions suspends.
