Codex Docs 高级功能
English

高级功能

跟踪 / 详细日志记录

因为 Codex 是用 Rust 编写的,它尊重 RUST_LOG 环境变量来配置其日志记录行为。

TUI 默认为 RUST_LOG=codex_core=info,codex_tui=info,日志消息写入 ~/.codex/log/codex-tui.log,所以你可以在单独的终端中运行以下命令来监控写入的日志消息:

tail -F ~/.codex/log/codex-tui.log

相比之下,非交互模式(codex exec)默认为 RUST_LOG=error,但消息是内联打印的,所以不需要监控单独的文件。

有关配置选项的更多信息,请参阅有关 RUST_LOG 的 Rust 文档。

模型上下文协议 (MCP)

Codex CLI 可以通过在 ~/.codex/config.toml 中定义 mcp_servers 部分来配置利用 MCP 服务器。它旨在镜像 Claude 和 Cursor 等工具在其各自的 JSON 配置文件中定义 mcpServers 的方式,尽管 Codex 格式略有不同,因为它使用 TOML 而不是 JSON,例如:

# 重要:顶级键是 `mcp_servers` 而不是 `mcpServers`。
[mcp_servers.server-name]
command = "npx"
args = ["-y", "mcp-server"]
env = { "API_KEY" = "value" }

将 Codex 用作 MCP 服务器

Codex CLI 也可以通过 codex mcp-server 作为 MCP _服务器_运行。例如,你可以使用 codex mcp-server 使 Codex 在 OpenAI Agents SDK 等多代理框架中作为工具可用。单独使用 codex mcp 在你的配置中添加/列表/获取/删除 MCP 服务器启动器。

Codex MCP 服务器快速入门

你可以使用 模型上下文协议检查器 启动 Codex MCP 服务器:

npx @modelcontextprotocol/inspector codex mcp-server

发送 tools/list 请求,你将看到有两个可用工具:

codex - 运行 Codex 会话。接受与 Codex Config 结构匹配的配置参数。codex 工具采用以下属性:

属性 类型 描述
prompt(必需) string 启动 Codex 对话的初始用户提示。
approval-policy string 模型生成的 shell 命令的批准策略:untrustedon-failurenever
base-instructions string 要使用的一组指令,而不是默认的指令。
config object 单个配置设置,将覆盖 $CODEX_HOME/config.toml 中的内容。
cwd string 会话的工作目录。如果是相对路径,则相对于服务器进程的当前目录解析。
include-plan-tool boolean 是否在对话中包含计划工具。
model string 模型名称的可选覆盖(例如 o3o4-mini)。
profile string 来自 config.toml 的配置文件,用于指定默认选项。
sandbox string 沙盒模式:read-onlyworkspace-writedanger-full-access

codex-reply - 通过提供对话 id 和提示继续 Codex 会话。codex-reply 工具采用以下属性:

属性 类型 描述
prompt(必需) string 继续 Codex 对话的下一个用户提示。
conversationId(必需) string 要继续的对话的 id。

尝试使用

[!TIP] Codex 通常需要几分钟来运行。为了适应这一点,请在 ⛭ 配置下将 MCP 检查器的请求和总超时调整为 600000ms(10 分钟)。

使用 MCP 检查器和 codex mcp-server 使用以下设置构建一个简单的井字棋游戏:

approval-policy: never

prompt: 使用 HTML、Javascript 和 CSS 实现一个简单的井字棋游戏。将游戏写在名为 index.html 的单个文件中。

sandbox: workspace-write

点击"运行工具",你应该会看到在构建游戏时从 Codex MCP 服务器发出的事件列表。