Agents SDK와 함께 Codex 사용하기
Codex를 MCP 서버로 실행하기
Codex를 MCP 서버로 실행하고 다른 MCP 클라이언트에서 연결할 수 있습니다. Codex를 MCP 서버로 시작하려면 다음 명령을 사용합니다.
codex mcp-server
Model Context Protocol Inspector로 Codex MCP 서버를 실행할 수도 있습니다.
npx @modelcontextprotocol/inspector codex mcp-server
tools/list 요청을 보내면 두 도구가 표시됩니다.
codex: Codex 세션을 실행합니다. Codex Config 구조체와 일치하는 구성 매개변수를 받습니다.
| 속성 | 타입 | 설명 |
|---|---|---|
prompt 필수 | string | Codex 대화를 시작할 초기 사용자 프롬프트 |
approval-policy | string | 모델이 생성한 shell 명령 승인 정책: untrusted, on-request, never |
base-instructions | string | 기본 지침 대신 사용할 지침 집합 |
config | object | $CODEX_HOME/config.toml 값을 오버라이드하는 개별 구성 설정 |
cwd | string | 세션 작업 디렉터리. 상대 경로면 서버 프로세스의 현재 디렉터리 기준으로 해석 |
include-plan-tool | boolean | 대화에 plan 도구를 포함할지 여부 |
model | string | 모델 이름 선택 오버라이드, 예: o3, o4-mini |
profile | string | 기본 옵션을 지정할 config.toml 구성 프로필 |
sandbox | string | 샌드박스 모드: read-only, workspace-write, danger-full-access |
codex-reply: thread ID와 프롬프트를 제공해 Codex 세션을 계속합니다.
| 속성 | 타입 | 설명 |
|---|---|---|
prompt 필수 | string | Codex 대화를 계속할 다음 사용자 프롬프트 |
threadId 필수 | string | 계속할 스레드 ID |
conversationId deprecated | string | threadId의 deprecated alias, 호환성을 위해 유지 |
tools/call 응답의 structuredContent.threadId에서 threadId를 가져옵니다. 승인 prompt(exec/patch)도 params payload에 threadId를 포함합니다.
응답 payload 예시:
{
"structuredContent": {
"threadId": "019bbb20-bff6-7130-83aa-bf45ab33250e",
"content": "`ls -lah` (or `ls -alh`) — long listing, includes dotfiles, human-readable sizes."
},
"content": [
{
"type": "text",
"text": "`ls -lah` (or `ls -alh`) — long listing, includes dotfiles, human-readable sizes."
}
]
}
현대 MCP 클라이언트는 보통 도구 호출 결과로 "structuredContent"만 보고하지만, Codex MCP 서버는 오래된 MCP 클라이언트를 위해 "content"도 반환합니다.
멀티 에이전트 워크플로 만들기
Codex CLI는 임시 작업 실행을 넘어 더 많은 일을 할 수 있습니다. CLI를 Model Context Protocol 서버로 노출하고 OpenAI Agents SDK로 오케스트레이션하면, 단일 에이전트에서 전체 소프트웨어 전달 파이프라인까지 확장되는 결정적이고 검토 가능한 워크플로를 만들 수 있습니다.
이 가이드는 OpenAI Cookbook에 소개된 워크플로와 같은 흐름을 설명합니다.
여기서 수행할 작업:
- Codex CLI를 장기 실행 MCP 서버로 시작합니다.
- 플레이 가능한 브라우저 게임을 만드는 집중 단일 에이전트 워크플로를 만듭니다.
- handoff, guardrail, 전체 trace를 갖춘 멀티 에이전트 팀을 오케스트레이션합니다.
시작하기 전에 다음을 준비하십시오.
npx codex를 실행할 수 있도록 로컬에 Codex CLI 설치- Python 3.10 이상 및
pip - Node.js 18 이상(
npx에 필요) - 로컬에 저장된 OpenAI API 키. 키는 OpenAI dashboard에서 만들거나 관리할 수 있습니다.
가이드용 작업 디렉터리를 만들고 .env 파일에 API 키를 추가합니다.
mkdir codex-workflows
cd codex-workflows
printf "OPENAI_API_KEY=sk-..." > .env
의존성 설치
Agents SDK는 Codex, handoff, trace 간 오케스트레이션을 처리합니다. 최신 SDK 패키지를 설치합니다.
python -m venv .venv
source .venv/bin/activate
pip install --upgrade openai openai-agents python-dotenv
가상 환경을 활성화하면 SDK 의존성이 시스템의 다른 패키지와 분리됩니다.
Codex CLI를 MCP 서버로 초기화하기
먼저 Codex CLI를 Agents SDK가 호출할 수 있는 MCP 서버로 바꿉니다. 서버는 두 도구(codex()로 대화 시작, codex-reply()로 계속 진행)를 노출하고 여러 에이전트 턴에 걸쳐 Codex를 계속 실행합니다.
codex_mcp.py 파일을 만들고 다음 코드를 추가합니다.
import asyncio
from agents import Agent, Runner
from agents.mcp import MCPServerStdio
async def main() -> None:
async with MCPServerStdio(
name="Codex CLI",
params={
"command": "npx",
"args": ["-y", "codex", "mcp-server"],
},
client_session_timeout_seconds=360000,
) as codex_mcp_server:
print("Codex MCP server started.")
# More logic coming in the next sections.
return
if __name__ == "__main__":
asyncio.run(main())
스크립트를 한 번 실행해 Codex가 정상적으로 시작되는지 확인합니다.
python codex_mcp.py
스크립트는 Codex MCP server started.를 출력한 뒤 종료됩니다. 다음 섹션에서는 같은 MCP 서버를 더 풍부한 워크플로 안에서 재사용합니다.
단일 에이전트 워크플로 만들기
작은 브라우저 게임을 만드는 범위가 좁은 예시부터 시작합니다. 이 워크플로는 두 에이전트를 사용합니다.
- Game Designer: 게임 brief를 작성합니다.
- Game Developer: Codex MCP를 호출해 게임을 구현합니다.
codex_mcp.py를 다음 코드로 갱신합니다. 위 MCP 서버 설정을 유지하면서 두 에이전트를 추가합니다.
import asyncio
import os
from dotenv import load_dotenv
from agents import Agent, Runner, set_default_openai_api
from agents.mcp import MCPServerStdio
load_dotenv(override=True)
set_default_openai_api(os.getenv("OPENAI_API_KEY"))
async def main() -> None:
async with MCPServerStdio(
name="Codex CLI",
params={
"command": "npx",
"args": ["-y", "codex", "mcp-server"],
},
client_session_timeout_seconds=360000,
) as codex_mcp_server:
developer_agent = Agent(
name="Game Developer",
instructions=(
"You are an expert in building simple games using basic html + css + javascript with no dependencies. "
"Save your work in a file called index.html in the current directory. "
"Always call codex with \"approval-policy\": \"never\" and \"sandbox\": \"workspace-write\"."
),
mcp_servers=[codex_mcp_server],
)
designer_agent = Agent(
name="Game Designer",
instructions=(
"You are an indie game connoisseur. Come up with an idea for a single page html + css + javascript game that a developer could build in about 50 lines of code. "
"Format your request as a 3 sentence design brief for a game developer and call the Game Developer coder with your idea."
),
model="gpt-5",
handoffs=[developer_agent],
)
await Runner.run(designer_agent, "Implement a fun new game!")
if __name__ == "__main__":
asyncio.run(main())
스크립트를 실행합니다.
python codex_mcp.py
Codex는 designer의 brief를 읽고 index.html 파일을 생성한 뒤 전체 게임을 디스크에 씁니다. 생성된 파일을 브라우저로 열어 결과를 플레이할 수 있습니다. 실행할 때마다 다른 디자인과 플레이 방식이 만들어집니다.
멀티 에이전트 워크플로로 확장하기
이제 단일 에이전트 설정을 오케스트레이션되고 trace 가능한 워크플로로 확장합니다. 시스템은 다음 역할을 추가합니다.
- Project Manager: 공유 요구 사항을 만들고 handoff를 조율하며 guardrail을 강제합니다.
- Designer, Frontend Developer, Server Developer, Tester: 각자 범위가 정해진 지침과 출력 폴더를 갖습니다.
multi_agent_workflow.py 파일을 새로 만듭니다.
import asyncio
import os
from dotenv import load_dotenv
from agents import (
Agent,
ModelSettings,
Runner,
WebSearchTool,
set_default_openai_api,
)
from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX
from agents.mcp import MCPServerStdio
from openai.types.shared import Reasoning
load_dotenv(override=True)
set_default_openai_api(os.getenv("OPENAI_API_KEY"))
async def main() -> None:
async with MCPServerStdio(
name="Codex CLI",
params={"command": "npx", "args": ["-y", "codex", "mcp"]},
client_session_timeout_seconds=360000,
) as codex_mcp_server:
designer_agent = Agent(
name="Designer",
instructions=(
f"""{RECOMMENDED_PROMPT_PREFIX}"""
"You are the Designer.\n"
"Your only source of truth is AGENT_TASKS.md and REQUIREMENTS.md from the Project Manager.\n"
"Do not assume anything that is not written there.\n\n"
"You may use the internet for additional guidance or research."
"Deliverables (write to /design):\n"
"- design_spec.md – a single page describing the UI/UX layout, main screens, and key visual notes as requested in AGENT_TASKS.md.\n"
"- wireframe.md – a simple text or ASCII wireframe if specified.\n\n"
"Keep the output short and implementation-friendly.\n"
"When complete, handoff to the Project Manager with transfer_to_project_manager."
"When creating files, call Codex MCP with {\"approval-policy\":\"never\",\"sandbox\":\"workspace-write\"}."
),
model="gpt-5",
tools=[WebSearchTool()],
mcp_servers=[codex_mcp_server],
)
frontend_developer_agent = Agent(
name="Frontend Developer",
instructions=(
f"""{RECOMMENDED_PROMPT_PREFIX}"""
"You are the Frontend Developer.\n"
"Read AGENT_TASKS.md and design_spec.md. Implement exactly what is described there.\n\n"
"Deliverables (write to /frontend):\n"
"- index.html – main page structure\n"
"- styles.css or inline styles if specified\n"
"- main.js or game.js if specified\n\n"
"Follow the Designer’s DOM structure and any integration points given by the Project Manager.\n"
"Do not add features or branding beyond the provided documents.\n\n"
"When complete, handoff to the Project Manager with transfer_to_project_manager_agent."
"When creating files, call Codex MCP with {\"approval-policy\":\"never\",\"sandbox\":\"workspace-write\"}."
),
model="gpt-5",
mcp_servers=[codex_mcp_server],
)
backend_developer_agent = Agent(
name="Backend Developer",
instructions=(
f"""{RECOMMENDED_PROMPT_PREFIX}"""
"You are the Backend Developer.\n"
"Read AGENT_TASKS.md and REQUIREMENTS.md. Implement the backend endpoints described there.\n\n"
"Deliverables (write to /backend):\n"
"- package.json – include a start script if requested\n"
"- server.js – implement the API endpoints and logic exactly as specified\n\n"
"Keep the code as simple and readable as possible. No external database.\n\n"
"When complete, handoff to the Project Manager with transfer_to_project_manager_agent."
"When creating files, call Codex MCP with {\"approval-policy\":\"never\",\"sandbox\":\"workspace-write\"}."
),
model="gpt-5",
mcp_servers=[codex_mcp_server],
)
tester_agent = Agent(
name="Tester",
instructions=(
f"""{RECOMMENDED_PROMPT_PREFIX}"""
"You are the Tester.\n"
"Read AGENT_TASKS.md and TEST.md. Verify that the outputs of the other roles meet the acceptance criteria.\n\n"
"Deliverables (write to /tests):\n"
"- TEST_PLAN.md – bullet list of manual checks or automated steps as requested\n"
"- test.sh or a simple automated script if specified\n\n"
"Keep it minimal and easy to run.\n\n"
"When complete, handoff to the Project Manager with transfer_to_project_manager."
"When creating files, call Codex MCP with {\"approval-policy\":\"never\",\"sandbox\":\"workspace-write\"}."
),
model="gpt-5",
mcp_servers=[codex_mcp_server],
)
project_manager_agent = Agent(
name="Project Manager",
instructions=(
f"""{RECOMMENDED_PROMPT_PREFIX}"""
"""
You are the Project Manager.
Objective:
Convert the input task list into three project-root files the team will execute against.
Deliverables (write in project root):
- REQUIREMENTS.md: concise summary of product goals, target users, key features, and constraints.
- TEST.md: tasks with [Owner] tags (Designer, Frontend, Backend, Tester) and clear acceptance criteria.
- AGENT_TASKS.md: one section per role containing:
- Project name
- Required deliverables (exact file names and purpose)
- Key technical notes and constraints
Process:
- Resolve ambiguities with minimal, reasonable assumptions. Be specific so each role can act without guessing.
- Create files using Codex MCP with {"approval-policy":"never","sandbox":"workspace-write"}.
- Do not create folders. Only create REQUIREMENTS.md, TEST.md, AGENT_TASKS.md.
Handoffs (gated by required files):
1) After the three files above are created, hand off to the Designer with transfer_to_designer_agent and include REQUIREMENTS.md and AGENT_TASKS.md.
2) Wait for the Designer to produce /design/design_spec.md. Verify that file exists before proceeding.
3) When design_spec.md exists, hand off in parallel to both:
- Frontend Developer with transfer_to_frontend_developer_agent (provide design_spec.md, REQUIREMENTS.md, AGENT_TASKS.md).
- Backend Developer with transfer_to_backend_developer_agent (provide REQUIREMENTS.md, AGENT_TASKS.md).
4) Wait for Frontend to produce /frontend/index.html and Backend to produce /backend/server.js. Verify both files exist.
5) When both exist, hand off to the Tester with transfer_to_tester_agent and provide all prior artifacts and outputs.
6) Do not advance to the next handoff until the required files for that step are present. If something is missing, request the owning agent to supply it and re-check.
PM Responsibilities:
- Coordinate all roles, track file completion, and enforce the above gating checks.
- Do NOT respond with status updates. Just handoff to the next agent until the project is complete.
"""
),
model="gpt-5",
model_settings=ModelSettings(
reasoning=Reasoning(effort="medium"),
),
handoffs=[designer_agent, frontend_developer_agent, backend_developer_agent, tester_agent],
mcp_servers=[codex_mcp_server],
)
designer_agent.handoffs = [project_manager_agent]
frontend_developer_agent.handoffs = [project_manager_agent]
backend_developer_agent.handoffs = [project_manager_agent]
tester_agent.handoffs = [project_manager_agent]
task_list = """
Goal: Build a tiny browser game to showcase a multi-agent workflow.
High-level requirements:
- Single-screen game called "Bug Busters".
- Player clicks a moving bug to earn points.
- Game ends after 20 seconds and shows final score.
- Optional: submit score to a simple backend and display a top-10 leaderboard.
Roles:
- Designer: create a one-page UI/UX spec and basic wireframe.
- Frontend Developer: implement the page and game logic.
- Backend Developer: implement a minimal API (GET /health, GET/POST /scores).
- Tester: write a quick test plan and a simple script to verify core routes.
Constraints:
- No external database—memory storage is fine.
- Keep everything readable for beginners; no frameworks required.
- All outputs should be small files saved in clearly named folders.
"""
result = await Runner.run(project_manager_agent, task_list, max_turns=30)
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
스크립트를 실행하고 생성된 파일을 확인합니다.
python multi_agent_workflow.py
ls -R
Project Manager 에이전트는 REQUIREMENTS.md, TEST.md, AGENT_TASKS.md를 작성한 뒤 designer, frontend, server, tester 에이전트 간 handoff를 조율합니다. 각 에이전트는 자기 폴더에 범위가 정해진 artifact를 작성한 뒤 제어를 Project Manager에게 돌려줍니다.
워크플로 trace 확인
Codex는 모든 프롬프트, 도구 호출, handoff를 담은 trace를 자동 기록합니다. 멀티 에이전트 실행이 끝나면 Traces dashboard를 열어 실행 timeline을 확인합니다.
상위 trace는 Project Manager가 다음 단계로 진행하기 전에 handoff를 어떻게 검증하는지 보여 줍니다. 각 단계로 들어가면 프롬프트, Codex MCP 호출, 작성된 파일, 실행 시간을 볼 수 있습니다. 이 정보는 모든 handoff를 감사하고 워크플로가 턴마다 어떻게 변했는지 이해하는 데 유용합니다. 추가 계측 없이도 워크플로 문제를 디버그하고, 에이전트 동작을 감사하고, 시간에 따른 성능을 측정할 수 있습니다.