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.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。
mcp-server-qdrant
这个仓库展示了如何为向量搜索引擎 Qdrant 创建一个 MCP (Managed Control Plane) 服务器的示例。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。