# OAuth client credentials (/develop/oauth-client-credentials)

Exchange an agent's API key for a short-lived access token with the standard OAuth 2.0 client credentials grant. Use it when an MCP client or agent runtime authenticates only through a token endpoint, not a static bearer key.

Refer to [Agent Identity](/concepts/agents/identity) concept for how keys and tokens relate.

## Overview [#overview]

The agent is the OAuth client. Its agent ID is the client ID and its API key is the client secret. The token endpoint returns a short-lived bearer token, and the agent sends that token to its gateway in place of the key.

| Setting               | Value                                                                        |
| --------------------- | ---------------------------------------------------------------------------- |
| Token endpoint        | `https://api.domesystems.ai/api/v1/auth/token`                               |
| Grant type            | `client_credentials`                                                         |
| Client authentication | HTTP Basic (`client_secret_basic`)                                           |
| Client ID             | The agent ID, a UUID                                                         |
| Client secret         | The agent API key                                                            |
| Scope                 | Optional: omit it or request `mcp`                                           |
| Token lifetime        | The agent's [token lifetime](/build/agents#token-lifetime), `10m` by default |

The token endpoint lives on the Dome API host, not the gateway host. The authorization server metadata at `https://api.domesystems.ai/.well-known/oauth-authorization-server` lists the same `token_endpoint`. On a self-managed deployment, use your own Dome API host.

Three behaviors differ from what some OAuth clients expect:

* **HTTP Basic only.** A request with `client_id` or `client_secret` in the form body is rejected, even when the `Authorization` header is also present.
* **One optional scope.** Omit `scope`, or request `mcp` when a client discovers it from gateway metadata. Other scope values are rejected with `invalid_scope`.
* **No refresh token.** Request a new access token with the same credentials before the current one expires.

## Requirements [#requirements]

Before you begin:

* Register an [agent](/build/agents) and [create an API key](/build/agents#create-key). The key's one-time `token` value is the client secret.
* [Grant the agent access](/build/gateways#manage-agent-access) to the gateway it calls.
* Record the agent ID. Run `dome agents get <name> --json` and read `agent.id`.

Send the agent ID exactly as Dome returns it, in lowercase hyphenated form. Any other spelling does not match the key's agent.

### Permissions [#permissions]

Requesting a token needs no platform permission, because the API key authenticates the agent.

> **Note**: Requires `agentkeys.manage` to create the API key.

## Request a token [#request-a-token]

Send the grant as a form-encoded `POST`, with the agent ID and key in an HTTP Basic header:

```bash title="Request an access token"
curl -X POST https://api.domesystems.ai/api/v1/auth/token \
  -u "{{AGENT_ID}}:{{DOME_AGENT_KEY}}" \
  -d grant_type=client_credentials
```

Add `-d gateway_id={{GATEWAY_ID}}` when the agent can reach more than one gateway. Refer to [Select a gateway](#select-a-gateway).

### Parameters [#parameters]

| Parameter    | Required                                       | Description                                                                      |
| ------------ | ---------------------------------------------- | -------------------------------------------------------------------------------- |
| `grant_type` | Yes                                            | `client_credentials`                                                             |
| `gateway_id` | When the agent can reach more than one gateway | The gateway whose endpoints the response returns, as a lowercase hyphenated UUID |
| `scope`      | No                                             | Omit it, or set it to `mcp`. Other values are not supported.                     |

The request also needs two headers:

| Header          | Value                                                                        |
| --------------- | ---------------------------------------------------------------------------- |
| `Authorization` | `Basic` followed by the base64 encoding of `{{AGENT_ID}}:{{DOME_AGENT_KEY}}` |
| `Content-Type`  | `application/x-www-form-urlencoded`                                          |

### Response [#response]

```json title="Token response"
{
  "access_token": "eyJ…",
  "token_type": "Bearer",
  "expires_in": 600,
  "gateway_id": "{{GATEWAY_ID}}",
  "gateway_endpoints": {
    "gateway_url": "https://{{GATEWAY_HOST}}/gateways/{{GATEWAY_ID}}",
    "mcp_url": "https://{{GATEWAY_HOST}}/gateways/{{GATEWAY_ID}}/mcp",
    "openai_base_url": "https://{{GATEWAY_HOST}}/gateways/{{GATEWAY_ID}}/v1",
    "anthropic_base_url": "https://{{GATEWAY_HOST}}/gateways/{{GATEWAY_ID}}"
  }
}
```

| Field               | Description                                                          |
| ------------------- | -------------------------------------------------------------------- |
| `access_token`      | A signed JWT. Treat it as opaque and send it as a bearer credential. |
| `token_type`        | Always `Bearer`.                                                     |
| `expires_in`        | Seconds until the token expires.                                     |
| `gateway_id`        | The selected gateway.                                                |
| `gateway_endpoints` | Call-ready URLs for the selected gateway. MCP clients use `mcp_url`. |

The response carries no `refresh_token` and no `scope`. Clients that ignore unknown fields can disregard `gateway_id` and `gateway_endpoints`.

### Select a Gateway [#select-a-gateway]

When you omit `gateway_id`, Dome selects the one enabled gateway that grants the agent access. If the agent can reach none, or more than one, the request fails with `invalid_target`. Send `gateway_id` to choose among several.

A named gateway must be enabled, belong to the agent's workspace, and grant the agent access. Otherwise the request fails with `invalid_target`, which does not say which condition failed.

`gateway_id` chooses the endpoints the response returns. It does not bind the token to that gateway. The token works at every gateway in the agent's workspace that grants the agent access, and each gateway checks that grant on every request. The token also authenticates the agent's own calls to the Dome API.

### Token lifetime [#token-lifetime]

`expires_in` matches the agent's [token lifetime](/build/agents#token-lifetime): `10m` unless you set a value between `1m` and `24h`. Cache the token and reuse it until shortly before it expires, then request a new one. Do not request a token for every call.

A suspended or revoked agent cannot obtain a token. Neither can an agent whose organization is suspended.

## Call the Gateway [#call-the-gateway]

Send the access token as a bearer credential to the `mcp_url` from the response:

```bash title="List tools with the access token"
curl -X POST "https://{{GATEWAY_HOST}}/gateways/{{GATEWAY_ID}}/mcp" \
  -H "Authorization: Bearer {{ACCESS_TOKEN}}" \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
```

The gateway authenticates the agent from the token, then applies the same gateway access checks and rules it applies to the agent key. Once the token expires, the gateway answers HTTP `401`. Request a new token and retry.

## Configure an MCP client [#configure-an-mcp-client]

MCP clients that support only OAuth client credentials ask for a similar set of values. Map them as follows:

| Client setting        | Value                                                                         |
| --------------------- | ----------------------------------------------------------------------------- |
| Server URL            | The gateway `mcp_url`, `https://{{GATEWAY_HOST}}/gateways/{{GATEWAY_ID}}/mcp` |
| Transport             | Streamable HTTP                                                               |
| Token URL             | `https://api.domesystems.ai/api/v1/auth/token`                                |
| Client ID             | The agent ID                                                                  |
| Client secret         | The agent API key                                                             |
| Client authentication | HTTP Basic header                                                             |
| Scope                 | Empty or `mcp`                                                                |

Check these client options before you connect:

* **Credential placement.** Choose the HTTP Basic header. Clients that send credentials in the request body (`client_secret_post`) fail with `invalid_request`.
* **Scope.** Either leave it empty or request `mcp` when the client discovers that scope from gateway metadata.
* **Secret encoding.** Enter the API key exactly as issued; clients using standard OAuth HTTP Basic encoding are supported.
* **Gateway selection.** If the client cannot add `gateway_id` to the token request, give it a dedicated agent with access to exactly one gateway.

Configure the client for Streamable HTTP, not SSE. The gateway does not support the legacy standalone SSE transport.

## Errors [#errors]

Error responses use the OAuth 2.0 error shape:

```json title="Error response"
{
  "error": "invalid_scope",
  "error_description": "the only supported scope is mcp"
}
```

| HTTP status | `error`                   | Cause                                                                                                                   |
| ----------- | ------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| `400`       | `invalid_request`         | The content type is not form-encoded, the body is malformed, or `grant_type` is missing                                 |
| `400`       | `invalid_request`         | The Basic header is missing or malformed, the client ID or secret is empty, or credentials appear in the body           |
| `400`       | `invalid_request`         | `gateway_id` is not a lowercase hyphenated UUID                                                                         |
| `400`       | `unsupported_grant_type`  | `grant_type` is not a supported grant                                                                                   |
| `400`       | `invalid_scope`           | The request has a `scope` other than `mcp`                                                                              |
| `400`       | `invalid_target`          | `gateway_id` is omitted and the agent can reach no gateway or several, or the named gateway is unavailable to the agent |
| `401`       | `invalid_client`          | The API key is unknown or revoked, or the client ID does not match the key's agent                                      |
| `403`       | `unauthorized_client`     | The agent is suspended or revoked, or its organization is suspended                                                     |
| `429`       | `temporarily_unavailable` | Too many failed attempts from your network address. Wait before retrying.                                               |
| `500`       | `server_error`            | Dome could not complete the exchange. Retry with backoff.                                                               |

A request that is not a `POST` receives a plain-text HTTP `405`.

Do not retry `400`, `401`, or `403` responses until you fix the request or the agent.

## Next steps [#next-steps]

* [Develop with Dome](/develop) to call tools and models through a gateway
* [Agents](/build/agents#create-key) to create, rotate, and revoke API keys
* [Gateways](/build/gateways#manage-agent-access) to grant an agent access to a gateway
* [Interactive MCP access](/build/gateways/interactive-mcp-access) to let people sign in with their own Dome account instead of an agent key