
Codex Subagents
STDIOFile-based sub-agents MCP server for Codex CLI with isolated execution environments
File-based sub-agents MCP server for Codex CLI with isolated execution environments
File‑based sub‑agents for Codex CLI. One MCP tool. Zero fluff.
validate_agents
, list_agents
Claude‑style sub‑agents for Codex CLI via a tiny MCP server. Each call spins up a clean context in a temp workdir, injects a persona via AGENTS.md
, and runs codex exec --profile <agent>
to preserve isolated state.
npm install
npm run build
npm start
Tools exposed by this server:
delegate
list_agents
, validate_agents
Agents directory discovery (in order): --agents-dir
arg, CODEX_SUBAGENTS_DIR
env, then defaults ./agents
, ./.codex-subagents/agents
, dist/../agents
.
# 1) Install + build
npm i && npm run build
# 2) Point Codex at the built server (absolute paths recommended)
# ~/.codex/config.toml
[mcp_servers.subagents]
command = "/usr/bin/env"
args = ["node", "/ABS/PATH/TO/dist/codex-subagents.mcp.js", "--agents-dir", "/ABS/PATH/TO/agents"]
# Example profiles you can reference from agent frontmatter
[profiles.review]
model = "gpt-5"
approval_policy = "on-request"
sandbox_mode = "read-only"
[profiles.debugger]
model = "o3"
approval_policy = "on-request"
sandbox_mode = "workspace-write"
# 3) In Codex, verify tools and agents
tools.call name=list_agents
tools.call name=validate_agents
# 4) Delegate one task to an agent
subagents.delegate(agent="review", task="Summarize and review the last commit")
Tip: See docs/SECURITY.md
for trust boundaries and docs/OPERATIONS.md
for E2E and logging.
Build the server and point Codex at the absolute path to the compiled entrypoint. Pass the agents directory explicitly so the server doesn't scan until after the handshake. The server also falls back to an agents/
folder adjacent to the installed binary (e.g. dist/../agents
) if --agents-dir
and CODEX_SUBAGENTS_DIR
are not provided:
# ~/.codex/config.toml
[mcp_servers.subagents]
command = "/absolute/path/to/node"
args = ["/absolute/path/to/dist/codex-subagents.mcp.js", "--agents-dir", "/absolute/path/to/agents"]
[profiles.review]
model = "gpt-5"
approval_policy = "on-request"
sandbox_mode = "read-only"
[profiles.debugger]
model = "o3"
approval_policy = "on-request"
sandbox_mode = "workspace-write"
[profiles.security]
model = "gpt-5"
approval_policy = "never"
sandbox_mode = "workspace-write"
Usage (in Codex):
AGENTS.md
Route all work through the orchestrator:
subagents.delegate(agent="orchestrator", task="<goal>")
Example security review routed via orchestrator:
subagents.delegate(agent="orchestrator", task="Audit the repo for secrets and unsafe shell calls")
Prefer tool calls over in-thread analysis to keep the main context clean.
Note: MCP servers run outside Codex’s sandbox. Keep surfaces narrow and audited. This server exposes a single tool: delegate
.
agent
(what you pass to subagents.delegate
): the name of an agent loaded from your registry directory. The name is the file basename, e.g., agents/review.md
→ agent review
.profile
(Codex CLI): an execution profile you define in ~/.codex/config.toml
under [profiles.<name>]
. Agents typically specify which profile to use via frontmatter (profile: <name>
), but agent names and profile names don’t have to match.There are no hardcoded built‑in agent names. The server loads agents from disk (agents/*.md|*.json
) or accepts an ad‑hoc agent when both persona
and profile
are provided inline.
delegate
agent
: string — the agent name from your registry (basename of agents/<name>.md|json
)task
: string (required)cwd?
: string (defaults to current working directory)mirror_repo?
: boolean (default false). If true and cwd
provided, mirrors the repo into the temp workdir for maximal isolation.profile?
and persona?
: optional ad-hoc definition when agent
is not found in registry. Provide both.AGENTS.md
with the agent persona.cp -R
fast path.
git worktree add <tempdir> <branch-or-HEAD>
(documented in docs/INTEGRATION.md
).codex exec --profile <agent-profile> "<task>"
with cwd
set to the temp dir if mirrored, else your provided cwd
.{ ok, code, stdout, stderr, working_dir }
.Add agents without code changes:
Create an agents directory and point the server to it via either:
"--agents-dir", "/path/to/agents"
in ~/.codex/config.toml
under the MCP server argsCODEX_SUBAGENTS_DIR=/path/to/agents
./agents
or ./.codex-subagents/agents
Define agents as files using the basename as the agent name:
Example agents/perf.md
:
---
profile: debugger
approval_policy: on-request # one of: never | on-request | on-failure | untrusted
sandbox_mode: workspace-write # one of: read-only | workspace-write | danger-full-access
---
You are a pragmatic performance analyst. Identify hotspots, measure, propose minimal fixes with benchmarks.
Or JSON agents/migrations.json
:
{
"profile": "debugger",
"approval_policy": "on-request",
"sandbox_mode": "workspace-write",
"persona": "You plan and validate safe DB migrations with rollbacks.",
"personaFile": null
}
Call delegate
with a new name and supply both profile
and persona
:
subagents.delegate(
agent="perf",
task="Analyze render jank",
profile="debugger",
approval_policy="on-request",
sandbox_mode="workspace-write",
persona="You are a perf specialist..."
)
List available agents:
tools.call name=list_agents
Validation: approval_policy
and sandbox_mode
are validated against the allowed values above. They are advisory metadata and should match the Codex profile you run under. Enforce actual behavior via profiles in ~/.codex/config.toml
.
Validate agent files:
tools.call name=validate_agents
# or
tools.call name=validate_agents arguments={"dir":"/abs/path/to/agents"}
Returns per-file errors/warnings and a summary. Invalid values are flagged; missing profile
in Markdown is a warning (loader defaults to default
).
npm install
npm run build
npm run lint
npm test
Note: Do not edit dist/
manually; it is build output.
Automated end-to-end check using the real Codex CLI:
npm run e2e
It will:
~/.codex/config.toml
pointing to the built server./mcp
to verify the server is connected.subagents.delegate
.The script requires OPENAI_API_KEY
and a working Codex CLI binary.
Concern | This repo does |
---|---|
Prevent handshake break | No stdout logs; debug only to stderr (DEBUG_MCP=1 ) |
Reduce blast radius | Temp workdir + optional git worktree isolation |
Gate risky personas | validate_agents + profiles/approvals alignment |
Network surface | Single tool (delegate ); Codex handles model I/O |
Auditability | Agents live as files; reviewable in PRs |
Warning: Stdout must be newline‑delimited JSON only. Any logs on stdout will break the MCP handshake. Use
DEBUG_MCP=1
to emit diagnostics to stderr.
npm run e2e
.dist/codex-subagents.mcp.js
path and passes --agents-dir
.DEBUG_MCP=1
to log timing to stderr only.git worktree
over mirror_repo=true
(see docs/INTEGRATION.md
).These come as file‑based agents under agents/
. Each link includes a one‑line goal and suggested metadata (frontmatter).
agents/review.md
: Code review and refactor checklist. Frontmatter: profile: review
(suggested: approval_policy: on-request
, sandbox_mode: read-only
).agents/debugger.md
: Reproduce failures and propose minimal fixes. Frontmatter: profile: debugger
(suggested: approval_policy: on-request
, sandbox_mode: workspace-write
).agents/security.md
: Threat modeling and concrete mitigations. Frontmatter: profile: security
(suggested: approval_policy: never
, sandbox_mode: workspace-write
).agents/perf.md
: Identify hotspots; measure and optimize. Frontmatter: profile: default
(suggested: approval_policy: on-request
, sandbox_mode: workspace-write
).agents/docs.md
: Improve and restructure docs. Frontmatter: profile: default
(suggested: approval_policy: on-request
, sandbox_mode: read-only
).agents/a11y.md
: Accessibility reviews and fixes. Frontmatter: profile: default
(suggested: approval_policy: on-request
, sandbox_mode: read-only
).Invite PRs that add new agents—see “Contribute an agent”.
Fork and create agents/<name>.md
with frontmatter:
profile: debugger approval_policy: on-request sandbox_mode: workspace-write
Then describe the persona in free text.
Run tools.call name=validate_agents
.
Open a PR using the “Agent request / contribution” template.
We track requested personas in GitHub Discussions (open a new topic under Show & Tell if none exists yet).
If this project helps, a star helps others discover it.
docs/INTEGRATION.md
: deeper wiring, profiles, AGENTS.md guidance.docs/SECURITY.md
: isolation, trust boundaries, sandbox guidance.docs/OPERATIONS.md
: logs, env vars, upgrades.MIT