# How to use Codex CLI: install, AGENTS.md, MCP and exec

> How to use Codex CLI: install it, sign in, write an AGENTS.md, set permissions, resume sessions, script it with codex exec and add MCP servers.

Part 3 of [Using coding agents: Claude Code, Cursor, Codex and more](https://polylane.com/series/using-coding-agents/)

Canonical: https://polylane.com/learn/coding-agents/how-to-use-codex-cli/

To use Codex CLI, install it with OpenAI's installer, run codex in your project and sign in with your ChatGPT account. Run /init to create an AGENTS.md, use /permissions to choose what Codex may do, and codex resume to reopen a chat. For scripts and CI, use codex exec, and add MCP servers with codex mcp add.

To use Codex CLI, **install it, run `codex` in your project, sign in with ChatGPT, and run `/init` to give it an AGENTS.md.** Choose what it may do with `/permissions`, reopen work with `codex resume`, and use `codex exec` when a script or CI job runs it instead of you.

Codex CLI is OpenAI's coding agent for the terminal. It reads your repository, edits files and runs commands from a session in your shell. The steps below follow [OpenAI's Codex CLI documentation](https://learn.chatgpt.com/docs/codex/cli).

## 1. Install Codex CLI and sign in

On macOS and Linux, the standalone installer is one line:

```bash
curl -fsSL https://chatgpt.com/codex/install.sh | sh
```

Then start it from a project:

```bash
cd ~/code/my-app
codex
```

The first run asks you to sign in. Choose **Sign in with ChatGPT** to use your ChatGPT plan.

## 2. Give it an AGENTS.md

Codex reads project instructions from an AGENTS.md file. Create one from inside a session:

```text
/init
```

`/init` writes an AGENTS.md with instructions for Codex based on your repository. Edit it down to what a new engineer would need: how to build, how to run the tests, the conventions that are easy to get wrong. Commit it, so every session and every teammate starts from the same rules.

## 3. Choose what Codex may do

Codex asks before it acts outside what you have allowed. Use the `/permissions` slash command to choose what Codex is allowed to do in the session. Start strict on a repository you care about, and loosen it once you trust how it works there.

The other slash commands worth learning first:

| Command | What it does |
| --- | --- |
| `/model` | Choose the model and its reasoning effort |
| `/status` | Show the current session configuration |
| `/review` | Review your changes and find issues |
| `/init` | Create an AGENTS.md |

## 4. Pick up where you left off

```bash
codex resume
```

`codex resume` reopens a recent chat from the current repository, with its history, so you don't have to explain the task again.

## 5. Run it from scripts with codex exec

`codex exec` runs Codex without the interactive UI, which is how you use it from repeatable workflows and pipelines:

```bash
codex exec "update the changelog for the commits since the last tag"
```

Keep scripted tasks narrow, with a clear definition of done, and review what they change before it merges.

## 6. Add MCP servers

Codex calls tools outside your repository through the Model Context Protocol. Each server is a `[mcp_servers.<name>]` table in `~/.codex/config.toml`, or in a trusted project's `.codex/config.toml`.

A local server needs the `command` that starts it:

```toml
[mcp_servers.context7]
command = "npx"
args = ["-y", "@upstash/context7-mcp"]
```

A remote server needs a `url`. If it takes a bearer token, name the environment variable that holds it with `bearer_token_env_var`:

```toml
[mcp_servers.polylane]
url = "https://mcp.polylane.com/mcp"
```

You can also add servers from the command line with `codex mcp add`. For a server that uses OAuth, finish the sign-in with:

```bash
codex mcp login polylane
```

Production is the context Codex usually lacks: it can read the code that failed, but not the logs, traces and deploys that show how. [Polylane gives Codex that context](/for/codex/) through the one remote server above, covering every provider you connect.

Using Codex? See [how to give Codex production context with Polylane](https://polylane.com/for/codex/).

## Common questions

**How do I sign in to Codex CLI?**

Run codex in a project directory. The first time, it asks you to sign in, and you can choose Sign in with ChatGPT.

**Where does Codex read project instructions from?**

From AGENTS.md. Run /init inside Codex to create one with instructions for your repository, then edit it and commit it.

**How do I run Codex without the interactive UI?**

Use codex exec. It is the entry point for repeatable workflows and pipelines, such as a CI job.

**Where is Codex's config file?**

~/.codex/config.toml by default. A trusted project can also have its own .codex/config.toml.

**How do I connect Codex to an MCP server that uses OAuth?**

Add the server with its URL, then run codex mcp login with the server's name to complete the sign-in.

## Sources

- [Codex CLI (OpenAI docs)](https://learn.chatgpt.com/docs/codex/cli)
- [Model Context Protocol in Codex (OpenAI docs)](https://learn.chatgpt.com/docs/extend/mcp?surface=cli)

## About the author



## More in this series

[Using coding agents: Claude Code, Cursor, Codex and more](https://polylane.com/series/using-coding-agents/)

1. [How to use Claude Code: a practical guide](https://polylane.com/learn/coding-agents/how-to-use-claude-code/)
2. [How to add MCP servers to Cursor](https://polylane.com/learn/coding-agents/how-to-add-mcp-servers-to-cursor/)
3. [How to use Codex CLI: install, AGENTS.md, MCP and exec](https://polylane.com/learn/coding-agents/how-to-use-codex-cli/)
4. [How to use OpenCode: install, plan and build, MCP](https://polylane.com/learn/coding-agents/how-to-use-opencode/)
5. [How to add MCP servers to GitHub Copilot in VS Code](https://polylane.com/learn/coding-agents/how-to-add-mcp-servers-to-github-copilot/)
6. [How to use the Pi coding agent](https://polylane.com/learn/coding-agents/how-to-use-the-pi-coding-agent/)

Previous: [How to add MCP servers to Cursor](https://polylane.com/learn/coding-agents/how-to-add-mcp-servers-to-cursor/)
Next: [How to use OpenCode: install, plan and build, MCP](https://polylane.com/learn/coding-agents/how-to-use-opencode/)

Get started with one command: `curl -fsSL https://polylane.com/setup | bash` installs the CLI, connects your coding agents, and creates the account.
