codex-codes
A typed Rust interface for the OpenAI Codex CLI app-server JSON-RPC protocol.
Part of the rust-code-agent-sdks workspace.
Overview
This crate provides type-safe Rust representations of the Codex CLI's JSON-RPC protocol, used by codex app-server. It includes optional sync and async clients for multi-turn conversations with the Codex agent.
Tested against: Codex CLI 0.104.0
Installation
Default (All Features)
Requires the Codex CLI (codex binary) to be installed and available in PATH.
Feature Flags
| Feature | Description | WASM-compatible |
|---|---|---|
types |
Core message types only (minimal dependencies) | Yes |
sync-client |
Synchronous client with blocking I/O | No |
async-client |
Asynchronous client with tokio runtime | No |
All features are enabled by default.
Types Only (WASM-compatible)
[]
= { = "0.100", = false, = ["types"] }
Sync Client Only
[]
= { = "0.100", = false, = ["sync-client"] }
Async Client Only
[]
= { = "0.100", = false, = ["async-client"] }
Usage
Async Client (Multi-Turn)
use ;
async
Sync Client (Multi-Turn)
use ;
Raw Protocol Access
use ;
// Parse exec-format JSONL events
let item_json = r#"{"type":"agent_message","id":"msg_1","text":"Hello!"}"#;
let item: ThreadItem = from_str.unwrap;
// Parse app-server JSON-RPC messages
let rpc_json = r#"{"id":1,"result":{"threadId":"th_abc"}}"#;
let msg: JsonRpcMessage = from_str.unwrap;
Protocol
The crate supports two protocol modes:
App-Server JSON-RPC (Primary)
The codex app-server --listen stdio:// process speaks a JSON-RPC 2.0 protocol (without the "jsonrpc":"2.0" field) over newline-delimited stdio.
Lifecycle: thread/start -> turn/start -> stream notifications -> turn/completed -> next turn/start
Approval flows: The server sends requests back to the client for command execution and file change approvals.
Exec JSONL (Legacy)
The codex exec --json - one-shot protocol emits ThreadEvent JSONL lines. These types are still available for parsing captures.
Types
JSON-RPC (jsonrpc module)
RequestId-- String or integer request identifierJsonRpcRequest,JsonRpcResponse,JsonRpcError,JsonRpcNotificationJsonRpcMessage-- Untagged union of all message types
Protocol (protocol module)
- Thread lifecycle:
ThreadStartParams/Response,ThreadArchiveParams/Response - Turn lifecycle:
TurnStartParams/Response,TurnInterruptParams/Response - Notifications:
TurnCompletedNotification,AgentMessageDeltaNotification, etc. - Approvals:
CommandExecutionApprovalParams/Response,FileChangeApprovalParams/Response UserInput,Turn,TurnStatus,ServerMessage
Items (ThreadItem)
Discriminated union of agent action items (shared between exec and app-server):
agent_message/agentMessage-- Text output from the modelreasoning-- Chain-of-thought reasoningcommand_execution/commandExecution-- Shell command with outputfile_change/fileChange-- File modificationsmcp_tool_call/mcpToolCall-- MCP tool invocationweb_search/webSearch-- Web search querytodo_list/todoList-- Task tracking listerror-- Error item
Events (ThreadEvent) -- Exec Format
thread.started,turn.started,turn.completed,turn.faileditem.started,item.updated,item.completederror
Compatibility
Tested against: Codex CLI 0.104.0
The crate version tracks the Codex CLI version. If you're using a different CLI version, please report whether it works at: https://github.com/meawoppl/rust-code-agent-sdks/issues
License
Apache-2.0. See LICENSE.