codex-mcp
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseYour role when these tools are available
这些工具可用时你的角色
If the codex MCP tools are available, delegate ALL work through them. Do not
read files, search code, or run commands yourself. Pick the right mode for
the task:
| You do | Codex does (via MCP tools) |
|---|---|
| Plan what to do next | |
| Handle a targeted read-only follow-up | |
| Craft the prompt for each turn | |
| Route to the right thread | |
| Report results to user | |
| Decide when to resume vs start fresh | |
Incremental prompting: don't dump everything into one call. Send focused
turns, use each response to craft the next prompt. Pass to
continue a previous run with full context.
thread_idReview pattern: after changes code, use
in a fresh thread with the original requirement — Codex
evaluates independently without self-review bias.
codex_run(mode=build)codex_reviewIf the tools are NOT available, run the setup below.
如果codex MCP工具可用,所有工作都通过它们来完成。不要自行读取文件、搜索代码或运行命令。为任务选择合适的模式:
| 你需要做的事 | Codex(通过MCP工具)完成的工作 |
|---|---|
| 规划下一步行动 | |
| 处理定向只读后续查询 | |
| 为每一轮交互设计提示词 | |
| 路由到正确的线程 | |
| 向用户反馈结果 | |
| 决定是继续之前的任务还是重新开始 | |
增量提示原则: 不要把所有内容都塞进一次调用里。发送聚焦的单轮请求,根据每轮返回的结果设计下一个提示词。传入即可在保留完整上下文的情况下继续之前的运行任务。
thread_id审查模式: 当修改代码后,在新线程中使用并传入原始需求——Codex会进行独立评估,不会出现自我审查的偏差。
codex_run(mode=build)codex_review如果工具不可用,请执行以下设置流程。
Non-Negotiable Workflow
不可变通的工作流
When this skill is invoked, follow this contract strictly:
- Do not proceed with the user's actual task through direct local work.
- First check whether /
codex_runare already available.codex_review - If the tools are missing, do only the setup workflow in this skill.
- During setup, do read-only detection first and stop for explicit user
permission before any install, copy, config write, or edit.
.gitignore - If the user does not approve setup changes, stop. Do not bypass MCP by reading files, editing code, or running the task directly yourself.
- Configure only the invoking agent by default. Do not modify other agent configs unless the user explicitly asks for multi-agent setup.
- After setup, tell the user to restart the agent session and wait for the restarted session to use the MCP tools. Do not continue the original task in the same non-MCP turn.
当这个技能被调用时,请严格遵守以下规则:
- 不要直接在本地执行用户的实际任务。
- 首先检查/
codex_run是否已经可用。codex_review - 如果缺少工具,仅执行本技能中的设置工作流。
- 设置过程中,首先执行只读检测,在进行任何安装、复制、配置写入或编辑操作前,必须先获得用户明确授权。
.gitignore - 如果用户不批准设置更改,请停止操作。不要通过读取文件、编辑代码或直接运行任务的方式绕过MCP。
- 默认仅配置发起调用的Agent。除非用户明确要求多Agent设置,否则不要修改其他Agent的配置。
- 设置完成后,告知用户重启Agent会话,等待会话重启后再使用MCP工具。不要在当前非MCP的会话轮次中继续执行原始任务。
What this does
功能说明
Two MCP tools wrapping Codex app-server. Each call bakes in a role prefix —
instructions that guide Codex's behavior for that task type, so quality stays
consistent even after many turns.
| Tool | Modes | Thread | When to use |
|---|---|---|---|
| explore | new or resume | Read/navigate codebase — never modifies files |
| inspect | new or resume | Targeted read-only checks on files, config, or injected context |
| build | new or resume | Write, edit, create, and run code |
| debug | new or resume | Reproduce → diagnose → fix → verify a bug |
| test | new or resume | Write or run tests, report pass/fail |
| research | new or resume | Web search only — no file writes |
| — | isolated | Independent code review — fresh thread, no self-bias |
Pass to to resume an existing run thread. Omit to
start fresh. threads are namespace-isolated — never pass a
review to or vice versa (the server enforces this
with a hard error).
thread_idcodex_runcodex_reviewthread_idcodex_runUse for broad discovery and mapping. Use for narrow
read-only checks, especially when the turn is driven by injected context or a
specific config/file target.
exploreinspectZero external dependencies. Manages app-server lifecycle, thread state, model
discovery, approval handling, and timeout transparently.
两个MCP工具封装了Codex app-server。每次调用都内置了角色前缀——针对不同任务类型指导Codex行为的指令,因此即使经过多轮交互,输出质量也能保持稳定。
| 工具 | 模式 | 线程 | 使用场景 |
|---|---|---|---|
| explore | 新建或续接 | 读取/导航代码库——不会修改任何文件 |
| inspect | 新建或续接 | 对文件、配置或注入上下文进行定向只读检查 |
| build | 新建或续接 | 编写、编辑、创建和运行代码 |
| debug | 新建或续接 | 复现→诊断→修复→验证Bug |
| test | 新建或续接 | 编写或运行测试,报告通过/失败结果 |
| research | 新建或续接 | 仅网页搜索——不会写入文件 |
| — | 隔离 | 独立代码审查——全新线程,无自我偏差 |
向传入即可续接已有的运行线程,不传则新建线程。线程是命名空间隔离的——永远不要将审查的传给,反之亦然(服务器会强制校验,违规会直接返回错误)。
codex_runthread_idcodex_reviewthread_idcodex_runexploreinspect无外部依赖。会透明地管理app-server生命周期、线程状态、模型发现、授权处理和超时逻辑。
Setup (run this automatically when skill triggers)
设置流程(技能触发时自动运行)
npx skills add.agents/skills/codex-mcp/scripts/codex-mcp-server.mjs.claude/skills/See for per-agent config snippets and Windows commands.
references/setup.mdFollow these steps in order:
- Read-only preflight — gather facts without writing anything:
- Resolve the absolute path to
.agents/skills/codex-mcp/scripts/codex-mcp-server.mjs - Check CLI is installed
codex - Check has a
.codex/config.tomllinemodel - Config-drift check: read the value for
argsin the active agent config and compare it to the resolved project-local path above. If they differ (e.g. a stale global path is still registered), flag this as a required fix — the config must be updated before setup is considered done.codex-mcp - Identify which agent is invoking this skill
- Resolve the absolute path to
- Present findings and wait — report what exists, what's missing, what you intend to write. Stop and wait for explicit user approval before any writes.
- Configure the invoking agent only — add the MCP entry pointing to the
resolved absolute path of the project-local script. Use resolved absolute
paths in config values (never or
~). Do not copy the script elsewhere.$HOME - Add to project
memory/codex-threads.json— per project..gitignore - Tell the user to restart — do not continue the original task in the same session unless MCP tools are already loaded.
- Post-restart health probe — in the new session, make one lightweight
call before starting real work. If this returns
codex_run(mode=inspect, prompt="echo ok")or fails, setup is not done — go to the "On failure" section. Do not treat tool visibility alone as proof of a healthy transport.Transport closed
npx skills add.agents/skills/codex-mcp/scripts/codex-mcp-server.mjs.claude/skills/查看获取各Agent的配置片段和Windows命令。
references/setup.md请按顺序执行以下步骤:
- 只读预检——不写入任何内容,收集必要信息:
- 解析的绝对路径
.agents/skills/codex-mcp/scripts/codex-mcp-server.mjs - 检查CLI是否已安装
codex - 检查是否包含
.codex/config.toml配置项model - 配置漂移检查: 读取活跃Agent配置中的
codex-mcp值,与上面解析得到的项目本地路径对比。如果两者不一致(例如仍注册了过期的全局路径),标记为需要修复——配置更新完成后才能视为设置完成。args - 识别发起调用本技能的Agent
- 解析
- 展示检查结果并等待授权——告知用户已有的内容、缺失的内容以及你打算执行的写入操作。在进行任何写入前停止操作,等待用户明确授权。
- 仅配置发起调用的Agent——添加指向已解析的项目本地脚本绝对路径的MCP入口。配置值使用解析后的绝对路径(不要使用或
~)。不要将脚本复制到其他位置。$HOME - 将添加到项目的
memory/codex-threads.json中——针对每个项目单独配置。.gitignore - 告知用户重启——不要在同一会话中继续执行原始任务,除非MCP工具已经加载完成。
- 重启后健康检查——在新会话中,开始实际工作前先执行一次轻量调用。如果返回
codex_run(mode=inspect, prompt="echo ok")或调用失败,说明设置未完成——请前往「故障处理」章节。不要仅以工具可见作为传输链路正常的证明。Transport closed
Thread registry
线程注册表
The server tracks threads in — mapping thread IDs
to topics so the agent routes to the right thread by topic, not by recency.
Threads persist across server restarts via .
memory/codex-threads.jsonthread/resumeSee for the full schema, maintenance rules
(when to add/update/close rows), and the routing decision tree (how to pick
the right before each call).
references/thread-registry.mdthread_id服务器在中跟踪线程信息——将线程ID映射到主题,这样Agent可以按主题而不是按最近使用时间路由到正确的线程。线程可以通过在服务器重启后继续使用。
memory/codex-threads.jsonthread/resume查看获取完整的schema、维护规则(何时添加/更新/关闭条目)和路由决策树(每次调用前如何选择正确的)。
references/thread-registry.mdthread_idPrompting Codex well
向Codex发送高效提示词
Every prompt should contain: what to do + where (file paths) +
expected outcome + constraints. Pick the right mode — its baked-in
role prefix handles the rest.
Scoping prompts: One focused task per call, pass to continue.
Codex accumulates context across calls — the second call already knows what
the first found. This gives better results than broad prompts, and prevents
inactivity timeouts on very large scopes ( on deep directory trees
is most at risk). Use the routing decision tree in
to pick the right thread.
thread_idexplorereferences/thread-registry.mdcodex_run(explore, prompt="Map skills/project-memory/") → T1
codex_run(explore, thread_id=T1, prompt="Now map skills/agent-handoff/")Each mode's role prefix instructs Codex to use subagents for parallelism
(file reading, multi-file edits, test execution, etc.). This produces a
denser event stream that reduces timeout risk, but focused prompts still
give higher quality results than broad ones.
If a single call is legitimately large, pass or
(per-call override, not global).
timeout=120timeout=180codex_run(explore): "List all exported functions in src/auth/ and their error handling patterns."
codex_run(inspect): "Use the injected `pwd` output and report the exact project root. Do not modify any files."
codex_run(build): "Add null checks to all auth functions that access user.email."
codex_run(build, thread_id=T1): "Also add the same null checks in src/payment/."
codex_run(test): "Write tests for the null-check cases in src/auth/. Cover: null, undefined, empty string."
codex_review: "Read src/auth/. Requirement: every user.email access has a null check. List any gaps."
codex_run(debug): "Login fails with TypeError on line 42 of src/auth.ts when email is null. Fix it."每个提示词都应该包含:要做什么 + 在哪里(文件路径) + 预期结果 + 约束条件。选择正确的模式——其内置的角色前缀会处理其余逻辑。
提示词范围原则: 每次调用仅处理一个聚焦的任务,续接任务时传入。Codex会在多次调用中累积上下文——第二次调用已经知道第一次调用的结果。这种方式比宽泛的提示词效果更好,还能避免超大范围任务导致的非活动超时(深度目录树的操作最容易触发超时)。使用中的路由决策树选择正确的线程。
thread_idexplorereferences/thread-registry.mdcodex_run(explore, prompt="Map skills/project-memory/") → T1
codex_run(explore, thread_id=T1, prompt="Now map skills/agent-handoff/")每种模式的角色前缀都会指导Codex使用子Agent实现并行处理(文件读取、多文件编辑、测试执行等)。这会生成更密集的事件流,降低超时风险,但聚焦的提示词仍然比宽泛的提示词输出质量更高。
如果单次调用的任务确实很大,可以传入或(单次调用超时覆盖,不是全局配置)。
timeout=120timeout=180codex_run(explore): "List all exported functions in src/auth/ and their error handling patterns."
codex_run(inspect): "Use the injected `pwd` output and report the exact project root. Do not modify any files."
codex_run(build): "Add null checks to all auth functions that access user.email."
codex_run(build, thread_id=T1): "Also add the same null checks in src/payment/."
codex_run(test): "Write tests for the null-check cases in src/auth/. Cover: null, undefined, empty string."
codex_review: "Read src/auth/. Requirement: every user.email access has a null check. List any gaps."
codex_run(debug): "Login fails with TypeError on line 42 of src/auth.ts when email is null. Fix it."On failure
故障处理
Tools visible but transport immediately closed — if the MCP tools appear in
the agent but the very first call (or the follow-up status ping)
returns :
codex_runTransport closed- Do not retry blindly. The transport is dead, not slow.
- Check for config-path drift: does the registered path in the invoking agent's config (not just
args) match the actual project-local script path? A stale global path is the most common cause after a skills update. Fix the config and restart.~/.codex/config.toml - If the path is correct, verify the wrapper and app-server independently:
- Windows: see for PowerShell commands and diagnosis sequence.
references/troubleshooting-windows.md - Unix / macOS: run the wrapper directly
(); then run
node "$(realpath .agents/skills/codex-mcp/scripts/codex-mcp-server.mjs)"standalone. If both start cleanly, inspect stderr from the wrapper'scodex app-servercall.spawn()
- Windows: see
- Tell the user exactly which step failed and what to check next.
General failure or timeout:
- Run a follow-up task to check status (ping "status" to see if it's still running)
- If it fails again, do not fall back to direct execution — that defeats context hygiene
- Tell the user what failed and why, so they can decide next steps
Never silently switch to reading files or running commands yourself. The user
chose MCP delegation for a reason, and invoking this skill means the agent
must stay inside this workflow.
工具可见但传输链路立即关闭——如果Agent中显示MCP工具可用,但第一次调用(或后续的状态 ping)返回:
codex_runTransport closed- 不要盲目重试。传输链路已经失效,不是响应慢。
- 检查配置路径漂移:发起调用的Agent的配置(不只是)中注册的
~/.codex/config.toml路径是否与实际的项目本地脚本路径匹配?技能更新后最常见的原因就是使用了过期的全局路径。修复配置后重启。args - 如果路径正确,分别验证包装脚本和app-server:
- Windows:查看获取PowerShell命令和诊断流程。
references/troubleshooting-windows.md - Unix / macOS:直接运行包装脚本();然后单独运行
node "$(realpath .agents/skills/codex-mcp/scripts/codex-mcp-server.mjs)"。如果两者都能正常启动,检查包装脚本codex app-server调用的stderr输出。spawn()
- Windows:查看
- 明确告知用户哪一步失败了,以及下一步需要检查什么。
通用故障或超时:
- 执行后续任务检查状态(发送ping "status"查看是否仍在运行)
- 如果再次失败,不要回退到直接执行——这会破坏上下文 hygiene
- 告知用户失败的内容和原因,让用户决定下一步操作
永远不要静默切换到自行读取文件或运行命令的模式。用户选择MCP委托是有原因的,调用本技能意味着Agent必须严格遵守本工作流。
Troubleshooting
问题排查
| Problem | Fix |
|---|---|
| "Codex CLI not found" | |
| Tools don't appear in agent | Check config path is absolute. Restart agent session. |
Wrong project used for | Always pass |
| Timeout errors | Default is 60s inactivity. Pass |
| "app-server exited" | Check |
| Check config-path drift first (most common cause). Then see |
| Turn hangs then times out | Likely an unhandled approval request. Update wrapper to latest version. Check stderr for unhandled method names. |
| "Codex completed with no output" | Check the |
| bwrap/sandbox errors | Expected in containers. The server uses |
| Thread state lost after restart | Expected — server state is in-memory. Registry staleness check (Step 0) handles this automatically. |
| Wrong thread routed | Check |
| Cross-namespace thread_id error | You passed a review thread_id to codex_run or vice versa. Check registry status column. |
| 问题 | 修复方案 |
|---|---|
| "Codex CLI not found" | |
| 工具未在Agent中显示 | 检查配置路径为绝对路径。重启Agent会话。 |
| 总是在工具调用中明确传入 |
| 超时错误 | 默认非活动超时为60秒。大型任务可以传入 |
| "app-server exited" | 检查 |
| 首先检查配置路径漂移(最常见原因)。然后参考 |
| 轮次挂起然后超时 | 很可能是未处理的授权请求。将包装脚本更新到最新版本。检查stderr中未处理的方法名。 |
| "Codex completed with no output" | 检查响应中的 |
| bwrap/sandbox错误 | 容器中属于预期行为。服务器默认使用 |
| 重启后线程状态丢失 | 属于预期行为——服务器状态存储在内存中。注册表过期检查(步骤0)会自动处理这个问题。 |
| 路由到错误的线程 | 检查 |
| 跨命名空间thread_id错误 | 你将审查线程的thread_id传给了codex_run,反之亦然。检查注册表的状态列。 |
Architecture
架构
Agent (Claude Code / Gemini CLI / Cursor / Codex CLI / Antigravity / Augment)
└─ MCP protocol (stdio)
└─ codex-mcp-server.mjs (.agents/skills/codex-mcp/scripts/)
├─ runServers map (per projectDir, codex_run threads)
├─ reviewServers map (per projectDir, codex_review threads, isolated)
└─ Codex app-server (JSON-RPC over stdio)
└─ GPT model (reads, writes, executes)
<project-root>/memory/codex-threads.json
└─ thread registry (topic-based routing, managed by the orchestrating agent)
created automatically on first tool call, isolated per projectThe MCP server spawns one app-server process per namespace per project
directory and keeps it alive across tool calls. Thread state is maintained in
memory — registry in maps thread IDs to topics so
routing survives context growth. On shutdown (SIGINT/SIGTERM), all app-server
processes are cleaned up.
memory/codex-threads.jsonThe wrapper handles the core protocol surface: turn lifecycle, text output,
diffs, file writes, command/file/permissions approvals, and token usage. Some
newer methods (, , tool forwarding via
) are not yet supported — unhandled server requests receive a
JSON-RPC error to prevent silent hangs.
thread/forkturn/steeritem/tool/call-32601Agent (Claude Code / Gemini CLI / Cursor / Codex CLI / Antigravity / Augment)
└─ MCP protocol (stdio)
└─ codex-mcp-server.mjs (.agents/skills/codex-mcp/scripts/)
├─ runServers map (per projectDir, codex_run threads)
├─ reviewServers map (per projectDir, codex_review threads, isolated)
└─ Codex app-server (JSON-RPC over stdio)
└─ GPT model (reads, writes, executes)
<project-root>/memory/codex-threads.json
└─ thread registry (topic-based routing, managed by the orchestrating agent)
created automatically on first tool call, isolated per projectMCP服务器为每个项目目录的每个命名空间生成一个app-server进程,并在多次工具调用中保持进程存活。线程状态存储在内存中——中的注册表将线程ID映射到主题,因此路由不会受上下文增长影响。收到关闭信号(SIGINT/SIGTERM)时,所有app-server进程都会被清理。
memory/codex-threads.json包装脚本处理核心协议逻辑:轮次生命周期、文本输出、diff、文件写入、命令/文件/权限授权,以及token用量统计。部分较新的方法(、、通过进行工具转发)暂不支持——未处理的服务器请求会返回JSON-RPC 错误,避免静默挂起。
thread/forkturn/steeritem/tool/call-32601