# auth.md

You are an agent. Buildcord supports **agentic registration** ([auth.md](https://github.com/workos/auth.md)): discover → register → claim → exchange for an access_token → call the MCP server → renew or handle revocation. Follow the steps in order.

Buildcord builds and runs Discord bots. With an access_token you can use Buildcord's MCP server at `https://api.buildcord.dev/mcp` to read and change the person's bots: flows, designs, data and settings. You can't see their secrets or bot tokens, and you can't deploy.

## Step 1 — Discover

A request to `https://api.buildcord.dev/mcp` without a token answers `401` with:

```http
WWW-Authenticate: Bearer resource_metadata="https://api.buildcord.dev/.well-known/oauth-protected-resource/mcp", scope="bots:read bots:write"
```

### 1a. Protected Resource Metadata

```http
GET https://api.buildcord.dev/.well-known/oauth-protected-resource/mcp
```

```json
{
  "resource": "https://api.buildcord.dev/mcp",
  "authorization_servers": ["https://api.buildcord.dev/api/auth"],
  "scopes_supported": ["bots:read", "bots:write"],
  "bearer_methods_supported": ["header"]
}
```

### 1b. Authorization Server metadata

```http
GET https://api.buildcord.dev/.well-known/oauth-authorization-server/api/auth
```

The fields you need:

```json
{
  "issuer": "https://api.buildcord.dev/api/auth",
  "token_endpoint": "https://api.buildcord.dev/api/auth/oauth2/token",
  "grant_types_supported": ["…", "urn:ietf:params:oauth:grant-type:jwt-bearer", "urn:workos:agent-auth:grant-type:claim"],
  "agent_auth": {
    "skill": "https://buildcord.dev/auth.md",
    "identity_endpoint": "https://api.buildcord.dev/agent/identity",
    "claim_endpoint": "https://api.buildcord.dev/agent/identity/claim",
    "identity_types_supported": ["service_auth"]
  }
}
```

## Step 2 — Pick a method

Buildcord accepts **service_auth** only: you need the email address of the person whose Buildcord account you'll act for. `anonymous` answers `anonymous_not_enabled`, because bots always belong to a person, and `identity_assertion` answers `issuer_not_enabled`, because Buildcord trusts no ID-JAG issuers yet.

## Step 3 — Register

Before you register, tell the person you're connecting to Buildcord, and that you'll be able to read and change their bots, and ask for their email if you don't have it.

```http
POST https://api.buildcord.dev/agent/identity
Content-Type: application/json

{ "type": "service_auth", "login_hint": "person@example.com" }
```

```json
{
  "registration_id": "reg_…",
  "registration_type": "service_auth",
  "claim_url": "https://api.buildcord.dev/agent/identity/claim",
  "claim_token": "clm_…",
  "claim_token_expires": "2026-11-27T19:00:00.000Z",
  "post_claim_scopes": ["bots:read", "bots:write"],
  "claim": {
    "user_code": "123456",
    "expires_in": 600,
    "verification_uri": "https://app.buildcord.dev/claim?claim_attempt_token=cat_…",
    "interval": 5
  }
}
```

`claim_token` is returned once. Keep it in memory until Step 4 is done; don't persist it. The registration lasts 24 hours.

## Step 4 — Claim ceremony

### 4a. Hand off to the person

Send `verification_uri` and `user_code` in one message:

> Open this link, sign in to Buildcord (or sign up) as person@example.com, and enter this code on the page: **123456**
> https://app.buildcord.dev/claim?claim_attempt_token=…

The code goes into the Buildcord page, not back to you. Only the account with the address you registered can allow it, and only with a confirmed email.

### 4b. Poll for completion

Poll every `interval` seconds:

```http
POST https://api.buildcord.dev/api/auth/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=urn:workos:agent-auth:grant-type:claim&claim_token=clm_…
```

While waiting: `400 {"error": "authorization_pending"}`. Polling faster than `interval`: `400 {"error": "slow_down"}`; wait longer.

Once the person has entered the code:

```json
{
  "access_token": "…",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "bots:read bots:write",
  "identity_assertion": "…",
  "assertion_expires": "2026-12-26T19:00:00.000Z"
}
```

The claim token works for this once. Keep `identity_assertion` (it lasts 30 days) to renew the access_token in Step 5.

### 4c. A code that expired

A code works for 10 minutes, and for 5 wrong tries. When polling answers `expired_token`, ask for a new code with the same `claim_token`:

```http
POST https://api.buildcord.dev/agent/identity/claim
Content-Type: application/json

{ "claim_token": "clm_…", "email": "person@example.com" }
```

```json
{
  "registration_id": "reg_…",
  "status": "initiated",
  "expires_at": "2026-11-27T19:00:00.000Z",
  "claim_attempt": { "user_code": "654321", "expires_in": 600, "verification_uri": "https://app.buildcord.dev/claim?claim_attempt_token=cat_…", "interval": 5 }
}
```

Hand off the new code and link, and poll again. `410 claim_expired` means the 24 hours are up: register again.

## Step 5 — Renew the access_token

When the access_token expires, exchange the identity assertion for a new one:

```http
POST https://api.buildcord.dev/api/auth/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=<identity_assertion>&resource=https://api.buildcord.dev/mcp
```

```json
{ "access_token": "…", "token_type": "Bearer", "expires_in": 3600, "scope": "bots:read bots:write" }
```

`invalid_grant` means the assertion expired or the person disconnected you: register again at Step 3.

## Step 6 — Use the access_token

Connect to the MCP server (Streamable HTTP) with the token:

```http
POST https://api.buildcord.dev/mcp
Authorization: Bearer <access_token>
```

A `401` on a token that worked before: renew it once (Step 5). If that answers `invalid_grant`, register again (Step 3).

## Revocation

The person can disconnect you at any time from their Account page, under AI apps. Your access_token stops working at once, and renewing answers `invalid_grant`.

## Errors

| Code | Where | What to do |
| --- | --- | --- |
| `anonymous_not_enabled` | `/agent/identity` | Register with `service_auth`. |
| `issuer_not_enabled` | `/agent/identity` | Register with `service_auth`. |
| `invalid_request` | `/agent/identity`, `/agent/identity/claim` | Fix the body: `login_hint` must be the person's email; `email` must match it. |
| `rate_limited` (429) | `/agent/identity` | Wait a minute and retry. |
| `invalid_claim_token` | `/agent/identity/claim` | Register again. |
| `claimed_or_in_flight` | `/agent/identity/claim` | Already claimed: poll the token endpoint. |
| `claim_expired` (410) | `/agent/identity/claim` | Register again. |
| `authorization_pending` | token endpoint (claim grant) | Keep polling every `interval` seconds. |
| `slow_down` | token endpoint (claim grant) | Poll less often. |
| `expired_token` | token endpoint (claim grant) | Ask for a new code (Step 4c). |
| `invalid_grant` | token endpoint | Register again. |
| `invalid_target` | token endpoint | Use `resource=https://api.buildcord.dev/mcp`, or leave it out. |

More about Buildcord's MCP server and its tools: https://docs.buildcord.dev/ai-apps.md
