openai codex mcp
一个MCP服务器,用于包装OpenAI Codex CLI工具,以便与Claude Code一起使用。
概述
该项目提供了一个简单的JSON-RPC服务器,允许Claude Code与OpenAI Codex CLI工具进行交互。这使得Claude Code能够在需要时使用OpenAI的模型进行代码生成、解释和解决问题。
视频演示
https://www.loom.com/share/5d9532a79ae24b309af08c1727156f9c?sid=d9277b91-bf8c-43ec-a3c6-dc69fc52e1d2
最近的改进
MCP服务器已得到增强,可提供更直观、更强大的界面:
- 专业方法:为常见编码任务添加了专用方法:
- write_code:用指定语言为特定任务生成代码 - explain_code:获取代码工作原理的详细解释 - debug_code:查找并修复有问题代码中的错误
- 模型选择:具有默认值的明确定义的模型选项:
- o4迷你 - o4预览 - o4-pro - o4最新
- 简化语法:更直观的参数命名和结构,便于集成
这些改进使Claude Code更容易将OpenAI的模型用于特定的编程任务,而不需要复杂的提示工程。
先决条件
- Python 3.12+
- OpenAI Codex命令行界面工具(
npm install -g @openai/codex) - 有效的OpenAI API密钥(适用于Codex CLI,不适用于此服务器)
安装和设置
本项目使用PEP‑621 pyproject.toml请按照以下步骤操作:
# 1. Create & activate a venv
uv venv # creates a .venv/ directory
source .venv/bin/activate # on Windows: .\.venv\Scripts\activate
# 2. Install package and dependencies into the venv
uv pip install .安装后 codex_server 入口点在您的PATH中可用。
运行服务器
快速开始
使用提供的安装脚本自动设置环境并启动服务器:
./setup_and_run.sh此脚本将:
- 检查
codexCLI安装 - 如果需要,设置Python虚拟环境
- 如果可用,通过Claude CLI安装MCP工具
- 启动MCP服务器
手动启动
如果您更喜欢手动启动服务器:
- 确保Codex CLI已安装并使用OpenAI API密钥正确配置
- 启动服务器:
codex_server*或者,直接使用uvicorn:* uvicorn codex_server:app
与Claude Code集成
MCP服务器运行后,您可以使用以下任一方法向Claude Code注册它:
方法1:使用Claude CLI(推荐)
该存储库包含一个配置文件,可以使用Claude CLI直接安装:
# Install the MCP tool directly from the JSON config
claude mcp add /path/to/openai_codex_mcp.json
# Verify the tool was installed correctly
claude mcp list方法2:通过UI手动配置
或者,您可以手动注册该工具:
- 在Claude Code中,导航到 设置→ 工具→ 管理MCP工具.
- 使用以下工具创建新工具:
- 名字: openai_codex - 描述: OpenAI Codex CLI integration via MCP server - 基本URL: http://localhost:8000/ - 协议: JSON-RPC 2.0 - 认证: _留空_
- 保存该工具。
现在,Claude Code可以将OpenAI Codex CLI工具用于需要不同视角或方法的任务。您可以通过让Claude为特定任务使用OpenAI模型来调用它。
API使用
MCP服务器为不同的编码任务提供了多种与OpenAI模型交互的方法。
方法1:总体竣工
对于OpenAI的灵活自定义提示:
curl -X POST http://localhost:8000/ \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"method": "codex_completion",
"params": {
"prompt": "Write a JavaScript function to sort an array of objects by a property value",
"model": "o4-mini"
},
"id": 1
}'方法2:编写代码
使用语言规范生成代码的专门方法:
curl -X POST http://localhost:8000/ \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"method": "write_code",
"params": {
"task": "Calculate the first 100 Fibonacci numbers and return them as an array",
"language": "python",
"model": "o4-mini"
},
"id": 1
}'方法3:解释代码
代码解释的专门方法:
curl -X POST http://localhost:8000/ \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"method": "explain_code",
"params": {
"code": "def quicksort(arr):\n if len(arr) pivot]\n return quicksort(left) + middle + quicksort(right)",
"model": "o4-mini"
},
"id": 1
}'方法4:调试代码
查找和修复错误的专门方法:
curl -X POST http://localhost:8000/ \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"method": "debug_code",
"params": {
"code": "function fibonacci(n) {\n if (n <= 0) return [];\n if (n === 1) return [1];\n let sequence = [1, 1];\n for (let i = 2; i <= n; i++) {\n sequence.push(sequence[i-2] + sequence[i-1]);\n }\n return sequence;\n}",
"issue_description": "It generates one too many Fibonacci numbers",
"model": "o4-mini"
},
"id": 1
}'可用型号
推理模型(O系列)
o4-mini:更快、更实惠的推理模型o3:最强大的推理模型o3-mini:o3的小型替代品o1:以前的全o系列推理模型o1-mini:o1的小型替代品o1-pro:o1版本具有更多计算能力,可获得更好的响应
GPT模型
gpt-4.1:复杂任务的旗舰GPT模型gpt-4o:快速、智能、灵活的GPT模型gpt-4.1-mini:智能、速度和成本平衡gpt-4.1-nano:最快、最具成本效益的GPT-4.1型号gpt-4o-mini:用于集中任务的快速、经济实惠的小型模型
可用参数
全面竣工(代码x竣工)
prompt(必填):发送给食品法典委员会的提示model(可选):要使用的型号(例如,“o4-mini”、“o3”、“gpt-4.1”、“gpt-4o-mini”)images(可选):要包含的图像路径或数据URI列表additional_args(可选):要传递给Codex的其他CLI参数
写代码(Write_Code)
task(必填):编码任务的描述language(必填):解决方案的编程语言(例如“python”、“javascript”、“java”)model(可选):要使用的模型
解释代码(Explain_Code)
code(必填):解释代码model(可选):要使用的模型
调试代码(Debug_Code)
code(必填):要调试的代码issue_description(可选):问题或错误的描述model(可选):要使用的模型
使用Claude代码的示例用法
配置后,您可以要求Claude使用任何可用方法将OpenAI Codex用于特定任务:
使用write_code方法
User: Can you use OpenAI Codex to write a Python function that generates prime numbers?
Claude: I'll use the OpenAI Codex write_code method to generate that function.
[Claude would use the write_code method with task="Write a Python function that generates prime numbers" and language="python"]使用explain_code方法
User: Can you ask OpenAI Codex to explain how this quicksort algorithm works?
Claude: I'll use OpenAI Codex to explain this algorithm.
[Claude would use the explain_code method with the code provided]使用debug_code方法
User: My JavaScript function has a bug. Can you use OpenAI Codex to debug it?
Claude: I'll ask OpenAI Codex to find and fix the bug in your code.
[Claude would use the debug_code method with the code provided]指定模型
User: Can you use OpenAI Codex with the o4-preview model to write an efficient implementation of a binary search tree?
Claude: I'll use the o4-preview model to generate that implementation.
[Claude would use the specified model with the appropriate method]这允许您在同一个界面中无缝利用Claude和OpenAI的功能,并为常见的编码任务提供专门的方法。
