Agent tools and AGENT.md
The agent drives its own env over MCP and reads a context file that tells it where its services are and when to fork or reset. It can see, reset and fork its own world, and nobody else's.
Scope
spwn mcp is an MCP server over stdio, scoped to one env. It finds its env from the worktree it starts in and can act only on that env and the children it forked. Secrets never cross it: tools report secret names and whether they resolved, never values. Hosts start it from the project's .mcp.json, which spwn install writes:
{ "mcpServers": { "spwn": { "command": "spwn", "args": ["mcp"] } } }
Tools
| Tool | Arguments | Returns | Exposed |
|---|---|---|---|
env_status | none | State, services and health, URLs, ports, volumes, snapshot id, secret names, and whether this env holds the lock | always |
env_logs | service, lines, grep | Log tail (200 lines by default) | always |
env_exec | service, cmd | Exit code and output of a command run inside a service | not built yet: not listed by tools/list or named in AGENT.md. What "inside" means for a host-process service is undecided; [agent] exec_services parses but has nothing to limit. |
env_reset | to (optional snapshot id) | The new status after volumes are restored | always |
env_snapshot | name | Snapshot id | always |
env_fork | name, worktree (clone or shared) | Child env id, worktree path, ports and URLs | always; at most [agent] max_forks live descendants |
env_lock | wait (default true), timeout (default 10m) | state (active or waiting), the holder, queue position, ports and URLs | when [limits] active ≥ 1 |
env_unlock | none | state: "parked" and the env that got the slot | when [limits] active ≥ 1 |
env_destroy | env (self or a child) | Confirmation | only with spwn mcp --enable-destroy |
[agent] tools narrows the list further. A fork returns a complete new world, not a database branch: the child has its own id, worktree, port block and URL, and the agent can address it with the same tools.
The context file
Most agents won't call a tool they don't know about, but they do read a file that tells them where the API is and how to reset the database. At ready, spwn writes .spwn/AGENT.md (or [agent] context_file). It is byte-identical for the same env, so it diffs cleanly. The default output for a forked env looks like this:
# spwn env k7mq
This worktree runs in its own spwn env `k7mq` (id `01JF…`, project `orders`),
forked from `abcd`. Its services, volumes, ports and URLs are yours alone; other
worktrees have their own.
## Ports
Code reaches services on 127.0.0.1; preview URLs are for browsers.
| Port | Address | Preview URL |
| ---- | --------------- | ---------------------------------- |
| pg | 127.0.0.1:41001 | |
| web | 127.0.0.1:41000 | http://web.k7mq.localhost:8642 |
## Volumes
- pgdata: `postgres://postgres@127.0.0.1:41001/orders`
## Tools
The `spwn` MCP server acts on this env and its forks only: `env_fork`,
`env_logs`, `env_reset`, `env_snapshot`, `env_status`.
## When to fork or reset
- Before a destructive change (a migration, a bulk delete, a risky script),
fork the env (`env_fork` or `spwn fork`) and try it on the copy.
- After you broke the env's state, reset it (`env_reset` or `spwn reset`):
its volumes return to their base snapshot.
- `spwn status` and `spwn logs` show what runs and why it failed.
## Notes
Run `make test-int` against this env.
The Notes section is [agent] notes from your spwn.toml: repo-specific guidance such as which test command reads the env file. [agent] include_in = ["CLAUDE.md"] adds an @.spwn/AGENT.md import line to the files you list, so hosts that read CLAUDE.md pick the context up without you pasting it.
With the env lock on
When the repo sets [limits] active ≥ 1, the file opens with the lock rule, because an agent that forgets it will test against a parked env:
## Before you use this env: take the lock
This repo runs at most one env at a time on this machine ([limits] active = 1).
Other envs are parked: their services are stopped, their data, ports and URLs kept.
- Call `env_lock` before you use the env (run tests, call its URLs, query its
databases). It returns when the env is running, with its ports and URLs. If
another session holds the lock, expect to wait …
- Call `env_unlock` when your task is done, so the next session gets the slot.
Your env is parked, not destroyed: `env_lock` brings it back as you left it.
- `env_status` tells you whether you hold the lock. If a call fails saying you
lost the lock, call `env_lock` again.
- Forks you make while holding the lock share your slot.
The env file
.spwn/env (or [env_file] path, for example .env.local for frameworks that auto-load it) is the other half of the agent surface: the app, the agent's shell and the test runner all read the same values. It always contains SPWN_ENV_ID, SPWN_ENV_SLUG, SPWN_PARENT_ID, SPWN_WORKTREE, both names of every port and one SPWN_URL_<ROUTE> per preview URL, plus your environment entries. Secrets are left out unless named in [env_file] secrets, because agents read files. Containers never see the file: a container's environment is only what its service declares, as in Compose. It is written at ready, named in AGENT.md and spwn status, and removed on down.
The [agent] table
| Key | Default | Meaning |
|---|---|---|
tools | all except env_destroy | Which MCP tools are exposed. |
max_forks | 4 | Live descendants one env may have, to stop runaway fan-out. |
exec_services | all non-external services | Where env_exec may run. |
logs_tail | 200 | Default lines for env_logs. |
context_file | ".spwn/AGENT.md" | Where the context file is written. Never contains secrets. |
include_in | [] | Files that get an @.spwn/AGENT.md import line. |
notes | "" | Repo-specific guidance appended to the context file. |
Telemetry
Off unless you opt in. When on, spwn counts up, forks, resets and MCP tool calls anonymously, which is how the project measures whether agents fork and reset unprompted. spwn makes no other network call you didn't configure, and works air-gapped.