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

> Turn OpenAI Codex (CLI + Desktop) into a **real-time observable, agent-friendly Executor / Sub-Agent** for any Leader agent (Antigravity, Claude Code, Cursor, Gemini CLI, or custom agents) via zero-dependency **MCP + Skill + Codex Hooks**.
>
> 把 OpenAI Codex(CLI + Desktop)变成**任意 Agent 可自由编排、人类可在 Codex Desktop 实时围观轨迹**的通用 Executor / Sub-Agent 桥梁。

---

## Why `codex-bridge`?

Standard `codex exec` runs are hidden from Codex Desktop's sidebar (`source="exec"`), while driving a live thread directly through an external `app-server` holds an exclusive writer lock (`~/.codex/thread-writer-locks/*.lock`), causing Codex Desktop to show *"This is open in another app"*.

`codex-bridge` solves both problems with a **dual-mode architecture**:

1. **`desktop_live` (Default — Real-Time Streaming in Codex Desktop)**
   - Spins up a lightweight `codex app-server --stdio` (~0.15s) to create the thread with exact `cwd`, `model`, `effort`, `sandbox`/`permissions`, `approval_policy`, `approvals_reviewer`, `title`, and optional `/goal`, then exits immediately to release the writer lock.
   - Queues the prompt into Codex's cross-process queue (`codex queue`) and opens `codex://threads/<id>`.
   - **Codex Desktop takes ownership cleanly (zero lock conflict, zero refresh)** and streams every reasoning/tool step live under the matching project.
   - `codex-bridge` tails the rollout JSONL read-only to return structured results to the Leader.

2. **`headless_rpc` (Direct App-Server Control & Parallel Workers)**
   - Runs `codex app-server --stdio` directly for features that need an active JSON-RPC stream:
     - **`output_schema`**: strict JSON Schema output
     - **`enable_ask_leader=true`**: injects an `ask_leader` dynamic tool (`item/tool/call`) so Codex can pause mid-turn and ask the Leader for decisions
     - **`codex_steer` / `codex_interrupt`**: live mid-turn steering or cancellation
     - **Parallel background fan-out** without stealing macOS window focus
   - Releases the writer lock cleanly on turn completion so the thread renders natively in Codex Desktop.

3. **Bidirectional Linkage & Traceability**
   - **SQLite Ledger (`~/.codex-bridge/ledger.sqlite`)**: groups multiple Codex threads under a `leader_task_id`, stores deep links (`codex://threads/<id>`), and indexes Hook/callback events.
   - **Codex Hooks (`~/.codex/hooks.json`)**: streams `Stop`, `PermissionRequest`, `PostToolUse`, `SessionEnd`, and `Interrupt` events back to the Leader — even when the human continues a thread manually in Codex Desktop.

---

## Zero-Dependency Quickstart

Requires **Python 3.9+** and **Codex CLI** (`codex` on `PATH` or `CODEX_BIN`). No `pip install` or `venv` required.

### One-Click Setup

```bash
# Install for Antigravity + Codex Hooks
python3 install.py --target antigravity --target codex-hooks

# Or install for Claude Code / Cursor / Gemini CLI / all
python3 install.py --target claude-code
python3 install.py --target cursor
python3 install.py --target gemini-cli
python3 install.py --target all

# Or inspect ready-to-paste snippets without modifying files
python3 install.py --print-configs
```

> **Note for Codex Hooks**: After running `--target codex-hooks`, open Codex CLI or Desktop and run `/hooks` once to review and trust the `codex-bridge` hooks.

### Manual MCP Configuration (Any MCP Client)

Add to your agent's MCP config (`~/.gemini/config/mcp_config.json`, `~/.claude.json`, `~/.cursor/mcp.json`, `~/.gemini/settings.json`, etc.):

```json
{
  "mcpServers": {
    "codex-bridge": {
      "command": "python3",
      "args": ["/ABSOLUTE/PATH/TO/codex-bridge/src/codex_bridge/mcp_server.py"]
    }
  }
}
```

For Claude Code CLI:

```bash
claude mcp add --scope user codex-bridge -- python3 /ABSOLUTE/PATH/TO/codex-bridge/src/codex_bridge/mcp_server.py
```

---

## Exposed MCP Tools (15 Tools)

| Category | Tools |
| :--- | :--- |
| **Discovery** | `codex_capabilities` (models, reasoning efforts, permission profiles, hooks) |
| **Execution** | `codex_run`, `codex_send`, `codex_goal`, `codex_steer`, `codex_interrupt` |
| **Async & Results** | `codex_status`, `codex_wait`, `codex_get_result` |
| **History & Threads** | `codex_list_threads`, `codex_read_thread`, `codex_manage_thread` |
| **Projects** | `codex_projects` (`list` / `read` / `create` / `update` / `delete` / `move`) |
| **Two-Way Wakeup** | `codex_events` (Hooks & callbacks), `codex_reply_callback` (`ask_leader` / approvals) |

---

## Included Agent Skill (`skills/codex-leader/`)

Point any Agent Skills-compatible client at `skills/codex-leader/SKILL.md` (done automatically by `install.py`) to teach your Leader agent:
- When to pick `desktop_live` vs `headless_rpc`
- How to route models, reasoning efforts, permissions, and `/goal`
- How to correlate multi-step invocations with `leader_task_id` and report `codex://threads/<id>` links back to the user

---

## Standalone CLI

```bash
python3 src/codex_bridge/cli.py capabilities
python3 src/codex_bridge/cli.py run --cwd /path/to/repo --model gpt-6.2-sol --effort medium --prompt "Run tests and summarize"
python3 src/codex_bridge/cli.py send --thread <THREAD_ID> --prompt "Now fix the failing test"
python3 src/codex_bridge/cli.py threads --cwd /path/to/repo
python3 src/codex_bridge/cli.py projects list
python3 src/codex_bridge/cli.py events --ack
```

---

## Repository Structure

```text
codex-bridge/
├── install.py                         # Universal one-click installer (Antigravity, Claude Code, Cursor, Gemini CLI, Codex Hooks)
├── pyproject.toml                     # Zero-dependency Python 3.9+ package
├── configs/                           # Example MCP & Codex Hooks JSON configs
├── skills/codex-leader/               # Generic Leader Agent Skill + references
├── src/codex_bridge/
│   ├── config.py                      # Path & Codex binary discovery
│   ├── rpc.py                         # Thread-safe stdio & Unix WebSocket JSON-RPC clients
│   ├── ledger.py                      # SQLite Leader traceability & events ledger
│   ├── rollout.py                     # Read-only rollout JSONL parser & live tailer
│   ├── engine.py                      # Core desktop_live & headless_rpc orchestrator
│   ├── mcp_server.py                  # Zero-dependency MCP stdio server (15 tools)
│   ├── hook_bridge.py                 # Codex lifecycle Hook bridge
│   └── cli.py                         # Standalone CLI
└── tests/test_bridge.py               # Unit & MCP protocol smoke tests
```

## License

MIT