代码生成服务器

OpenAI Codex MCP Server 是一个将 OpenAI Codex CLI 封装为 MCP(模型上下文协议)服务器的工具,用于与 Claude 集成,提供代码生成、解释、调试、重构等功能。

  • by Tomatio13
  • Python
  • 本地部署
  • 4 星标
  • 更新于
在 GitHub 查看 Tomatio13/openai-codex-mcp

使用说明

OpenAI Codex MCP Server

Wraps OpenAI Codex CLI as an MCP (Model Context Protocol) server for integration with Claude.

🚀 特性

此MCP服务器提供以下工具:

  • codex_agent:可访问所有OpenAI Codex CLI功能的综合工具

    • 代码生成、解释、调试、重构
    • 安全分析、测试创建、文档生成
    • 多模式支持(图像输入)
    • 多AI提供商支持
    • 3级自动化模式(建议/自动编辑/全自动)
  • codex_interactive:启动互动会话的工具

🔌 支持的模式

  • stdio模式:使用标准输入/输出(适用于Claude Desktop)
  • SSE模式:使用服务器端事件(用于Web API)

📋 先决条件

  1. OpenAI Codex命令行界面:

    npm install -g @openai/codex
    
  2. Python 3.12或更高版本

  3. 环境设置:复制 .env.example 向 .env 并配置

🛠️ 安装

1.克隆存储库

git clone https://github.com/Tomatio13/openai-codex-mcp.git
cd openai-codex-mcp

2.设置

./setup.sh

3.启动服务器

stdio模式(适用于克劳德桌面)

./run.sh

SSE模式(适用于Web API)

./run_sse.sh [port] [host]

# Examples:
./run_sse.sh 8080 0.0.0.0  # Port 8080, listen on all interfaces
./run_sse.sh              # Default: localhost:8000

手动启动

# stdio mode
python codex_server.py --mode stdio

# SSE mode
python codex_server.py --mode sse --port 8000 --host localhost

或者运行安装程序并在一个命令中启动:

./setup_and_run.sh

手动安装

# Install dependencies
pip install -e .

# Start server
python codex_server.py

🔧 克劳德配置

stdio模式(推荐)

要将MCP服务器与Claude Desktop一起使用,请将以下内容添加到配置文件中:

macOS

~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "openai-codex": {
      "command": "python",
      "args": ["/path/to/openai-codex-mcp/codex_server.py", "--mode", "stdio"],
      "env": {
        "PATH": "/usr/local/bin:/usr/bin:/bin"
      }
    }
  }
}

视窗

%APPDATA%\Claude\claude_desktop_config.json:

{
  "mcpServers": {
    "openai-codex": {
      "command": "python",
      "args": ["C:\\path\\to\\openai-codex-mcp\\codex_server.py", "--mode", "stdio"],
      "env": {
        "PATH": "C:\\Program Files\\nodejs;C:\\Windows\\System32"
      }
    }
  }
}

SSE模式

在SSE模式下,您可以通过HTTP端点访问MCP服务器:

# Start server
./run_sse.sh 8000

# Available endpoints
# GET  http://localhost:8000/sse - SSE connection
# POST http://localhost:8000/message - Send message

💡 用法

在Claude Desktop中配置MCP服务器后,您可以使用以下工具:

基本代码生成

@openai-codex codex_agent prompt="Create a Python function to calculate Fibonacci numbers"

任务特定优化

@openai-codex codex_agent prompt="Explain this code" task_type="code-explanation"
@openai-codex codex_agent prompt="Fix the bug in utils.py" task_type="debugging" model="o4-preview"
@openai-codex codex_agent prompt="Check for security vulnerabilities" task_type="security"

自动化级别设置

# Suggestions only (default, safest)
@openai-codex codex_agent prompt="Refactor this code" approval_mode="suggest"

# Auto-edit (automatic file read/write, command execution requires confirmation)
@openai-codex codex_agent prompt="Explain this codebase" approval_mode="auto-edit"

# Full auto (fully automatic execution in network-disabled sandbox)
@openai-codex codex_agent prompt="Create and run tests" approval_mode="full-auto"

多模式(图像输入)

@openai-codex codex_agent prompt="Implement this UI design" images=["design.png"] task_type="code-generation"

使用不同的AI提供商

@openai-codex codex_agent prompt="Review this code" provider="azure" model="gpt-4.1"
@openai-codex codex_agent prompt="Explain this" provider="ollama" model="llama3"

交互式会话

@openai-codex codex_interactive initial_prompt="Tell me about the project structure" approval_mode="auto-edit"

🎯 任务类型

使用 task_type 用于生成针对特定任务优化的提示的参数:

  • general:常规编码辅助(默认)
  • code-generation:生成新代码
  • code-explanation:解释现有代码
  • debugging:查找并修复错误
  • refactoring:改进代码结构
  • testing:编写或修复测试
  • security:安全分析和修复
  • documentation:生成或改进文档

🤖 AI提供商

通过 provider 参数:

  • openai:OpenAI(默认)
  • azure:Azure OpenAI
  • gemini:谷歌双子座
  • ollama:Olama(当地模特)
  • mistral:米斯特拉尔AI
  • deepseek:DeepSeek
  • xai:xAI
  • **groq**希腊:

🔒 审批模式

  • suggest:仅建议更改,所有操作都需要批准(最安全)
  • auto-edit:可以自动读写文件,要求shell命令
  • full-auto:完全自主,禁用网络沙盒(最强大)

🛡️ 安全

  • 这 full-auto 出于安全考虑,模式在禁用网络的沙盒中运行
  • 文件操作仅限于当前工作目录
  • 所有shell命令都需要明确批准,除非 full-auto 模式

🐛 故障排除

常见问题

  1. “找不到codex命令”

    npm install -g @openai/codex
    
  2. 脚本权限被拒绝

    chmod +x *.sh
    
  3. Python版本问题

    • 确保已安装Python 3.12+
    • 使用虚拟环境: python3 -m venv venv && source venv/bin/activate
  4. MCP连接问题

    • 检查Claude Desktop配置文件语法
    • 验证文件路径是否为绝对路径
    • 配置更改后重新启动Claude Desktop

📝 许可证

麻省理工学院署名许可证-见 许可证 文件以获取详细信息。

🤝 贡献

欢迎投稿!请随时提交拉取请求。

📞 支持

如果您遇到任何问题或有疑问,请在GitHub上打开问题。


Made with ❤️ for the Claude + OpenAI Codex community