---
title: Connect Codex
description: Connect the Codex CLI to Unity Gateway to route requests through governed Databricks models, MCP tools, and skills.
canonical_url: https://docs.databricks.com/aws/en/ai-gateway/coding-agent-codex
---

# Connect Codex

Use Codex in your terminal with models, MCP tools, and skills through Unity Gateway. Use the Unity Gateway CLI (`ug`) to configure access, or configure the connection manually.

Use Codex CLI with [supported open-source models](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-open-source-models.md) through Unity Gateway to help reduce coding agent costs.

To connect tools to an existing Codex setup, go to [Add MCP tools](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-codex.md#add-mcp-tools).

## Prerequisites

You need your Databricks workspace URL, access to the models, MCPs, and skills you want to use, and the latest [Codex CLI](https://developers.openai.com/codex/cli).

If your admin has already configured your device, follow your organization's launch instructions.

## Use the Unity Gateway CLI (recommended)

[Install `ug`](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-quickstart.md), then run this command from your project directory:

```bash
ug codex
```

Follow the prompts to select your workspace and sign in. `ug` configures the connection and opens Codex in your terminal. Start working with the same prompts and commands you already use. To change models, enter `/model`.

Continue to [Add MCP tools](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-codex.md#add-mcp-tools) or [Add skills](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-codex.md#add-skills) to give Codex access to data and shared instructions.

### <a id="bring-your-own-subscription" />Bring your own ChatGPT subscription

To run Codex on your own ChatGPT Go, Plus, Pro, or Enterprise subscription through Unity Gateway, ask your admin for a ChatGPT subscription model provider service. See [ChatGPT subscription support](https://docs.databricks.com/aws/en/ai-gateway/chatgpt-subscription-support.md) for requirements, admin setup, how it works, and limitations.

Pass the model provider service's fully qualified name (`<catalog>.<schema>.<name>`) to `--provider` and the workspace URL to `--workspace`:

```bash
ug codex --provider main.default.chatgpt_subscription --workspace https://<workspace>.databricks.com
```

If Codex isn't signed in with ChatGPT, `ug` runs `codex login` to open the browser sign-in. An API key sign-in doesn't count. `ug` configures Codex to send the `Databricks-Model-Provider-Service` header and points Codex at a local refresh proxy rather than directly at the Unity Gateway endpoint. The proxy forwards each request to `https://<workspace-url>/ai-gateway/codex/v1` and adds the `X-Databricks-AI-Gateway-Token` header. Because the service stores no credential, the proxy passes your ChatGPT sign-in through without storing it.

If you run into issues, sign out with `codex logout`, then rerun the command to sign in again.

> [!NOTE]
>
> `--provider` for a ChatGPT subscription model provider service is only supported with `ug codex`. Smart Routing isn't supported with a ChatGPT subscription model provider service.

### Configure a ChatGPT subscription manually

Use the manual setup only if you cannot use `ug`.

1. Get a Databricks token. The `X-Databricks-AI-Gateway-Token` header carries a Databricks token that authenticates the request to Unity Gateway. Unity Gateway attributes access control and usage logs to the identity tied to this token, so use a token that belongs to the querying user. Select one of the following:
   * **OAuth user-to-machine (U2M) with the Databricks CLI (recommended).** Log in once with the CLI, then mint a short-lived OAuth token:

     ```bash
     databricks auth login --host https://<workspace-url>
     databricks auth token --host https://<workspace-url> | jq -r .access_token
     ```

     See [Authorize user access to Databricks with OAuth](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-u2m.md) and [Authentication for the Databricks CLI](https://docs.databricks.com/aws/en/dev-tools/cli/authentication.md). A U2M OAuth token expires in about 1 hour, so for interactive use you must replace it in your configuration when it expires. To automate token refresh, use [`ug`](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-codex.md#bring-your-own-subscription), which runs a local proxy that refreshes the OAuth token for you on every request.

   * **Personal access token (PAT), as a backup.** If you cannot use OAuth, create a PAT and use it as `<databricks-token>`. A PAT does not expire on the 1-hour OAuth cadence, but it is a long-lived credential tied to your identity. See [Authenticate with Databricks personal access tokens (legacy)](https://docs.databricks.com/aws/en/dev-tools/auth/pat.md).

2. Close Codex, then open or create `~/.codex/config.toml`. On Windows, use `%USERPROFILE%\.codex\config.toml`. Merge the following settings into the file. Keep `model_provider` at the top level, before any table headers, and preserve unrelated settings.

   ```toml
   model_provider = "databricks"

   [model_providers.databricks]
   name = "Databricks"
   base_url = "https://<workspace-url>/ai-gateway/codex/v1"
   wire_api = "responses"
   requires_openai_auth = true
   http_headers = { "X-Databricks-AI-Gateway-Token" = "Bearer <databricks-token>", "Databricks-Model-Provider-Service" = "<catalog>.<schema>.<name>" }
   ```

   * Replace `<workspace-url>` with your workspace URL.
   * Replace `<databricks-token>` with the Databricks token from the previous step.
   * Replace `<catalog>.<schema>.<name>` with the fully qualified name of your model provider service.

   `requires_openai_auth = true` makes Codex send its own ChatGPT sign-in. Don't add an `auth` table to this provider. Keep the file private, because it stores your Databricks token.

3. Start Codex:

   ```bash
   codex
   ```

4. If you are not already signed in, Codex prompts you to authenticate. Select **Sign in with ChatGPT** and sign in with your ChatGPT account. An API key sign-in doesn't work with a ChatGPT subscription model provider service. If you run into issues, sign out with `codex logout` and sign in again.

Your Codex requests now route through Unity Gateway. Requests are billed against your ChatGPT subscription, and you get centralized access, routing, and usage governance.

### Run on Databricks compute

To run Codex on serverless compute or a dedicated cluster without installing `ug`, connect with the SSH tunnel and run `codex`. The agent is preconfigured for Unity Gateway. See [Run coding agents in an SSH session](https://docs.databricks.com/aws/en/dev-tools/ssh-tunnel.md#coding-agents).

## Configure models manually

Close Codex, then open or create `~/.codex/config.toml`. On Windows, use `%USERPROFILE%\.codex\config.toml`.

Merge the following settings into the file. Keep `model` and `model_provider` at the top level, before any table headers, and preserve unrelated settings.

```toml
model = "<catalog>.<schema>.<model-name>"
model_provider = "databricks"

[model_providers.databricks]
name = "Databricks"
base_url = "https://<workspace-hostname>/ai-gateway/codex/v1"
wire_api = "responses"
requires_openai_auth = false
supports_websockets = false
http_headers = { Authorization = "Bearer <databricks-pat>" }
```

Replace the model placeholder with its full Unity Catalog name, `<workspace-hostname>` with your workspace hostname, and `<databricks-pat>` with your personal access token. This example stores the token locally; keep the file private and use your own token.

Run `codex` from your project directory. If your device has managed provider settings, ask your admin to update them; those settings take precedence over this user configuration.

See [OpenAI's configuration reference](https://developers.openai.com/codex/config-reference) for field details.

## <a id="add-mcp-tools" />Add MCP tools

`system.ai.dbsql`, `system.ai.sandbox`, and `system.ai.web_search` are in [Beta](https://docs.databricks.com/aws/en/release-notes/release-types.md). To use them, an account admin must enable the **Enhanced Unity Gateway** preview from the account console **Previews** page. Unity Gateway itself is generally available. See [Manage account previews](https://docs.databricks.com/aws/en/admin/workspace-settings/manage-previews.md#account).

1. Open **Unity Gateway > MCPs** in your workspace. Choose a [built-in MCP](https://docs.databricks.com/aws/en/agents/mcp-tools/built-in-mcp-services.md) or [register your external MCP server](https://docs.databricks.com/aws/en/ai-gateway/register-mcp-service.md).
2. Copy the MCP's full name, such as `system.ai.github` or `<catalog>.<schema>.<service>`.
3. Confirm that you have [access to the MCP](https://docs.databricks.com/aws/en/ai-gateway/govern-mcp-service.md#control-callers).

### Use the Unity Gateway CLI

[Install `ug`](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-quickstart.md), then add the MCP to Codex:

```bash
ug mcp add --agents codex --names <catalog>.<schema>.<service>
```

Replace the placeholder with the name you copied. For example, use `--names system.ai.github` for GitHub. To choose services interactively, omit `--names`.

On first setup, follow the prompts to select your workspace, sign in, and choose a model. `ug` configures both model and MCP access and refreshes credentials. For MCP access with your own OAuth app, use the manual setup below.

Restart the Codex CLI with `ug codex`, then [test a tool](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-codex.md#test-an-mcp-tool).

### Configure MCPs manually

Use a public Databricks OAuth app to sign in and refresh credentials.

To use the [default Codex client ID](https://docs.databricks.com/aws/en/partner-connect/ai-agents.md#default-oauth-apps), which doesn't need an account admin to create an OAuth app:

1. Register the MCP in Codex, replacing the placeholders:

   ```bash
   codex mcp add databricks-tools \
     --url "https://<workspace-hostname>/ai-gateway/mcp-services/<catalog>.<schema>.<service>?o=<workspace-id>" \
     --oauth-client-id https://chatgpt.com/oauth/codex/client.json
   ```

2. Sign in, then reopen Codex:

   ```bash
   codex mcp login databricks-tools --scopes ai-gateway,offline_access
   ```

Then [test a tool](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-codex.md#test-an-mcp-tool).

**Set up a custom OAuth app**

1. Close Codex. In `~/.codex/config.toml`, add these settings at the top level, before any table headers. They give the local OAuth callback a fixed port:

   ```toml
   mcp_oauth_callback_port = 8080
   mcp_oauth_callback_url = "http://127.0.0.1:8080/callback"
   ```

2. Have an account admin open **Settings > App connections > Add connection** in the [account console](https://accounts.cloud.databricks.com). Use a name such as `codex-mcp`, clear **Generate a client secret**, set the redirect URL to `http://127.0.0.1:8080/callback`, and select the `ai-gateway` scope. Save and copy the **Client ID**. See [Create an OAuth app](https://docs.databricks.com/aws/en/integrations/enable-disable-oauth.md#enable-custom-app-ui).

3. Register the MCP in Codex, replacing the placeholders:

   ```bash
   codex mcp add databricks-tools \
     --url "https://<workspace-hostname>/ai-gateway/mcp-services/<catalog>.<schema>.<service>?o=<workspace-id>" \
     --oauth-client-id <client-id>
   ```

4. Check the callback URL printed by Codex. It can include a server-specific suffix. Have the admin add that exact URL to the OAuth app's redirect URLs before signing in. Repeat this check when adding another server.

5. Sign in, then reopen Codex:

   ```bash
   codex mcp login databricks-tools --scopes ai-gateway,offline_access
   ```

For callback settings, see [OpenAI's MCP authentication instructions](https://developers.openai.com/codex/mcp). Then [test a tool](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-codex.md#test-an-mcp-tool).

**Use a personal access token for local testing**

Add the following to `~/.codex/config.toml`, replacing the hostname, workspace ID, and token:

```toml
[mcp_servers.dbsql]
url = "https://<workspace-hostname>/ai-gateway/mcp-services/system.ai.dbsql?o=<workspace-id>"
http_headers = { Authorization = "Bearer <databricks-pat>" }
```

For another MCP, use a unique name under `mcp_servers` and replace `system.ai.dbsql` with its full name. Keep the file private, then restart Codex and test a tool.

### <a id="test-an-mcp-tool" />Test an MCP tool

1. In Codex, enter `/mcp` and check that the server is connected.
2. Ask Codex to list that server's tools, then request a read operation. For GitHub, ask it to find an issue in a repository you can access. For a custom server, use a tool and inputs you tested during registration.
3. If a tool call prompts you to sign in, open the login link returned by the MCP, complete provider consent, then retry the call. This sign-in gives the MCP access to your external account.
4. Check that Codex makes a tool call and returns its result. A text answer alone doesn't verify the connection.

To use Databricks data, connect `system.ai.genie_one_mcp` for business questions or `system.ai.dbsql` for SQL. For example, ask the SQL MCP to run `SELECT 1 AS result`.

For permissions, policies, and usage, see [Govern an MCP](https://docs.databricks.com/aws/en/ai-gateway/govern-mcp-service.md).

## <a id="add-skills" />Add skills

### Use the Unity Gateway CLI

Run the interactive picker:

```bash
ug skills add
```

Or download a specific published skill:

```bash
ug skills add --names <catalog>.<schema>.<skill-name>
```

Restart the Codex CLI. Downloaded skills are available locally in `~/.agents/skills/`. Re-run the download to get an updated version.

To expose a schema's skills through MCP instead, run:

```bash
ug skills add --location "<catalog>.<schema>" --via mcp
```

### Connect the skill registry manually

Add the following to `~/.codex/config.toml`:

```toml
[mcp_servers.databricks-skill-registry]
url = "https://<workspace-hostname>/ai-gateway/skills/"
http_headers = { Authorization = "Bearer <databricks-pat>" }
```

Replace the hostname and token. Keep the trailing slash in the URL. Restart Codex and ask it to use a published skill, such as `Use <catalog>.<schema>.<skill-name> to review this query.`

The registry loads skill instructions through MCP. To install skill files you already have, place the complete skill folder, including `SKILL.md` and bundled files, in `~/.agents/skills/`.

Unity Gateway skills are in Beta. See [Govern skills](https://docs.databricks.com/aws/en/ai-gateway/govern-skills.md) for enablement and permissions.

## Troubleshooting

* **Requests fail with a WebSocket error:** Set `supports_websockets = false` in the active Databricks provider table. If your admin manages that configuration, ask them to update it. Restart the app afterward.

* **A model is missing:** Check your model permissions. Set `model` to its full Unity Catalog name in the active configuration and start a new conversation.

* **An MCP or skill connection fails:** Check the URL, permissions, and connector error. For manual connections, also check token expiration. For `ug` setup problems, run `ug doctor`. A downloaded skill can remain available even if the registry connection fails.

* **Workspace sign-in works, but a tool asks you to log in:** Open the login link returned by the MCP, sign in to the external provider, then retry the call. Workspace and provider sign-in are separate steps.

* **OAuth reports a redirect mismatch or the connection times out:** Match the OAuth app's redirect URL to the exact URL Codex reports, including any suffix, and check [network access](https://docs.databricks.com/aws/en/agents/mcp-tools/networking.md). OAuth app changes can take [up to 30 minutes](https://docs.databricks.com/aws/en/integrations/enable-disable-oauth.md) to take effect.

## Next steps

* [Connect ChatGPT desktop](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-chatgpt-desktop.md)
* [ug CLI reference](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-ug-cli.md)
* [Add tools and skills](https://docs.databricks.com/aws/en/ai-gateway/coding-agent-mcp-skills.md)
