Swarm
Portable coordination rules for any MCP-capable coding agent.
Tool names are namespaced by the host. Depending on the client you may see swarm_register, mcp__swarm__register, or similar variants. Use whichever form your host exposes.
Register early
At the start of every session, call register before using any other swarm tool.
directory: your current project directory (required)scope: omit unless you want multiple directories or worktrees to share one swarm; do not use it to split frontend/backend inside one repofile_root: omit unless working in a disposable worktree that should share locks and annotations with a stable checkoutlabel: optional, but prefer machine-readable tokens likeprovider:codex-cli role:planner team:frontend
No role: token means the session is a generalist.
If your host resets context or you start a fresh window, call register again and rehydrate from poll_messages, list_tasks, list_instances, and any role-specific KV keys you rely on. The shared database is the durable source of truth.
Check for pending work
Immediately after registering, call poll_messages, list_tasks, and list_instances.
- If you have unread messages, read and act on them before starting new work.
- If there are tasks assigned to you (by instance ID or matching your
role:), claim and prioritize them. Prefer the highest-priority task. - Skip tasks with
blockedstatus — they are waiting on dependencies and will becomeopenautomatically. - If you see open
reviewtasks and you handle reviews, claim them before starting implementation work. - If nothing is waiting, proceed with your own task.
Check poll_messages and list_tasks periodically, not just at startup.
React to what you find
When you receive a task via request_task:
- If
claim_taskreports unread messages, callpoll_messagesand handle those messages before retrying claim_taskpromptly — this also moves the task toin_progressfor you in one call- Call
update_taskonce at completion withdoneand a structured result (see below), orfailed/cancelled. Locks on the task's files release automatically. - If the task requires follow-up, create a new
request_task(e.g. the implementer sends areviewtask back to the planner)
When you receive a direct message via send_message:
- Treat it as coordination, not a formal task. Respond with
send_messageor take action.
When you see a broadcast:
- Use it for awareness. No response is required unless the content affects your current work.
- If the broadcast contains
[signal:complete], the planner is signaling all work is done — finish current work and deregister.
Structured results
When completing a task, prefer a JSON result:
{
"files_changed": ["src/foo.ts"],
"test_status": "pass",
"summary": "What was done and why."
}
Fields:
files_changed: array of file paths you modifiedtest_status:"pass","fail", or"skipped"summary: short description of what you did
Fall back to a plain string if you cannot produce structured output.
Lock while editing
When you begin editing a file, call lock_file with a short reason. Its response includes any peer annotations on that file, so a separate pre-check is not needed. If list_instances shows you alone in scope, skip locking until peers join.
Locks on a task's files release automatically when you call terminal update_task. Use unlock_file only for early per-file release before the task as a whole completes.
Delegate clearly
Use request_task for review, implementation, fix, test, or research handoffs.
Include a short title, a useful description, and relevant files when possible. Set assignee only when you want a specific active session to take it. Set priority to control execution order (higher = more urgent).
Use depends_on to express task ordering — a dependent task stays blocked until all its dependencies reach done. If a dependency fails, downstream tasks are auto-cancelled.
Use explicit review tasks for normal code review handoff. Reserve approval_required for true approval gates such as production deploys or human sign-off checkpoints.
When choosing who to delegate to, inspect list_instances labels:
- Prefer a session with a matching
role:token (e.g.role:reviewerfor review work) - If the swarm uses
team:labels, prefer a same-team specialist - Fall back to any matching specialist, then to a generalist
- If multiple planners are active, coordinate ownership before creating tasks in shared areas — use
send_messageandkv_setto divide domains - For planner sessions, check
kv_get("owner/planner")to see which planner currently owns the swarm-wide planner role
Share context
Use annotate to leave findings, warnings, notes, bugs, or todos on files.
Use broadcast for short updates that help everyone stay in sync. Use send_message for direct coordination with one session.
Track shared state
Use kv_set and kv_get for small shared state like plans, owners, or handoff notes.
Keep values short and structured. JSON strings work well when the value needs a little shape.
Progress heartbeats
While working on a task, periodically update your status:
- Key:
progress/<your-instance-id> - Value: short summary of current activity and progress (e.g.
"implementing auth middleware, ~50% done")
This lets planners and other agents check on you with kv_list("progress/") without interrupting your work. Clear your progress key when you finish a task or go idle.
Stay autonomous
After your initial registration and inspection, do not wait for user prompting between tasks. Use wait_for_activity to stay in an active loop:
- After completing a task or when you have nothing to do, call
wait_for_activity. - When it returns with changes, act on them immediately:
- new_messages: Read and respond. Messages prefixed with
[auto]are system notifications about task assignments or completions. - task_updates: Claim open tasks (highest priority first) or review completed ones, depending on your role. Skip
blockedtasks. - kv_updates: Check for plan changes or progress updates from other agents.
- instance_changes: Adapt to agents joining or leaving.
- new_messages: Read and respond. Messages prefixed with
- If it returns with
timeout: true, callwait_for_activityagain — or checklist_tasksfor anything you may have missed. - Repeat until the work is done.
Task creation and completion automatically notify the relevant parties via message. You don't need to manually send_message to inform someone about a task you created for them or completed — but you can add extra context if helpful.
Finish cleanly
When you complete assigned work:
unlock_fileany files you lockedupdate_taskwithdoneand a structured result- If follow-up is needed, create a new
request_task(don't reuse the old one) broadcasta short summary if other sessions should know- If you are leaving the swarm entirely, call
deregisterto release your tasks and locks
If another instance appears stuck or stale, use remove_instance to force-remove it. This releases its tasks and locks and notifies the rest of the swarm.
