# Monoxyde auth.md: authenticating as an AI agent

Monoxyde makes and schedules TikTok slideshows for app founders. An agent acts on a person's account with one of three credentials, all sent as `Authorization: Bearer <token>`:

1. **OAuth 2.1** (this page, below): for MCP clients and agents that can open a browser. The person signs in at https://monoxyde.com and approves exactly what the agent may do.
2. **The CLI's browser login**: `npx -y monoxyde-mcp login` opens https://monoxyde.com/app/mcp/connect, the person approves the code their terminal shows, and the CLI stores an API key.
3. **An API key** (`sk_mox_…`) the person creates in https://monoxyde.com/app/settings/mcp and puts in the `MONOXYDE_API_KEY` environment variable of the MCP server.

## Never

- Never ask the person to paste an API key, token or password into a chat. Use OAuth or the CLI login; a key belongs in the MCP server's environment, typed by the person.
- Never log, print or forward tokens to another service. Never send a token to any host but https://monoxyde.com. Access tokens are bound to the resource they were issued for (RFC 8707) and are refused elsewhere.
- Never keep using a refresh token after rotating it: every refresh returns a new one, and a refresh token presented twice revokes the whole connection.

## Discovery

- Authorization server metadata (RFC 8414): https://monoxyde.com/.well-known/oauth-authorization-server (issuer `https://monoxyde.com`)
- Protected resource metadata (RFC 9728): https://monoxyde.com/.well-known/oauth-protected-resource/api for the API (`https://monoxyde.com/api`), https://monoxyde.com/.well-known/oauth-protected-resource/api/mcp for the MCP endpoint (`https://monoxyde.com/api/mcp`)
- A request without a valid credential answers `401` with `WWW-Authenticate: Bearer resource_metadata="…"` naming that document.

```sh
curl -si -X POST https://monoxyde.com/api/mcp -H 'Content-Type: application/json' -d '{}' | grep -i www-authenticate
curl -s https://monoxyde.com/.well-known/oauth-protected-resource/api/mcp
curl -s https://monoxyde.com/.well-known/oauth-authorization-server
```

## Scopes

| Scope | Lets the agent |
|---|---|
| `slideshows:read` | See your slideshows, videos, products, photos, posting calendar and TikTok stats. |
| `slideshows:write` | Make, edit, schedule, post and delete slideshows and videos, and change your products and photos. Making slideshows uses your plan's allowance. |
| `library:read` | Browse the viral blueprints, hooks and inspiration posts. Opening a blueprint counts as it does in the app. |
| `account:read` | See your name, email address, plan and how many slideshows you have left. |

Reads need the matching `…:read` scope and every change needs `slideshows:write`. Ask only for what you need: the person sees each scope in plain words before approving. A request without `scope` asks for all four. It can never see or change your billing, API keys or connected apps, reach the admin panel, or delete your account. A missing scope answers `403` with `WWW-Authenticate: Bearer error="insufficient_scope", scope="…"`.

## The OAuth flow, step by step

Authorization code with PKCE (S256 only), refresh tokens with rotation, dynamic client registration. Loopback redirect URIs may use any port at authorization time (RFC 8252).

### 1. Register the client (RFC 7591)

Redirect URIs must be https, or http on `localhost`, `127.0.0.1` or `[::1]`. Public clients use `"token_endpoint_auth_method": "none"`; without it the default is `client_secret_basic` and the reply holds a `client_secret`. A client that nobody authorizes within a day is deleted.

```sh
curl -s -X POST https://monoxyde.com/api/oauth/register -H 'Content-Type: application/json' -d '{
  "client_name": "My agent",
  "redirect_uris": ["http://127.0.0.1:8976/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "token_endpoint_auth_method": "none"
}'
# → {"client_id": "mox_client_…", …}
```

### 2. Send the person to the authorization endpoint

```sh
VERIFIER=$(openssl rand -base64 48 | tr -d '=+/\n' | cut -c1-64)
CHALLENGE=$(printf '%s' "$VERIFIER" | openssl dgst -sha256 -binary | openssl base64 -A | tr '+/' '-_' | tr -d '=')
open "https://monoxyde.com/api/oauth/authorize?response_type=code&client_id=$CLIENT_ID&redirect_uri=http%3A%2F%2F127.0.0.1%3A8976%2Fcallback&scope=slideshows%3Aread%20slideshows%3Awrite%20library%3Aread%20account%3Aread&state=$(openssl rand -hex 16)&code_challenge=$CHALLENGE&code_challenge_method=S256&resource=https%3A%2F%2Fmonoxyde.com%2Fapi"
```

The person signs in if needed and sees your client's name, its redirect host and the scopes, then chooses Allow or Deny. Your redirect URI receives `code`, your `state` and `iss=https://monoxyde.com` (check both), or `error=access_denied`. The code works once, for 120 seconds. Use `resource=https://monoxyde.com/api/mcp` for a token that only the MCP endpoint accepts. A malformed request (no PKCE S256, an unknown scope or resource) is never redirected back to you: the person sees the error on our page, with `error` and `error_description` in its URL.

### 3. Exchange the code

```sh
curl -s -X POST https://monoxyde.com/api/oauth/token \
  -d grant_type=authorization_code -d code=$CODE -d code_verifier=$VERIFIER \
  -d client_id=$CLIENT_ID -d redirect_uri=http://127.0.0.1:8976/callback -d resource=https://monoxyde.com/api
# → {"access_token": "mox_at_…", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "mox_rt_…", "scope": "…"}
```

Access tokens last 1 hour; refresh tokens 30 days from their last use. Redeeming a code twice revokes everything it gave.

### 4. Call the API or the MCP endpoint

```sh
curl -s https://monoxyde.com/api/me -H "Authorization: Bearer $ACCESS_TOKEN"
```

The remote MCP endpoint is `https://monoxyde.com/api/mcp` (Streamable HTTP); the local server is `npx -y monoxyde-mcp`. The REST routes behind a token are the MCP server's contract and may change: prefer MCP tools.

### 5. Refresh

```sh
curl -s -X POST https://monoxyde.com/api/oauth/token \
  -d grant_type=refresh_token -d refresh_token=$REFRESH_TOKEN -d client_id=$CLIENT_ID
```

Store the new refresh token and drop the old one. You may pass `scope` to get an access token with fewer scopes.

### 6. Revoke (RFC 7009)

```sh
curl -s -X POST https://monoxyde.com/api/oauth/revoke -d token=$REFRESH_TOKEN -d client_id=$CLIENT_ID
```

Revoking a refresh token ends the whole connection; revoking an access token ends only that token. The person can also disconnect your agent in https://monoxyde.com/app/settings/mcp, under Connected apps.

## Agent registration without a browser (auth.md)

An agent that cannot open a browser registers itself and asks a person to claim it. The endpoints are in the `agent_auth` block of https://monoxyde.com/.well-known/oauth-authorization-server. Only the anonymous method is offered; nothing is emailed and no account is created by it.

1. Register. The reply holds a `registration_id`, an `identity_assertion` and a `claim_token`:

```sh
curl -s -X POST https://monoxyde.com/api/agent/identity -H 'Content-Type: application/json' -d '{"type":"anonymous"}'
```

2. Until it is claimed, the registration can read only itself (`GET https://monoxyde.com/api/agent/identity` with the token from `grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer`). It reaches no account, no library and no AI. It expires after 24 hours.
3. Start the claim with the email address of the person's account. The reply holds a 6-digit `user_code` and a `verification_uri`:

```sh
curl -s -X POST https://monoxyde.com/api/agent/identity/claim -H 'Content-Type: application/json' \
  -d '{"claim_token":"'$CLAIM_TOKEN'","email":"person@example.com"}'
```

4. Show the person the code and the link. They sign in with that address, type the code, choose what the agent may do and allow or deny it. The code is good for 10 minutes.
5. Poll the token endpoint every 5 seconds; it answers `authorization_pending`, `slow_down`, `access_denied` or `expired_token` as RFC 8628 does, then the tokens once:

```sh
curl -s -X POST https://monoxyde.com/api/oauth/token -d grant_type=urn:workos:agent-auth:grant-type:claim -d claim_token=$CLAIM_TOKEN
```

From then on the agent holds an ordinary grant with the scopes the person kept, listed and revocable in https://monoxyde.com/app/settings/mcp. Limits per IP address: 5 registrations a minute, 20 a day, 10 claim starts a minute.

## The CLI's browser login (device authorization, RFC 8628)

`npx -y monoxyde-mcp login` calls `POST https://monoxyde.com/api/mcp/pair`, opens the `verification_uri_complete` it gets back, and polls `POST https://monoxyde.com/api/mcp/pair/token` with the `device_code` every 5 seconds. Once the person approves the code in the browser, the poll returns an API key exactly once, which the CLI saves for the MCP server.

## Plans, limits and errors

- **Plan gating.** Signing in and connecting need no plan. Without one the library shows a preview, and anything that uses AI or makes slideshows answers `402` with `{"ok": false, "error": {"code": "NO_ACTIVE_PLAN", …}}` until the person subscribes (from $19.99 a month, no free tier); `402 LIMIT_REACHED` means the month's slideshows are used. Tell the person; never retry.
- **Per account:** 600 requests a minute, 60 a minute on the library routes, and daily ceilings on library reads that normal use never meets. Over a limit: `429` with `Retry-After`.
- **Per IP on the authorization server:** registration 10, authorization 30, token and revocation 60 requests a minute; 30 failed API key or token checks a minute.
- **Errors:** `401` the credential is missing, expired or revoked (OAuth: refresh, else authorize again); `403` the scope is missing or the route is for the web app only (keys, billing, connected apps, admin).
