AnyRouter Spawn Agent
MCP fallback that launches and monitors local codex exec agents when native sub agents are unavailable.
README
AnyRouter Spawn Agent
Observable spawn_agent MCP fallback for Codex sessions that use anyrouter.top or other API providers where Codex App native Sub Agents are unavailable.
What This Is
Codex has two different subagent paths:
- Codex App native Sub Agent: preferred when the current Codex runtime exposes native Sub Agent tools and App sidebar integration.
- This MCP fallback: a local MCP server that launches registered agents through detached
codex execchild processes, then exposes job status through MCP tools.
Use native Sub Agents first. Use this repository when native Sub Agents are hidden or unavailable, for example when a session is routed through https://anyrouter.top/v1 and the App native Sub Agent tool is not exposed to the model.
This fallback is intentionally honest about its limits:
- It does not create App sidebar-visible native Sub Agents.
- It does not fake native Sub Agent success.
- It starts real local
codex execchild processes. - It can observe queued/running/completed/failed/timed_out/cancelled fallback jobs through MCP.
Tools
The MCP server exposes:
| Tool | Use |
|---|---|
spawn_agent |
Legacy synchronous one-shot fallback. Best for short fixed-response checks. |
spawn_agent_start |
Start an observable fallback job and return run_id immediately. |
spawn_agent_status |
Check status, pid, thread id, last activity, output tails, and possibly_stalled. |
spawn_agent_result |
Fetch final answer and metadata when ready. |
spawn_agent_list |
List recent jobs in the current MCP server process. |
spawn_agent_cancel |
Cancel queued/running jobs. On Windows it tries taskkill /T /F /PID before child.kill(). |
spawn_agent_issue_record |
Record a redacted diagnostic note/bug for later MCP improvement. |
spawn_agent_issue_list |
List recent diagnostic issues from the persistent journal. |
spawn_agent_issue_report |
Build a Markdown report from recent diagnostic issues. |
possibly_stalled becomes true after five minutes without stdout/stderr activity. It is a warning, not automatic failure.
Diagnostic Journal
The MCP fallback now has a small persistent diagnostic journal for problems discovered while using fallback jobs.
Default location:
$CODEX_HOME/spawn-agent-logs/issues.jsonl
$CODEX_HOME/spawn-agent-logs/events.jsonl
Set SPAWN_AGENT_LOG_DIR to place the journal somewhere else.
The journal is intentionally redacted by default. It stores metadata and short previews such as run_id, agent_type, status, tool, error, timeout_ms, idle_ms, and message_preview. It does not copy full parent prompts, final answers, stdout tails, or stderr tails unless a caller explicitly writes a short note.
Launch accounting is also written to events.jsonl. Every fallback subagent launch records a fallback_launch_recorded event:
- Observable
spawn_agent_startlaunches record their realrun_id. - Legacy synchronous
spawn_agentlaunches recordrun_id: "legacy/no_run_id"and keep the old response shape. - The counter is derived from the persistent event journal, so it can survive MCP server restarts as long as the log files remain.
Every 20 fallback launches, spawn_agent_start returns journal_review_due: true plus journal_review_recommended_tool: "spawn_agent_issue_report" in structuredContent, and the response text includes journal review due. The main Agent should then inspect the issue journal and tell the human what recurring MCP issues were found and what changes are recommended.
After reporting to the human, the main Agent should persist the same redacted findings into this repository's development directory. Use the report writer script:
node "$env:USERPROFILE\.codex\skills\spawn-agent\scripts\write_development_report.js" --limit 20 --output spawn-agent-diagnostic-report.md
The default output is:
development/spawn-agent-diagnostic-report.md
The script can also write main-agent-authored Markdown from stdin:
@'
# Spawn Agent Development Report
## Findings
- ...
## Recommended Changes
- ...
'@ | node "$env:USERPROFILE\.codex\skills\spawn-agent\scripts\write_development_report.js" --stdin-only --output journal-review.md
For safety, output paths must stay inside development/ and must end in .md. The script redacts common bearer tokens, sk-... keys, GitHub PATs, and token/api_key/password assignments before writing.
Issues are recorded automatically for:
failed,timed_out, andcancelledjobs.- First observation of
possibly_stalledfor a run. - Missing
run_idlookups. - Tool argument errors and unknown tool calls.
Use spawn_agent_issue_record manually when a fallback job behaved strangely but did not fail mechanically, for example partial research, unclear timeout behavior, missing capability, bad agent instructions, or a recurring workflow problem.
Example manual issue:
{
"event": "manual_note",
"severity": "warning",
"title": "docs_researcher returned partial source list",
"run_id": "run_000004",
"agent_type": "docs_researcher",
"status": "timed_out",
"notes": "Retry with a narrower source request or longer timeout."
}
Repository Layout
agent-mcp/
spawn_agent_server.js
spawn_agent_server.test.js
agents/
explorer.toml
reviewer.toml
docs-researcher.toml
conversation-analyzer.toml
agent-evaluator.toml
spec-miner.toml
skills/
spawn-agent/
spawn-agent-observer/
examples/
config.spawn-agent.toml
AGENTS.spawn-agent.md
Install
Clone the repo:
git clone git@github.com:wendou-chen/anyrouter-spawn-agent.git
cd anyrouter-spawn-agent
Copy the files into your Codex home:
$codexHome = "$env:USERPROFILE\.codex"
New-Item -ItemType Directory -Force -Path "$codexHome\agent-mcp", "$codexHome\agents", "$codexHome\skills" | Out-Null
Copy-Item -Recurse -Force .\agent-mcp\* "$codexHome\agent-mcp\"
Copy-Item -Recurse -Force .\agents\* "$codexHome\agents\"
Copy-Item -Recurse -Force .\skills\spawn-agent "$codexHome\skills\spawn-agent"
Copy-Item -Recurse -Force .\skills\spawn-agent-observer "$codexHome\skills\spawn-agent-observer"
Add the snippets from examples/config.spawn-agent.toml to C:\Users\<you>\.codex\config.toml, adjusting absolute paths if needed.
Add the rules from examples/AGENTS.spawn-agent.md to your global C:\Users\<you>\.codex\AGENTS.md or to a project-local AGENTS.md.
Restart Codex App or reload the session so MCP tools and skills are rediscovered.
Agent Selection Rule
Use this decision flow:
- Try Codex App native Sub Agent tools when they are visible and supported.
- If native Sub Agents are unavailable, hidden by provider/API mode, or cannot be verified, use the MCP fallback tools from this repository.
- For long-running, exploratory, research, review, or parallel work, use
spawn_agent_startand keep the returnedrun_id. - Use
spawn_agent_statuswhile the job runs andspawn_agent_resultwhen it completes. - Use legacy
spawn_agentonly for short one-shot requests where progress does not matter. - If the fallback mechanism misbehaves or produces a partial/failed result worth improving, call
spawn_agent_issue_recordor include the automatic issue in aspawn_agent_issue_report. - When a launch response says
journal_review_due: true, callspawn_agent_issue_reportand report the issue summary plus recommended fixes to the human. - Persist the same review findings with
node "$env:USERPROFILE\.codex\skills\spawn-agent\scripts\write_development_report.js" --stdin-only --output <name>.mdfrom the target project root so future MCP development inside that repository can read the Markdown report. In this repository,npm run report:issuesis a no-argument shortcut.
Example
Start an observable explorer job:
{
"agent_type": "explorer",
"message": "Inspect the current configuration. Do not edit files.",
"timeout_ms": 900000
}
Then poll:
{ "run_id": "run_000001" }
Report status from observed state, for example:
explorer run_000001: running, pid 1234, thread_id pending, last activity 42s ago, not stalled.
docs_researcher run_000002: completed, thread_id 019..., result ready.
Verify
Run the unit tests:
npm test
Check the server schema directly:
$messages = @(
'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05"}}',
'{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}'
)
$messages | node .\agent-mcp\spawn_agent_server.js
Expected tools:
spawn_agent
spawn_agent_start
spawn_agent_status
spawn_agent_result
spawn_agent_list
spawn_agent_cancel
spawn_agent_issue_record
spawn_agent_issue_list
spawn_agent_issue_report
Notes
- Job state is in-memory only. If the MCP server restarts, historical
run_idstate is lost. - Child timeouts default to 15 minutes and are capped at 30 minutes.
- Registered agents live in
[agents.*]entries inconfig.toml; eachconfig_filemust stay insideCODEX_HOME. - On API-key
codex execsessions, external MCP tools may not be available to the child. Put all required context inmessage.
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
Neon Database
MCP server for interacting with Neon Management API and databases
E2B
Using MCP to run code via e2b.
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.