Skip to main content
Glama
README.md
# claude-codex-bridge

> 🤖 用 AI 助手安装: 让你的 AI 助手 (Claude Code, Codex, Cursor 等) 读 [AI-INSTALL.md](AI-INSTALL.md), 按里面的步骤检查, 询问并安装.
> Using an AI assistant? Ask it to read [AI-INSTALL.md](AI-INSTALL.md) and follow it.

[中文](#中文) | [English](#english)

配套的规则与模板在 https://github.com/harrison-ming/ai-workflows .

---

## 中文

### 这是什么

一个本地运行的 MCP (Model Context Protocol) stdio 服务器. Claude Code 调用它, 它再通过 `@openai/codex-sdk` 驱动 OpenAI Codex.

思路很简单: **一个 AI 当指挥, 一个 AI 干活**. Claude 负责理解需求, 拆任务, 验收结果; Codex 接收边界清楚的小任务去分析或修改代码. 桥接器为每个 `session_key` 保存一个持久的 Codex 会话 (thread), 所以 Claude 可以在同一个项目里反复把任务交给 "同一个" Codex, 它记得之前的上下文.

### 环境要求

- Node.js >= 22.13 (用到内置的 `node:sqlite`)
- 已安装并登录 Codex CLI (桥接器使用本机的 Codex / ChatGPT 登录, 不读取 `OPENAI_API_KEY`)
- Claude Code

### 从源码安装

```bash
git clone https://github.com/harrison-ming/claude-codex-bridge.git
cd claude-codex-bridge
npm ci
npm run build
```

构建产物在 `dist/index.js`.

### 注册到 Claude Code

```bash
claude mcp add codex-bridge -- node /path/to/claude-codex-bridge/dist/index.js
```

Claude Code 会在项目目录里启动 MCP 服务器, 默认情况下桥接器只允许 Codex 在这个启动目录 (及其子目录) 里工作. 如果要允许多个目录, 用环境变量指定:

```bash
claude mcp add codex-bridge \
  -e CODEX_BRIDGE_ALLOWED_ROOTS="$HOME/projects/my-app:$HOME/projects/my-lib" \
  -- node /path/to/claude-codex-bridge/dist/index.js
```

多个目录之间用系统路径分隔符隔开 (macOS / Linux 是 `:`, Windows 是 `;`).

### MCP 工具

| 工具 | 作用 | Codex 沙箱 |
|---|---|---|
| `codex_analyze` | 新建或续接一个持久的 Codex 会话, 只读分析 | `read-only` |
| `codex_execute` | 新建或续接一个持久的 Codex 会话, 允许修改工作区 | `workspace-write` |
| `codex_sessions` | 列出桥接器保存的会话, 可按 `cwd` 过滤, 可包含已归档的. 只列出保存的 `cwd` 在允许根目录之内的会话 | 不调用 Codex |
| `codex_session_archive` | 软归档或恢复一个会话映射, 不删除 Codex 端的历史. 允许根目录之外的会话同样返回 `SESSION_NOT_FOUND` | 不调用 Codex |

`codex_analyze` 与 `codex_execute` 的参数:

| 参数 | 说明 |
|---|---|
| `session_key` | 会话名, 1-128 个字符, 只能是字母, 数字, `.` `_` `:` `-`, 且以字母或数字开头. 复用同一个名字就是续接同一个 Codex 会话 |
| `prompt` | 交给 Codex 的任务, 最长 200,000 个字符 |
| `cwd` | 工作目录, 必须是绝对路径. 首次创建会话时必填, 之后可省略; 会话永久绑定到首次使用时的规范路径 (解析符号链接之后), 之后传不同的目录会报错 `SESSION_CWD_MISMATCH` |
| `retry_failed` | 默认 `false`. 如果上一次尝试没有拿到 Codex thread ID, 必须显式设为 `true` 才会重新开一个会话, 桥接器不会悄悄替换 |

同一个会话可以先用 `codex_analyze` 再用 `codex_execute`, 它们共用同一个 Codex thread, 沙箱模式按每次调用决定.

`codex_sessions` 参数: `cwd` (可选, 必须在允许列表内), `include_archived` (默认 `false`).
`codex_session_archive` 参数: `session_key`, `archived` (默认 `true`, 设为 `false` 即恢复).

**返回内容**: `codex_analyze` / `codex_execute` 的文本内容是 Codex 的最终回复, 结构化内容包含 `session_key`, `thread_id`, `cwd`, `sandbox_mode`, `resumed`, `response`, `usage`. `codex_sessions` 返回 `sessions` 数组, `codex_session_archive` 返回 `session` 对象, 每个会话对象包含 `session_key`, `thread_id`, `cwd`, `sandbox_mode`, `status`, `archived`, `created_at`, `updated_at`, `last_used_at`, `archived_at`, `last_error`. `status` 是最近一个回合的状态 (`pending` / `active` / `error`), 是否归档看单独的布尔字段 `archived`.

出错时工具返回 `isError: true` 和一个错误码, 例如 `CWD_REQUIRED`, `WORKSPACE_NOT_ALLOWED`, `SESSION_CWD_MISMATCH`, `SESSION_ARCHIVED`, `SESSION_RETRY_REQUIRED`, `SESSION_BUSY`, `SESSION_NOT_FOUND`, `CODEX_TURN_FAILED`.

### 配置

| 环境变量 | 默认值 | 说明 |
|---|---|---|
| `CODEX_BRIDGE_ALLOWED_ROOTS` | 服务器启动时的当前目录 | 允许 Codex 工作的根目录列表, 用系统路径分隔符隔开. 每个根目录必须存在且是目录 |
| `CODEX_BRIDGE_STATE_DIR` | macOS: `~/Library/Application Support/claude-codex-bridge`; Windows: `%LOCALAPPDATA%\claude-codex-bridge` (未设置 `LOCALAPPDATA` 时按 "其他" 处理); 其他: `$XDG_STATE_HOME/claude-codex-bridge` 或 `~/.local/state/claude-codex-bridge` | 状态目录, 存放会话数据库 `sessions.sqlite3` 和锁目录 `locks/`. 目录权限设为 `0700`, 数据库文件 `0600` |
| `CODEX_BRIDGE_LOG_LEVEL` | `info` | `debug` / `info` / `warn` / `error`. 日志是 JSON 行, 只写到 stderr (stdout 留给 MCP 协议) |

### 我的用法

1. **先计划**: 在 Claude 里把需求想清楚, 拆成几个互相独立, 能单独验证的小任务.
2. **一次只派一个小任务**: 给任务起一个稳定的 `session_key` (例如 `my-app:auth-refactor`), 先用 `codex_analyze` 让 Codex 读代码给方案, 确认后再用 `codex_execute` 让它动手. prompt 里写清楚范围, 不做什么, 怎么算完成.
3. **验证**: Claude 自己看 diff, 跑测试, 不直接相信 Codex 的总结.
4. **单独一轮审查**: 用另一个 `session_key` 开一个新会话, 用 `codex_analyze` 只读审查这次改动, 避免让写代码的那个会话审查自己.

任务做完后用 `codex_session_archive` 归档, 让会话列表保持干净.

### 安全说明

- **工作区允许列表**: `cwd` 必须是绝对路径, 解析符号链接后必须落在某个允许的根目录之内. 同名前缀的兄弟目录 (例如 `my-app-other`) 和指向外部的符号链接都会被拒绝.
- **环境变量过滤**: 传给 Codex 子进程的环境变量是白名单 (`PATH`, `HOME`, 语言, 代理, 证书, `CODEX_HOME`, `XDG_*` 等). `OPENAI_API_KEY`, `OPENAI_BASE_URL`, `CODEX_API_KEY` 等认证变量永远不会传过去, 所以 Codex 使用的是本机登录.
- **Codex 运行选项**: 审批策略固定为 `never`, 关闭网络访问, 永远不使用 `danger-full-access` 沙箱.
- **会话锁**: 同一个 `session_key` 同一时间只能跑一个回合, 另一个调用会很快返回 `SESSION_BUSY`. 不同的会话互不影响.
- **错误信息脱敏**: 返回给 Claude 的错误信息会遮掉看起来像 OpenAI key, `OPENAI_API_KEY=...`, `CODEX_API_KEY=...` 和 `Bearer ...` 的内容, 并截断到 2000 字符.

更多细节见 [docs/security-model.md](docs/security-model.md), 架构见 [docs/architecture.md](docs/architecture.md).

### 局限

- 实际的文件系统隔离靠的是 Codex 自己的沙箱和本机 Codex 配置, 桥接器只负责选择沙箱模式和检查 `cwd`.
- `codex_execute` 下 Codex 可以修改整个工作目录里的文件, 请配合 git 使用, 便于检查和回滚.
- Codex 网络访问是关闭的, 需要联网的步骤 (例如安装依赖) 要由你或 Claude 自己完成.
- 一个回合在 Codex 返回之前会一直占着 MCP 调用, 没有流式输出和中途取消.
- 会话绑定的目录不能修改, 换目录就换一个 `session_key`.
- 只在本机单用户场景下设计和测试, 不是多用户服务.

### 许可证

MIT, 见 [LICENSE](LICENSE).

---

## English

### What it is

A local MCP (Model Context Protocol) stdio server. Claude Code calls it, and it drives OpenAI Codex through `@openai/codex-sdk`.

The idea: **one commander AI, one worker AI**. Claude understands the goal, breaks it into tasks and checks the results; Codex receives small, bounded tasks to analyze or change code. The bridge keeps one persistent Codex thread per `session_key`, so Claude can keep handing work to the "same" Codex within a project and it remembers earlier context.

### Requirements

- Node.js >= 22.13 (uses the built-in `node:sqlite`)
- Codex CLI installed and logged in (the bridge uses your local Codex / ChatGPT login and does not read `OPENAI_API_KEY`)
- Claude Code

### Install from source

```bash
git clone https://github.com/harrison-ming/claude-codex-bridge.git
cd claude-codex-bridge
npm ci
npm run build
```

The entry point is `dist/index.js`.

### Register with Claude Code

```bash
claude mcp add codex-bridge -- node /path/to/claude-codex-bridge/dist/index.js
```

Claude Code starts MCP servers in the project directory. By default the bridge only lets Codex work in that launch directory and its subdirectories. To allow several directories, set an environment variable:

```bash
claude mcp add codex-bridge \
  -e CODEX_BRIDGE_ALLOWED_ROOTS="$HOME/projects/my-app:$HOME/projects/my-lib" \
  -- node /path/to/claude-codex-bridge/dist/index.js
```

Separate entries with the platform path delimiter (`:` on macOS / Linux, `;` on Windows).

### MCP tools

| Tool | Purpose | Codex sandbox |
|---|---|---|
| `codex_analyze` | Start or resume a persistent Codex thread for read-only analysis | `read-only` |
| `codex_execute` | Start or resume a persistent Codex thread that may change the workspace | `workspace-write` |
| `codex_sessions` | List saved bridge sessions, optionally filtered by `cwd`, optionally including archived ones. Only sessions whose saved `cwd` is inside the allowed roots are listed | does not call Codex |
| `codex_session_archive` | Soft-archive or restore a session mapping; never deletes Codex thread history. Sessions outside the allowed roots also return `SESSION_NOT_FOUND` | does not call Codex |

Parameters of `codex_analyze` and `codex_execute`:

| Parameter | Description |
|---|---|
| `session_key` | Session name, 1-128 characters: letters, digits, `.` `_` `:` `-`, starting with a letter or digit. Reusing the name continues the same Codex thread |
| `prompt` | The task for Codex, up to 200,000 characters |
| `cwd` | Absolute working directory. Required when the session is created, optional afterwards. A session is permanently bound to the canonical path (symlinks resolved) of its first use; passing a different directory later fails with `SESSION_CWD_MISMATCH` |
| `retry_failed` | Default `false`. If an earlier attempt never obtained a Codex thread ID, you must pass `true` to start a replacement; the bridge never replaces it silently |

One session can use `codex_analyze` and then `codex_execute`; both continue the same Codex thread, and the sandbox mode is chosen per call.

`codex_sessions` parameters: `cwd` (optional, must be allow-listed), `include_archived` (default `false`).
`codex_session_archive` parameters: `session_key`, `archived` (default `true`; `false` restores).

**Output**: the text content of `codex_analyze` / `codex_execute` is Codex's final response; the structured content holds `session_key`, `thread_id`, `cwd`, `sandbox_mode`, `resumed`, `response` and `usage`. `codex_sessions` returns a `sessions` array and `codex_session_archive` returns a `session` object; each session object holds `session_key`, `thread_id`, `cwd`, `sandbox_mode`, `status`, `archived`, `created_at`, `updated_at`, `last_used_at`, `archived_at` and `last_error`. `status` is the state of the last turn (`pending` / `active` / `error`); archival is the separate boolean `archived`.

On failure a tool returns `isError: true` with an error code such as `CWD_REQUIRED`, `WORKSPACE_NOT_ALLOWED`, `SESSION_CWD_MISMATCH`, `SESSION_ARCHIVED`, `SESSION_RETRY_REQUIRED`, `SESSION_BUSY`, `SESSION_NOT_FOUND`, `CODEX_TURN_FAILED`.

### Configuration

| Variable | Default | Description |
|---|---|---|
| `CODEX_BRIDGE_ALLOWED_ROOTS` | the directory the server was launched from | Roots Codex may work in, separated by the platform path delimiter. Each root must exist and be a directory |
| `CODEX_BRIDGE_STATE_DIR` | macOS: `~/Library/Application Support/claude-codex-bridge`; Windows: `%LOCALAPPDATA%\claude-codex-bridge` (falls back to the "others" path when `LOCALAPPDATA` is unset); others: `$XDG_STATE_HOME/claude-codex-bridge` or `~/.local/state/claude-codex-bridge` | State directory holding the session database `sessions.sqlite3` and the `locks/` directory. The directory is set to `0700`, the database file to `0600` |
| `CODEX_BRIDGE_LOG_LEVEL` | `info` | `debug` / `info` / `warn` / `error`. Logs are JSON lines on stderr only (stdout belongs to MCP) |

### How I use it

1. **Plan**: think the goal through in Claude and split it into small tasks that are independent and verifiable on their own.
2. **Dispatch one small task at a time**: give it a stable `session_key` (for example `my-app:auth-refactor`), ask Codex for a plan with `codex_analyze`, then let it make the change with `codex_execute`. State the scope, what not to touch, and what "done" means.
3. **Verify**: Claude reads the diff and runs the tests itself instead of trusting Codex's summary.
4. **Separate review round**: open a new session under a different `session_key` and run a read-only review with `codex_analyze`, so the session that wrote the code does not review itself.

Archive finished sessions with `codex_session_archive` to keep the list clean.

### Security notes

- **Workspace allow-list**: `cwd` must be absolute and, after resolving symlinks, fall inside an allowed root. Same-prefix siblings (for example `my-app-other`) and symlinks pointing outside are rejected.
- **Environment filtering**: the Codex child process gets an allow-listed environment (`PATH`, `HOME`, locale, proxy, certificate variables, `CODEX_HOME`, `XDG_*`, and similar). Auth variables such as `OPENAI_API_KEY`, `OPENAI_BASE_URL` and `CODEX_API_KEY` are never forwarded, so Codex uses your local login.
- **Codex run options**: approval policy is fixed to `never`, network access is disabled, and the `danger-full-access` sandbox is never used.
- **Session locks**: only one turn per `session_key` runs at a time; a concurrent call returns `SESSION_BUSY` quickly. Different sessions do not block each other.
- **Error redaction**: error messages returned to Claude mask strings that look like OpenAI keys, `OPENAI_API_KEY=...`, `CODEX_API_KEY=...` and `Bearer ...`, and are truncated to 2000 characters.

See [docs/security-model.md](docs/security-model.md) and [docs/architecture.md](docs/architecture.md) for details.

### Limitations

- Actual filesystem isolation comes from Codex's own sandbox and your local Codex configuration; the bridge only picks the sandbox mode and checks `cwd`.
- Under `codex_execute`, Codex can change any file in the working directory. Use git so you can inspect and roll back.
- Codex network access is disabled; steps that need the network (such as installing dependencies) must be done by you or Claude.
- A turn blocks the MCP call until Codex returns; there is no streaming or mid-turn cancellation.
- A session's directory cannot be changed; use a new `session_key` for a different directory.
- Designed and tested for a single local user, not as a multi-user service.

### License

MIT, see [LICENSE](LICENSE).

TDQS

B3.4/5.0

Scored across 4 tools

Disambiguation4/5

codex_analyze and codex_execute both start/resume a persistent Codex thread and differ only in sandbox write access, which is a subtle distinction an agent could misjudge. The descriptions do explicitly name read-only vs workspace-write, so the boundary is recoverable. The two session tools (list vs archive/restore) are clearly distinct.

Naming Consistency4/5

All tools share a predictable codex_ snake_case prefix, making the family obvious at a glance. The suffix convention wavers slightly: codex_analyze/codex_execute are verbs, codex_sessions is a bare noun, and codex_session_archive is a noun_verb compound. This is readable but not a single uniform pattern.

Tool Count4/5

Four tools is well-scoped for a thin bridge that launches threads and manages session mappings. It is slightly lean, since there is no dedicated tool to fetch a thread's output or metadata, but nothing is redundant.

Completeness3/5

The surface covers launching/executing work, listing session mappings, and archiving/restoring them, which is a coherent lifecycle for the bridge layer. However there is no tool to inspect an individual session's details, retrieve prior thread output/history, or hard-delete a mapping, leaving agents without a way to read back results through the bridge.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues