# MCP: drive it from an agent

> Let an AI agent connect and check domains.

Source: https://docs.customdomain.ai/docs/getting-started/mcp

Use the MCP server when an **agent** should connect domains for a user. Claude,
Cursor or your own MCP client can find providers, start connections and read
their status. It is the same API, behind a tool interface.

## Connect

[Open AI tools in console](https://app.customdomain.ai/app/mcp)

* Endpoint: `https://mcp.customdomain.ai/mcp` (Streamable HTTP).
* **API key:** a `cd_live_…` key from the console's **API & SDK** page (an older
  `sk_` key keeps working), sent as `Authorization: Bearer <key>`. It works for
  every tool except `create-domain-order`. It carries your whole workspace's
  authority, so give it only to agents you trust.
* **Agent token:** the scoped option. A person approves exactly which scopes the
  agent gets (`domains:read`, `domains:connect`, `domains:disconnect`,
  `domains:purchase`) and can revoke it at any time. See
  [Agent access](/docs/agents/overview).
* **Purchases through MCP are off on the hosted service.**
* The console's **MCP server** page (switch it on in **Settings → Features**)
  has ready made setup for each client (with a key) and a connection test.
* **VS Code in one command:**
  `code --add-mcp '{"name":"customdomain","type":"http","url":"https://mcp.customdomain.ai/mcp"}'`.
  VS Code opens your browser to sign in the first time it connects.

## Set up an agent from the welcome

On the welcome's first question, tick **Also let an AI agent connect and manage
domains for me** and it has one more step, **Give your agent access**. It is
optional, and **Not now** always works. Pick your agent (Claude Code, Claude
Desktop, Codex, Cursor, VS Code, ChatGPT or Other) and the step shows what to add
to it: a command, or a few fields to fill in.

* **Most agents sign in, so there is no key.** A browser opens, you approve what
  the agent may do, and the step says **connected**, with the agent's name, only
  once it signed in and you approved it. A grant you revoked or that expired does
  not count. The same grant is what **Overview** shows on its **Connect your
  agent** row, and you can end it under **Agents** (in the sidebar under **More**).
* **Cursor cannot sign in to a new server by itself yet**, so it gets the key
  setup, with your default key from **API & SDK** (every workspace has one from the start).
* **An agent that connects with a key leaves nothing to see**, so the step says it
  cannot watch for it and the checklist does not claim it. Carry on once you have
  added it.
* **A prompt to paste** into the agent tells it to read the
  [agent skill](/docs/agents/skills), to show you what it plans before anything
  changes, and what to do first for what you told the welcome: connect your
  domain, add the widget to your product, or list the domains you added. It never
  holds a key.

## The flow an agent should follow

1. `discover-provider { domain }` is read only. It says where the domain's DNS
   lives and which ways can run: `rails` (`domainConnect`, `oauth`, `secureSignIn`,
   `apiKey`, `manual`, `dnsHosting` and `savedAccess`), `recommendedRail` and
   `blockedReason` (the same truth as `rails` on `POST /v1/domains:check`). A person
   starts a secure sign in, never an agent, so for an agent `secureSignIn` reads
   `available: false` with `reason: caller_cannot_start` even where the provider
   offers it: that is not a missing way. Do this **before** promising anything.
2. `connect-domain { domain }` creates the connection and returns the `records` to
   add, a `connectionId` and a `link` for the **human** to complete, with a
   `linkKind` that says who opens it: `domain_connect` (one click at their DNS
   provider) and `guided_setup` (a single use setup page for this one domain, with
   no account, where they take a sign in, a secure sign in or the records) are for
   the user; `console` needs a login to the workspace, so it is for a member of the
   workspace. An API key or a client credentials token gets `guided_setup` where the
   provider has no one click page; an agent token always gets `console`, so give the
   user the `records`. A `guided_setup` link is a bearer link that lasts 30 days and
   lets whoever opens it first set up the domain, so send it only to the person who
   owns the domain. The agent never supplies DNS records: the server works them out.
3. `check-connection-status { connectionId }` returns a `status` of `pending`,
   `propagating`, `live` or `failed`. Stop when `connected` is `true`. A failed
   connection says why in `errorCode`. `pending` has no deadline; a `failed`
   connection can be retried by calling `connect-domain` again for the same
   domain, which revives it.
4. A domain that is not live yet: `diagnose-connection` says record by record
   what public DNS shows and what is missing. Once the person has fixed it,
   `recheck-connection` runs the check now. `check-certificate-status` reads the
   HTTPS certificate; certificates renew on their own.
5. `list-connections { status?, dnsHosting?, limit?, cursor? }` lists the
   connected domains with their status (every application in the workspace when
   you use an API key). Each row carries `dnsHosting`, who runs the domain's DNS,
   and `dnsHosting: "hosted"` (or `moving`, or `any`) keeps only the domains
   CustomDomain™ runs. A page that was cut says `truncated: true`; call again
   with `nextCursor` as `cursor`.

When a connect fails with `billing_required`, `check-billing-status` says why and
where an owner fixes it.

Buying: `search-domain-availability` and `generate-domain-suggestions` work as
read tools. `create-domain-order` exists, but ordering is off on the hosted
service. `check-order-status` and `list-domain-orders` read orders. Email and
forwarding: `add-email`, `forward-domain`. Moving a domain's nameservers to the
application's DNS host: `migrate-nameservers` and `rollback-nameservers` (both ask
the person first and send `confirm: true`), and `check-nameserver-status` to follow
the move. With an agent token, the application's setting decides where the
nameservers go: `target` and `nameservers` are refused, and it never resumes the
move another application of the workspace has open. An agent may move nameservers
only while the application's DNS hosting is on; with it off the call fails with
`ns_not_offered` and changes nothing, and a person can turn DNS hosting on in the
application's settings, or the platform can start the move with its API key. Each move an agent token
starts, resumes or reverses is in the audit log as `agent.acted` under its grant. See
[Domain maintenance (nameserver migration)](/docs/guides/nameserver-migration).

## Re-apply and disconnect

* `reapply-connection` rewrites a connection's records through the access
  CustomDomain™ keeps for the domain: the stored grant of a **managed** connection,
  a DNS zone CustomDomain™ hosts for it (`dnsHosting` `hosted` on
  `list-connections`) or a connected provider account that keeps it. A connection
  with none of these answers `409` (`conflict`), so check first, and repair it with
  `find-saved-access` and `use-saved-access`, or have the person add the records
  at their DNS provider or sign in again. `managed: false` does not mean a repair
  needs the person at their DNS provider. With an agent token
  each re-apply that reached a write is in the audit log as `agent.acted`. See
  [Managed connections](/docs/connect-flow/managed-connections).
* `disconnect-domain { connectionId, confirm }` removes any connection of the
  application, managed or not. The domain stops being served and its certificate
  and monitoring go away. DNS records are removed only where a kept grant wrote
  them; anywhere else they stay at the provider and the user deletes them. It
  answers `409 removal_in_progress` while another removal of the same connection
  is running. The domain goes offline, so ask the person first and send the
  domain they confirmed as `confirm`. Every caller must send it, and it is
  compared with the connection's domain whatever the credential (an API key, a
  client credentials token or an agent token): another domain answers
  `409 confirmation_mismatch` and nothing is removed. The control plane
  checks it before anything is removed.
* Disconnecting needs the `domains:disconnect` scope, which is separate from
  connecting. A grant approved before that scope existed does not carry it until
  the person approves the agent again. See
  [Removing a domain](/docs/connect-flow/offboarding).

## When a call fails

A failed call is an `isError` result with `success: false`, an `error` code and
`details` when there is one. Branch on the code. A token without the tool's
scope gets `insufficient_scope`, and a caller over its limit (5 calls a second,
bursts of 20) gets `rate_limited` with `retryAfterSeconds`. With API keys the
limit is per credential, so each key has its own; agent tokens share one per
workspace. An `add-email` or `forward-domain` value of the wrong shape (an IP
address where a hostname belongs, a DKIM selector with dots, more than one DMARC
address) fails with `-32602`, which names the argument. A key or token that is
invalid, expired or revoked gets HTTP `401` with a `WWW-Authenticate` challenge,
so the client knows to sign in again. See
[What a call answers](/docs/mcp/overview#what-a-call-answers).

## Agent authorization

Delegated agent access is live. An agent registers as an OAuth client, a person
approves it on the consent page, and the agent gets a 15 minute token limited to
the scopes that person approved. Review and revoke agents under **Agents**,
which switches on after your first approval (see
[Managing access](/docs/agents/managing-access)). Full tool reference:
[MCP server](/docs/mcp/overview).
