OpenAI Codex MCP Documentation: CLI Guide
Master OpenAI Codex CLI Model Context Protocol configuration: config.toml syntax, codex mcp subcommands, JSON-RPC debugging, and runtime troubleshooting.
Generate & Validate Multi-Client MCP Config
One-click export with environment variables & path locators for Claude Desktop, Cursor, Windsurf, and OpenAI Codex CLI.
OpenAI Codex MCP Documentation: CLI Guide
The OpenAI Codex CLI provides command-line artificial intelligence assistance for software engineering workflows, including autonomous test execution, code refactoring, and multi-file debugging. By natively integrating the Model Context Protocol (MCP), the Codex CLI connects language model reasoning directly to local databases, git version control, external cloud APIs, and specialized development tools.
Unlike graphical desktop hosts that rely on background electron processes, the Codex CLI operates inside terminal sessions and continuous integration runners. This requires a dedicated approach to subprocess management, standard input and output stream isolation, and configuration schemas.
This guide provides an authoritative technical reference for the OpenAI Codex CLI Model Context Protocol integration, detailing the config.toml specification, command-line operations, JSON-RPC message flows, and troubleshooting procedures.
1. Protocol Architecture and Subprocess Mechanics
When the OpenAI Codex CLI initializes, it parses server definitions declared in its global configuration file. For each enabled server, the Codex runtime spawns an independent child process communicating over Standard Input and Output (stdio).
Subprocess Lifecycle and Isolation Rules
- ▸Strict Stdio Stream Hygiene: The Codex CLI uses standard input (
stdin) to deliver JSON-RPC requests and standard output (stdout) to receive JSON-RPC responses. Any diagnostic printing, status logging, or interactive confirmation prompt directed to standard output corrupts the framing buffer and breaks the connection. - ▸Standard Error Routing: Diagnostic messages from child processes are captured from standard error (
stderr) and forwarded to the internal Codex logging subsystem. - ▸Graceful Signal Handling: When a terminal user terminates Codex (via
SIGINTorCtrl+C), the CLI runtime sendsSIGTERMsignals to all active child MCP server processes to prevent orphaned background tasks.
2. Environment Prerequisites and Installation
The Codex CLI can invoke MCP servers authored in TypeScript (via Node.js and npx), Python (via uvx and python), or compiled system binaries. Ensure the following prerequisites are installed on your host system:
| Prerequisite | Minimum Version | Supported Platforms |
|---|---|---|
| OpenAI Codex CLI | >= 0.14.0 | macOS, Linux, Windows |
| Node.js / npx | >= 20.0.0 | Required for Node.js MCP packages |
| Python / uv | >= 3.10 / uv >= 0.4.0 | Required for Python-based servers |
| Docker | >= 24.0.0 | Optional, for containerized server sandboxing |
Verify your active Codex CLI installation:
codex --versionConfiguration files reside in the user home directory:
- ▸macOS / Linux:
~/.codex/config.toml - ▸Windows PowerShell:
$HOME\.codex\config.toml(or%USERPROFILE%\.codex\config.toml)
3. Configuration Specification (config.toml)
Codex CLI stores MCP registrations in TOML format under the [mcp_servers.<name>] table.
Schema Fields Reference
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
command | String | Yes | — | Absolute path or PATH-resolvable executable binary (node, npx, python, uvx). |
args | Array | No | [] | Positional command-line arguments passed to the server binary. |
env | Table | No | {} | Key-value table of environment variables injected into the child process. |
disabled | Boolean | No | false | When true, disables server initialization without removing configuration. |
Complete config.toml Example
# ~/.codex/config.toml
# 1. GitHub Integration (Node.js via npx)
[mcp_servers.github]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-github"]
[mcp_servers.github.env]
GITHUB_PERSONAL_ACCESS_TOKEN = "ghp_your_personal_access_token_here"
# 2. Local PostgreSQL Database Connection
[mcp_servers.postgres]
command = "npx"
args = [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://appuser:secretpassword@/production_analytics"
]
# 3. SQLite Database (Python via fast uvx runner)
[mcp_servers.sqlite]
command = "uvx"
args = ["mcp-server-sqlite", "--db-path", "./data/analytics.db"]
# 4. Brave Search Integration
[mcp_servers.brave_search]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-brave-search"]
[mcp_servers.brave_search.env]
BRAVE_API_KEY = "BSA_your_brave_api_key_here"4. Command-Line Interface Reference
The Codex CLI provides a dedicated codex mcp subcommand suite for managing registered servers without manually editing TOML files.
1. codex mcp list
Displays all configured servers, transport modes, connection statuses, and tool counts:
codex mcp listExample Terminal Output:
SERVER NAME TRANSPORT STATUS TOOLS RESOURCES
github stdio Connected 7 0
postgres stdio Connected 1 2
sqlite stdio Connected 2 1
brave_search stdio Connected 2 02. codex mcp add
Appends a new server definition to config.toml:
codex mcp add --name fetch --command "uvx" --args "mcp-server-fetch"To include environment variables:
codex mcp add --name slack --command "npx -y @modelcontextprotocol/server-slack" --env SLACK_BOT_TOKEN=xoxb-xxx3. codex mcp tools <server_name>
Inspects tool schemas, input parameters, and descriptions registered by an active server:
codex mcp tools postgres4. codex mcp restart <server_name>
Restarts a crashed server or reloads after updating environment secrets without terminating your active Codex session:
codex mcp restart github5. codex mcp logs <server_name>
Streams the standard error (stderr) diagnostic log of a specific child process:
codex mcp logs postgres5. JSON-RPC 2.0 Protocol Message Traces
During interactive execution, Codex exchanges JSON-RPC 2.0 messages with its child processes. The following frames demonstrate a standard lifecycle trace.
Client Initialization (initialize)
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {
"roots": { "listChanged": true },
"sampling": {}
},
"clientInfo": {
"name": "openai-codex-cli",
"version": "0.14.2"
}
}
}Tool Discovery (tools/list)
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/list",
"params": {}
}Tool Execution Request (tools/call)
{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "read_query",
"arguments": {
"query": "SELECT COUNT(*) FROM users WHERE is_active = true;"
}
}
}6. Common Errors and Production Troubleshooting
When operating MCP servers within the OpenAI Codex CLI, engineers commonly encounter the following operational issues:
Error: spawn npx ENOENT
- ▸Symptom: Codex logs indicate child process creation failure immediately upon launch.
- ▸Root Cause: The terminal environment executing Codex does not possess
npxin its defaultPATH, common when invoking Codex from non-login shells or desktop shortcuts. - ▸Resolution: Provide the absolute path to the executable binary in
config.toml:- ▸macOS / Linux:
command = "/opt/homebrew/bin/npx"orcommand = "/usr/local/bin/npx" - ▸Windows PowerShell:
command = "C:\\Program Files\\nodejs\\npx.cmd"
- ▸macOS / Linux:
Error: Silent Process Stall (Hanging on Startup)
- ▸Symptom: Codex displays "Connecting..." indefinitely and fails to discover registered tools.
- ▸Root Cause: The command invoked prompted the user for terminal input (e.g.
npxasking "Need to install the following packages: ok to proceed? (y)"). Because standard input is bound to the JSON-RPC channel, the process hangs awaiting user keystrokes. - ▸Resolution: Always pass the non-interactive automatic approval flag
-ywhen invokingnpx:
args = ["-y", "@modelcontextprotocol/server-github"]Error: Unicode Decode Error on Windows (CP1252 vs UTF-8)
- ▸Symptom: Python servers fail on Windows with
UnicodeDecodeError: 'charmap' codec can't decode byte. - ▸Resolution: Force Python to use UTF-8 encoding for standard streams by adding
PYTHONUTF8 = "1"to the server environment table:
[mcp_servers.python_server.env]
PYTHONUTF8 = "1"7. External Citations and Security Guardrails
Running terminal-level coding agents with external tool integrations requires strict security boundaries:
- ▸Least-Privilege Database Access: Connect database servers using dedicated read-only credentials. Never provide administrative or
DROP TABLEpermissions to interactive coding assistants. - ▸Token Storage: Keep personal access tokens out of shared source repositories. Store tokens in local environment variables and reference them securely.
- ▸Official Specifications and Repositories:
Weekly Protocol Diffs & Verified Drops
New server guides, benchmarks, and security audits. No spam. Unsubscribe anytime.
Zero marketing spam. 1-click unsubscribe anytime.
Build your full agent toolstack in the Visual Generator
Combine OpenAI Codex MCP Documentation: CLI Guide with databases, search APIs, and memory graphs in a single configuration file.
Did this setup guide work with your AI host?
Help test this configuration across Claude, Cursor, and Codex CLI. Be the first to report:
OpenAI Codex MCP Documentation: CLI Guide FAQ
What is the OpenAI Codex MCP Documentation: CLI Guide?
Master OpenAI Codex CLI Model Context Protocol configuration: config.toml syntax, codex mcp subcommands, JSON-RPC debugging, and runtime troubleshooting.
How do I configure OpenAI Codex MCP Documentation: CLI Guide in Claude Desktop or Cursor?
You can copy the configuration JSON from our guide or launch the interactive MCP Codex Config Generator at https://mcp-codex.com/generator to export valid configs in 1 click.
Can I use OpenAI Codex MCP Documentation: CLI Guide with the OpenAI Codex CLI?
Yes, OpenAI Codex CLI supports Model Context Protocol. You can add it directly to ~/.codex/config.toml or pass arguments to codex mcp add.
Specializing in Model Context Protocol (MCP) integrations, autonomous AI agent orchestration, and distributed developer toolchains. Researches and benchmarks production MCP client-server architectures across OpenAI Codex, Claude, and Cursor.
Related Guides
OpenCode Free AI Agent Guide
Guide to running OpenCode coding agent completely free without API keys using Zen free tier, local LLMs, and OAuth accounts.
Dev ToolsSelf-Correcting Verification Loops in Agents
Build robust self-correcting /loop verification harnesses for AI agents using Test-Driven Agent Development, dual-agent critics, and MCP test sandboxes.
Dev ToolsAutomated Doc Verification Loops with Claude CLI
Build automated documentation verification loops with Claude CLI and MCP. Validate code snippets, test tool schemas, and sync SDK docs in CI/CD. Verified.