codex-wrapper
A type-safe Codex CLI wrapper for Rust
Overview
codex-wrapper provides a type-safe builder-pattern interface for invoking the
codex CLI programmatically. It follows the same design philosophy as
claude-wrapper and
docker-wrapper: each CLI
subcommand is a builder struct that produces typed output.
Installation
Quick Start
use ;
async
Two-Layer Builder Architecture
The Codex client holds shared configuration (binary path, environment,
timeout, retry policy). Command builders hold per-invocation options and call
execute(&codex).
Codex Client
Configure once, reuse across commands:
let codex = builder
.env
.timeout_secs
.retry
.build?;
Options:
binary()-- path tocodexbinary (auto-detected viaPATHby default)working_dir()-- working directory for commandsenv()/envs()-- environment variablestimeout_secs()/timeout()-- command timeoutconfig()-- global config overrides (-c key=value)enable()/disable()-- global feature flagsretry()-- default retry policy
Command Builders
Each CLI subcommand is a separate builder. Available commands:
| Command | CLI Equivalent | Description |
|---|---|---|
ExecCommand |
codex exec |
Run Codex non-interactively |
ExecResumeCommand |
codex exec resume |
Resume a non-interactive session |
ReviewCommand |
codex exec review |
Code review with git integration |
ResumeCommand |
codex resume |
Resume an interactive session |
ForkCommand |
codex fork |
Fork an interactive session |
LoginCommand |
codex login |
Authenticate |
LoginStatusCommand |
codex login status |
Check auth status |
LogoutCommand |
codex logout |
Remove credentials |
McpListCommand |
codex mcp list |
List MCP servers |
McpGetCommand |
codex mcp get |
Get MCP server details |
McpAddCommand |
codex mcp add |
Add stdio or HTTP MCP server |
McpRemoveCommand |
codex mcp remove |
Remove MCP server |
McpLoginCommand |
codex mcp login |
Auth to MCP server |
McpLogoutCommand |
codex mcp logout |
Deauth from MCP server |
McpServerCommand |
codex mcp-server |
Start Codex as MCP server |
SandboxCommand |
codex sandbox |
Run command in sandbox |
ApplyCommand |
codex apply |
Apply agent diff |
CompletionCommand |
codex completion |
Generate shell completions |
FeaturesListCommand |
codex features list |
List feature flags |
FeaturesEnableCommand |
codex features enable |
Enable a feature |
FeaturesDisableCommand |
codex features disable |
Disable a feature |
VersionCommand |
codex --version |
Get CLI version |
RawCommand |
(any) | Escape hatch for arbitrary args |
ExecCommand: The Workhorse
Full coverage of codex exec options:
let output = new
.model
.sandbox
.approval_policy
.skip_git_repo_check
.ephemeral
.json
.execute
.await?;
All ExecCommand options:
| Method | CLI Flag | Description |
|---|---|---|
model() |
--model |
Model to use |
sandbox() |
--sandbox |
Sandbox policy |
approval_policy() |
--ask-for-approval |
Approval policy |
profile() |
--profile |
Config profile |
full_auto() |
--full-auto |
Auto sandbox + approval |
dangerously_bypass_approvals_and_sandbox() |
--dangerously-bypass-approvals-and-sandbox |
Skip all safety |
cd() |
--cd |
Working directory |
skip_git_repo_check() |
--skip-git-repo-check |
Run outside git repo |
add_dir() |
--add-dir |
Additional writable dirs |
search() |
--search |
Enable web search |
ephemeral() |
--ephemeral |
Don't persist session |
output_schema() |
--output-schema |
JSON Schema for response |
color() |
--color |
Color output mode |
progress_cursor() |
--progress-cursor |
Cursor-based progress |
json() |
--json |
JSONL event output |
output_last_message() |
--output-last-message |
Write last message to file |
image() |
--image |
Attach image(s) |
config() |
-c |
Config override |
enable() / disable() |
--enable / --disable |
Feature flags |
oss() |
--oss |
Use local OSS provider |
local_provider() |
--local-provider |
Specify lmstudio/ollama |
retry() |
(client-side) | Per-command retry policy |
JSONL Output Parsing
Use execute_json_lines() to parse structured events from --json mode:
let events = new
.ephemeral
.execute_json_lines
.await?;
for event in &events
Event types include thread.started, turn.started, item.completed,
turn.completed, and more.
Code Review
// Review uncommitted changes
let output = new
.uncommitted
.model
.execute
.await?;
// Review against a base branch
let output = new
.base
.json
.execute
.await?;
MCP Server Management
// List servers
let output = new.execute.await?;
// List as JSON
let servers = new.execute_json.await?;
// Add stdio server
stdio
.arg
.env
.execute
.await?;
// Add HTTP server
http
.bearer_token_env_var
.execute
.await?;
// Remove server
new.execute.await?;
Sandbox Execution
Run commands inside the Codex sandbox:
let output = new
.arg
.execute
.await?;
Session Management
// Resume the most recent interactive session
new
.last
.model
.execute
.await?;
// Fork a session to try a different approach
new
.session_id
.prompt
.execute
.await?;
Shell Completions
let output = new
.shell
.execute
.await?;
write?;
Feature Flags
// List all feature flags
new.execute.await?;
// Enable/disable features persistently
new.execute.await?;
new.execute.await?;
Escape Hatch: RawCommand
For subcommands or flags not yet covered by typed builders:
let output = new
.arg
.execute
.await?;
Error Handling
All commands return Result<T>, with errors typed via thiserror:
use Error;
match new.execute.await
Retry Policy
Configure automatic retries for transient failures:
use RetryPolicy;
use Duration;
let policy = new
.max_attempts
.initial_backoff
.exponential
.retry_on_timeout
.retry_on_exit_codes;
// Set on the client (applies to all commands)
let codex = builder.retry.build?;
// Or override per-command
let output = new
.retry
.execute
.await?;
Features
Optional Cargo features (enabled by default):
json-- JSONL output parsing viaserde_json(execute_json_lines(),execute_json(),JsonLineEvent)
Testing
License
MIT OR Apache-2.0