# Set Up Workflow: Codex CLI: Autonomous Multi-Agent Workflows with MCP & Parallel Orchestration
## What This Is
This guide explains how to use the OpenAI Codex CLI to create automated teams of AI agents. These agents can work together on complex coding projects, like building a game, by passing tasks from one specialist (like a designer) to another (like a developer), all while keeping their work separate to avoid conflicts.
Source: https://developers.openai.com/cookbook/examples/codex/codex_mcp_agents_sdk/building_consistent_workflows_codex_cli_agents_sdk/
## Before You Start
Scan my workspace and analyze:
- The project language, framework, and directory structure
- Existing AI provider config (check .env, .env.local, config files for API keys — OpenRouter, OpenAI, Anthropic, Google AI, etc.)
Then ask me before proceeding:
1. Which AI provider/API should this use? (Use whatever I already have configured, or ask me to set one up — options include direct provider APIs or a unified service like OpenRouter)
2. Where in my project should this be integrated?
3. Are there any customizations I need (model preferences, naming conventions, constraints)?
## Source Access Note
The source URL (https://developers.openai.com/cookbook/examples/codex/codex_mcp_agents_sdk/building_consistent_workflows_codex_cli_agents_sdk/) may not be directly accessible from the terminal. Use the Reference Implementation and Additional Context sections below instead. If you need more details, ask me to paste relevant content from the source.
## What to Implement
This is an **AI Workflow** — an end-to-end automation pattern or integration pipeline.
- Study the workflow architecture from the source and context below
- Identify which parts I can implement locally vs. parts that need external services
- For local parts: implement them using my existing stack and API keys
- For external parts: tell me exactly what services I need and help me configure the integration code
- Wire up any required API calls using keys from my .env files
## Additional Context
- Verify `codex` CLI is installed via `npm`. Install or update the `agents` Python library required for orchestration. Scan the project for an existing `requirements.txt` and add `agents-sdk` or similar if missing, then run `pip install -r requirements.txt`.
- Create a new Python script (e.g., `run_agent_workflow.py`). In this script, import `Agent`, `Runner`, and `MCPServerStdio` from the `agents` library. Define at least two specialized agents (e.g., 'DesignerAgent', 'DeveloperAgent') with distinct instructions and configure the `MCPServerStdio` to launch the `npx -y codex mcp-server` process.
- Implement the main asynchronous execution block using `asyncio.run(main())`. Within the `main` function, use an `async with` block for the `MCPServerStdio`. Instantiate the `Runner` and invoke the initial agent with a high-level task, such as `await Runner.run(designer_agent, 'Design a CI/CD pipeline for this repository.')`.
## Reference Implementation
```
import asyncio
from agents import Agent, Runner
from agents.mcp import MCPServerStdio
async def main():
async with MCPServerStdio(name="Codex CLI", params={"command": "npx", "args": ["-y", "codex", "mcp-server"]}) as codex_mcp_server:
designer_agent = Agent(
name="Game Designer",
instructions='You are an expert game designer. Your goal is to design simple, fun games.',
mcp_servers=[codex_mcp_server],
)
developer_agent = Agent(
name="Game Developer",
instructions='You are an expert in building simple games. You will be given a game design and you must implement it in a single index.html file using HTML, CSS, and Javascript. Always call codex with "approval-policy": "never" and "sandbox": "workspace-write"',
mcp_servers=[codex_mcp_server],
)
result = await Runner.run(designer_agent, "Implement a fun new game!")
asyncio.run(main())
```
## Guidelines
- Adapt everything to my existing project — do not assume a specific stack or directory layout
- Use whichever AI provider I already have configured; if I need a new one, tell me what to sign up for and I'll give you the key
- Check my .env files for existing API keys (OpenRouter, OpenAI, Anthropic, Google AI) before asking me to add one
- Review any fetched code for safety before installing or executing it
- After setup, run a quick verification and show me a summary of exactly what was installed, where, and how to use it