# Adoption & Device Flow

Source: https://molted.email/docs/adoption

Add agents or CLI users to an existing tenant via invite tokens or the device authorization flow.

Adoption lets you add new agents or CLI users to an existing tenant without requiring them to create a new account. Two flows are supported:

- **Invite flow** -- someone already in the workspace creates a token and hands it to the agent, which claims it.
- **Device flow** -- the agent starts the request and shows a short code, and a person signed in to the portal approves it.

Both flows end with the agent holding its own API key (`mm_live_...`) for the tenant, scoped to all mailboxes or to specific ones, and registered as an agent under the invite's label. Every step is available from the CLI (`molted adopt`, see the [CLI reference](https://molted.email/docs/cli/adopt.md)) and over HTTP. The MCP server has no adoption tools: an MCP agent is configured with the key a claim produces.

---

## Invite flow

Use the invite flow when you control both sides of the handoff (e.g., you are provisioning an agent programmatically).

Every mailbox id in an invite's (or a device approval's) scope must be a live mailbox of the workspace: otherwise creation answers `400 mailbox_scope_invalid` naming the ids. If a mailbox is deleted after the invite was made, the claim answers the same `400` and the invite stays `pending` (it is not burned): restore the mailbox, or revoke it and create a new invite.

### Step 1: Create an invite

An invite can be created with an API key (`POST /v1/agent/adopt/invite`, what the CLI uses) or from a portal session (`POST /v1/me/adopt/invite`, what the portal's Welcome page uses). Creating invites is limited: 20 an hour per person, 40 an hour per workspace (past that, `429 rate_limited` with `Retry-After`), and a workspace can hold at most 50 open invites (`409 invite_limit_reached`; revoke some, or wait for them to expire). `label` is at most 100 characters.

```bash title="CLI"
molted adopt create --label "support-agent" \
  --mailbox <mailboxId> --permissions "read,send" --expires 1440
# or every current and future mailbox:
molted adopt create --label "my-agent" --all-mailboxes
```

```bash title="curl (API key)"
curl -X POST "https://mailbox.molted.email/v1/agent/adopt/invite" \
  -H "Authorization: Bearer mm_live_xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "label": "support-agent",
    "mailboxScopes": [
      { "mailboxId": "MAILBOX_UUID", "permissions": ["read", "send"] }
    ],
    "expiresMinutes": 1440
  }'
```

```bash title="curl (portal session)"
curl -X POST https://api.molted.email/v1/me/adopt/invite \
  -H "Cookie: YOUR_SESSION_COOKIE" \
  -H "Content-Type: application/json" \
  -d '{ "label": "my-agent", "scopeAllMailboxes": true }'
```

| Field | Type | Description |
| - | - | - |
| `label` | string | The key's label and the agent's name once claimed. Default `agent`. |
| `scopeAllMailboxes` | boolean | Grant every current and future mailbox. |
| `mailboxScopes` | array | `[{ mailboxId, permissions }]`, permissions from `read`, `send`, `manage`. Ignored when `scopeAllMailboxes` is `true`. |
| `expiresMinutes` | integer | 1 to 43200 (30 days). Default 1440 (24 hours). |

The scope is required: set `scopeAllMailboxes: true` or a `mailboxScopes` list with at least one entry granting `read`, `send` or `manage` (`--all-mailboxes` or `--mailbox` in the CLI). With neither, the API answers `400 {"error": "scope_required"}` instead of minting a key that could reach no mailbox, and the CLI refuses before sending anything. A permission other than `read`, `send`, `manage` is a `400` validation error. The invite is never defaulted to all mailboxes.

```json title="Response"
{
  "id": "invite-uuid",
  "tenantId": "tenant-slug-a1b2c3d4",
  "label": "support-agent",
  "flow": "invite",
  "status": "pending",
  "scopeAllMailboxes": false,
  "mailboxScopes": [{ "mailboxId": "MAILBOX_UUID", "permissions": ["read", "send"] }],
  "userCode": null,
  "expiresAt": "2026-04-01T12:00:00Z",
  "claimedAt": null,
  "approvedAt": null,
  "createdAt": "2026-03-31T12:00:00Z",
  "token": "ma_inv_..."
}
```

The full `token` is returned only once. Keep `id` if you may want to revoke the invite.

Who may create an invite:

- **An agent key** may only invite within its own scope: a mailbox-scoped key can't create an all-mailbox invite, or one for a mailbox or permission it doesn't have. Such an invite is a human decision (`403 human_decision_required`, `kind: "adopt-invite"`). See [Loosening a control](https://molted.email/docs/errors.md#loosening-a-control).
- **A portal session** held by a person always may. A session held by an agent is gated the same way as an agent key, for every invite.

### The portal's Connect an agent page

A person who would rather not use the CLI opens **Connect an agent** in the portal (`/app/welcome`, from the account menu or the command menu). The page creates an invite for them and shows it as a prompt to paste into an agent, or as the CLI command `molted adopt claim <code>`. See [Connect an agent](https://molted.email/docs/portal.md#connect-an-agent) for what the person sees. The invite it makes is:

- **Single-use and short-lived.** It expires after **1 hour** (`expiresMinutes: 60`), where an invite you create yourself defaults to 24 hours. The page shows the time left.
- **For every mailbox** (`scopeAllMailboxes: true`), with the label `Agent (connected from Welcome)`. That label becomes the key's label and the agent's name once it claims.
- **One at a time.** Reloading the same browser tab shows the same code. A new tab, or **New code**, first revokes the workspace's other pending invites with that label, so an agent still holding the older code gets the "replaced or revoked" `404`.

While the code is unclaimed the page polls `GET /v1/me/adopt/:id` for its status. After the agent claims it, the page lists what is still left for the person (connecting DNS, choosing a plan).

### Step 2: Claim the invite

The agent presents the token. No credentials are needed.

```bash title="CLI"
molted adopt claim ma_inv_...
```

The CLI stores the new key and tenant ID in the active profile (`~/.molted/profiles/<profile>.json`), then asks for the setup checklist (Step 3) and prints the next step:

```json title="CLI output"
{
  "message": "Token claimed - credentials stored",
  "tenant_id": "tenant-slug-a1b2c3d4",
  "label": "support-agent",
  "agent_id": "agent_...",
  "ready_to_send": false,
  "next_step": { "step": "plan", "owner": "human", "do": "..." },
  "hint": "Not ready to send yet. Run `molted doctor` for the full setup checklist, and again after each step."
}
```

```bash title="curl"
curl -X POST https://mailbox.molted.email/v1/adopt/claim \
  -H "Content-Type: application/json" \
  -d '{"token": "ma_inv_..."}'
```

```json title="Response"
{
  "apiKey": "mm_live_xxx",
  "tenantId": "tenant-slug-a1b2c3d4",
  "keyId": "key-uuid",
  "label": "support-agent",
  "agentId": "agent_...",
  "next": {
    "setup": "/v1/agent/setup",
    "hint": "Call GET /v1/agent/setup with this key next: ..."
  }
}
```

Save `apiKey` immediately -- it is only returned once. Use it as your `Authorization: Bearer` token for all agent API calls. `agentId` is missing if the automatic agent registration failed; the key works either way.

An unknown, already-claimed, or expired token returns `404 { "message": "Invalid or expired token" }`. A token that matches a specifically _revoked_ invite (a fresh invite replaced it, or the owner revoked it directly) returns a different `404` message: "This code was replaced or revoked. Ask your human for a new prompt." Request a fresh invite rather than retrying the same token.

### Step 3: Check your setup

`next` points at the agent's setup checklist. Call it with the new key:

```bash title="curl"
curl "https://mailbox.molted.email/v1/agent/setup" \
  -H "Authorization: Bearer mm_live_xxx"
```

It answers "can I send yet, and if not, what is next and whose move is it?": `readyToSend`, the single `next` step (`{ step, owner: "agent" | "human", do, request? }`) and every step's status. A fresh workspace is usually still on the trial plan, so `next` is the human's move, with the call that makes the link to hand them (a portal login link or a checkout link). Work the list until `readyToSend` is `true`; `next` is then a first send that works as given. See [Quickstart](https://molted.email/docs/quickstart.md#5-check-your-setup) for the full response. With the CLI, `molted adopt claim <token>` prints the next step itself, and `molted doctor` shows the whole checklist; over MCP, call `setup_status`.

---

## Device flow

Use the device flow when the agent cannot receive a token directly (e.g., a CLI running locally that needs approval from a person in the workspace).

### Step 1: Start the device flow

The agent starts the flow without any credentials.

```bash title="CLI"
molted adopt request --label "sales-agent"
```

The CLI prints the URL and code to stderr, polls every 5 seconds, and on approval stores the credentials (as `adopt claim` does) and prints `{ "message": "Adopted - credentials stored", "tenant_id", "label", "agent_id", "hint" }`. If the code expires first it exits 1.

```bash title="curl"
curl -X POST https://mailbox.molted.email/v1/adopt/device \
  -H "Content-Type: application/json" \
  -d '{"label": "sales-agent"}'
```

```json title="Response"
{
  "deviceCode": "ma_dev_...",
  "userCode": "K7MQ3X",
  "expiresAt": "2026-03-31T12:15:00Z",
  "verificationUrl": "https://molted.email/adopt/K7MQ3X"
}
```

`label` is optional (default `agent`, at most 64 characters; longer is a `400`). Starting a request is rate limited per client address (20 in 15 minutes) and per network (100): past that the route answers `429 rate_limited` with a `Retry-After` header, and `429 device_flow_busy` when your network already has 100 codes waiting. Claiming an invite is limited too (30 tries per client address in 10 minutes, 10 per invite code), so a mistyped token a few times is fine, a script guessing is not. Wait and retry. The `userCode` is 6 characters (letters and digits, never `0`, `O`, `1` or `I`), and the request expires after 15 minutes. Show the person `verificationUrl`; keep `deviceCode` to yourself, since it is what collects the key.

### Step 2: The person approves

The person opens `verificationUrl` (`/adopt/<userCode>` on the portal), signs in if needed, chooses all mailboxes (the default) or specific mailboxes and permissions, and approves. There is no reject button: a request nobody approves expires.

The portal calls this route, which you can also call from a portal session:

```bash title="curl (portal session)"
curl -X POST https://api.molted.email/v1/me/adopt/device/K7MQ3X/approve \
  -H "Cookie: YOUR_SESSION_COOKIE" \
  -H "Content-Type: application/json" \
  -d '{ "scopeAllMailboxes": true }'
```

The body takes `scopeAllMailboxes` and `mailboxScopes` as in [Create an invite](#step-1-create-an-invite). One of them is required here too: an approval with neither is refused with `400 scope_required`. An unknown, expired or already-approved code returns `404 "Token not found or expired"`. A session held by an agent is gated (`403 human_decision_required`, `kind: "adopt-invite"`), as for invites.

To look the request up first (its label and expiry), `GET /v1/me/adopt/device/:userCode/info`. Looking codes up (this route and the approval) is limited to 30 a signed-in user in 15 minutes, found or not; past that, `429 rate_limited`.

### Step 3: Poll for the key

```bash title="curl"
curl "https://mailbox.molted.email/v1/adopt/device/ma_dev_.../poll"
```

| `status` | Meaning |
| - | - |
| `pending` | Not approved yet. Poll again (the CLI waits 5 seconds). |
| `complete` | Approved. The response also carries `apiKey`, `tenantId`, `keyId`, `label`, `agentId` and `next`, the same fields as an invite claim. |
| `invalid` | Unknown, expired, revoked, or already collected. Start a new request. |

Poll every 5 seconds, no faster. Polling is limited to 20 a minute per `deviceCode`, 300 a minute per client address and 1,500 a minute per network (a /24, or a /48 for IPv6). The address and network numbers cover every request the start limits let you keep open at once (20 per address, 100 per network) polling every 5 seconds, with some room. Past a limit the route answers `429 rate_limited` with a `Retry-After` header; wait that long and poll again, the code stays valid (the CLI does this for you). Polling at the documented pace never gets near it.

The first poll after approval claims the key, so it is returned exactly once: a later poll with the same `deviceCode` answers `invalid`. Continue with [Check your setup](#step-3-check-your-setup).

---

## Managing adoptions

### List pending adoptions

Pending means not yet claimed and not expired: open invites, plus device requests that were approved but not yet collected.

```bash title="CLI"
molted adopt list
```

```
GET https://mailbox.molted.email/v1/agent/adopt/pending   (API key)
GET https://api.molted.email/v1/me/adopt/pending                               (portal session)
```

The response is an array of adoption records (the invite response above without `token`), newest first. An all-mailbox key sees every pending adoption in the workspace; a mailbox-scoped key sees only the invites it created.

The portal also checks a single invite's status with `GET /v1/me/adopt/:id` (`{ id, status, expiresAt }`, `status` one of `pending`, `claimed`, `revoked`, `expired`); a session held by an agent is gated there too.

### Revoke an adoption

```bash title="CLI"
molted adopt revoke <id>
```

```
DELETE https://mailbox.molted.email/v1/agent/adopt/:id   (API key)
DELETE https://api.molted.email/v1/me/adopt/:id                              (portal session)
```

Revoking returns `{ "revoked": true }` and stops a token that has not been claimed yet: a later claim gets the "replaced or revoked" `404`. It never touches a claimed token, or the key a claim produced (`404 "Token not found or already claimed"`): to cut off an agent that already claimed, revoke its key with `molted auth keys revoke <keyId>` (see [API keys](https://molted.email/docs/api-keys.md)). Revoking is a tightening, so it is never gated. A mailbox-scoped key can revoke only an invite it created; any other id gets the same `404`.

On the portal's Home, each pending invite shows as a card ("waiting to be claimed") with a countdown to its expiry. **Deny** revokes the invite. **Let it in** only opens the Connect an agent page, because an invite is claimed by the agent that holds the token, not approved by a person.

---

## Mailbox scopes

Both flows support mailbox scoping: `scopeAllMailboxes: true` for every mailbox, or `mailboxScopes` with per-mailbox `read`, `send` and `manage` permissions. The key a claim produces has exactly that scope. See [Authentication](https://molted.email/docs/authentication.md#mailbox-scoped-keys) for what each permission allows.

## Related reading

- [CLI reference: molted adopt](https://molted.email/docs/cli/adopt.md) -- every flag of the adopt commands
- [Authentication](https://molted.email/docs/authentication.md) -- session cookies, Bearer keys, and programmatic signup
- [Mailboxes](https://molted.email/docs/mailboxes.md) -- mailbox management
