# Discourse ID Agent Authentication & Site Provisioning

Discourse ID (`https://id.discourse.com`) supports AI coding agents via OAuth 2.0 Device Authorization Grant (RFC 8628 — User-Claimed flow) to authenticate users and provision hosted Discourse communities (`free`, `pro`, `business`).

## Discovery Metadata

- Authorization Server Metadata (RFC 8414): `https://id.discourse.com/.well-known/oauth-authorization-server`
- Protected Resource Metadata (RFC 9728): `https://id.discourse.com/.well-known/oauth-protected-resource`
- Agent Skill: `npx skills add discourse/create-site`
- Repository & Playbook: https://github.com/discourse/create-site
- Discourse Overview (`llms.txt`): https://www.discourse.org/llms.txt

## Option 1 (Recommended): Official CLI (`@discourse/create-site`)

If you have Node.js (>= 20) and shell access, use the CLI in `--json` mode (exit codes: `0` = done, `1` = error, `2` = user action required):

```bash
npx -y @discourse/create-site create --json
```

Follow this workflow:
1. If unauthenticated (`code: "auth_required"`), share `verification_uri_complete` and `user_code` with the user (or run `npx -y @discourse/create-site auth start --signup --json` for a new Discourse ID account), then poll with `npx -y @discourse/create-site auth poll --json`.
2. Ask ONLY for **community name** (`--name`) and **plan** (`free`, `pro`, or `business`). Do not prompt for subdomain or region unless requested.
3. Fetch Step 2 terms (`npx -y @discourse/create-site create --name "<Community Name>" --plan <plan> --json`), show the exact `terms_urls`, and ask for explicit user agreement.
4. Provision with `--accept-terms` (and `--accept-account-terms` when `user_terms_needed` is `true`).
5. Share ONLY `redirect_url` as a clickable link (do not share the bare `https://<subdomain>.discourse.group` URL alongside it).
6. Instruct the user to enable MCP (`mcp_server_enabled`) at `mcp_setup.admin_mcp_url` (`<site_origin>/admin/config/mcp/settings`) after admin sign-in, then reply `"now add mcp support"`. Do not instruct them to run an external CLI command outside the session.
7. When they reply `"now add mcp support"`, follow `mcp_setup.agent_instructions` (and `mcp_setup.client_instructions` as reference) to configure `mcp_setup.mcp_endpoint`, then include:
   > Restart your coding agents after the setup. Most agents read their skills directory and MCP configuration at startup.

## Option 2: Direct HTTP / `curl` Flow

For environments without Node.js, use the public non-confidential OAuth `client_id` below:

- `client_id`: `sZzDY6daDItt71iT4w0Y2RBPn5Mo7eZS6m4Uyj1wWKM`
- `scope`: `read site_provision`

### 1. Start Device Authorization (RFC 8628)

```bash
curl -sS -X POST "https://id.discourse.com/oauth/authorize_device" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "client_id=sZzDY6daDItt71iT4w0Y2RBPn5Mo7eZS6m4Uyj1wWKM" \
  --data-urlencode "scope=read site_provision"
```

Show the user `verification_uri_complete` (append `&intent=signup` if they need a new Discourse ID account) and `user_code`, and ask them to approve in their browser.

### 2. Poll for Access Token

```bash
curl -sS -X POST "https://id.discourse.com/oauth/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:device_code" \
  --data-urlencode "client_id=sZzDY6daDItt71iT4w0Y2RBPn5Mo7eZS6m4Uyj1wWKM" \
  --data-urlencode "device_code=<DEVICE_CODE>"
```

Respect `interval` (default 5 seconds) and back off if `slow_down` is returned. Never print `device_code` or `access_token` back to the user.

### 3. Check User State & Available Plans

```bash
curl -sS "https://id.discourse.com/discourse_login/cli/info" \
  -H "Authorization: Bearer <ACCESS_TOKEN>"
```

### 4. Obtain Explicit Legal Terms Agreement

Before provisioning, show the exact plan terms URL(s) to the user and obtain explicit confirmation:
- Free plan (`free`): `https://discourse.org/free-terms`
- Pro / Business plans (`pro`, `business`): `https://discourse.org/trial-terms`
- Account & age terms (required whenever `user.age_terms_agreed` or `user.user_terms_agreed` is `false`): `https://discourse.org/user-terms`

### 5. Provision & Poll Until Ready

```bash
curl -sS -X POST "https://id.discourse.com/discourse_login/cli/sites" \
  -H "Authorization: Bearer <ACCESS_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{
    "community_name": "My Community",
    "subdomain": "my-community",
    "plan": "free",
    "region": "global",
    "legal_agreed": true,
    "age_terms_agreed": true,
    "user_terms_agreed": true
  }'
```

Then poll `GET https://id.discourse.com/discourse_login/cli/sites/<TOKEN>` until `status` is `"ready"`, and share ONLY `redirect_url` with the user.

### 6. Post-Provisioning Discourse MCP & Token Revocation

- Enable `mcp_server_enabled` at `<site_origin>/admin/config/mcp/settings`.
- Streamable HTTP MCP endpoint: `<site_origin>/mcp`
- Site MCP OAuth PRM: `<site_origin>/.well-known/oauth-protected-resource/mcp`
- Revoke Discourse ID token when finished:
  `POST https://id.discourse.com/oauth/revoke` (`token=<ACCESS_TOKEN>`, `client_id=sZzDY6daDItt71iT4w0Y2RBPn5Mo7eZS6m4Uyj1wWKM`).
