# TerraLab auth.md

How an AI agent gets access to TerraLab: the remote MCP server at `https://mcp.terra-lab.ai/mcp` and the REST API at `https://mcp.terra-lab.ai/v1`. Full page: https://terra-lab.ai/docs/authentication.md

## Who this is for

AI agents and MCP clients (Claude, ChatGPT, Cursor, Codex, VS Code or any agent) acting for a person who has, or creates, a TerraLab account. There is no anonymous access: every call counts against a person's account.

## Discover

1. Call `https://mcp.terra-lab.ai/mcp` without a token. It answers `401` with `WWW-Authenticate: Bearer resource_metadata="https://mcp.terra-lab.ai/.well-known/oauth-protected-resource", scope="data"`.
2. Read that protected resource metadata, then the authorization server metadata at https://terra-lab.ai/.well-known/oauth-authorization-server . Its `agent_auth` block lists the registration endpoints below.

| What | Value |
| --- | --- |
| Resource | `https://mcp.terra-lab.ai/mcp` |
| Authorization server (issuer) | `https://terra-lab.ai` |
| Scope | `data` |
| Client authentication | none (public clients) |
| Access token | 1 hour, renewed with a refresh token valid 30 days |
| Revocation | `POST https://terra-lab.ai/oauth/revoke` with your refresh token, or the person disconnects the agent at https://terra-lab.ai/dashboard/mcp |

Pick one method: agent registration when you cannot open a browser, OAuth 2.1 when you can.

## Method 1: agent registration with the person's email (`service_auth`)

For an agent that cannot open a browser or receive a redirect. You show the person a link and a 6-digit code; they sign in to TerraLab, or sign up, and type the code. TerraLab never emails the code.

Step 1. Ask the person for the email of their TerraLab account and tell them you are about to connect to TerraLab for them. Then register:

```http
POST https://terra-lab.ai/agent/identity
Content-Type: application/json

{ "type": "service_auth", "login_hint": "person@example.com", "client_name": "Your agent's name" }
```

`client_name` is optional (40 characters, letters and digits); the person sees it on the confirmation page. The answer (`200`):

```json
{
  "registration_id": "reg_...",
  "registration_type": "service_auth",
  "claim_url": "https://terra-lab.ai/agent/identity/claim",
  "claim_token": "clm_...",
  "claim_token_expires": "2026-11-14T13:00:00.000Z",
  "post_claim_scopes": ["data"],
  "claim": {
    "user_code": "123456",
    "expires_in": 600,
    "verification_uri": "https://terra-lab.ai/oauth/claim?t=...",
    "interval": 5
  }
}
```

Keep `claim_token` in memory only. It is returned once.

Step 2. Show the person `claim.verification_uri` and `claim.user_code` in one message, for example: "Open this link, sign in to TerraLab, and type this code on the page: 123456". The code goes into that page, never back to you. Only the account with the email you gave can confirm.

Step 3. Poll the token endpoint every `interval` seconds:

```http
POST https://terra-lab.ai/oauth/token
Content-Type: application/x-www-form-urlencoded

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

| Answer | Meaning |
| --- | --- |
| `authorization_pending` | The person has not confirmed yet. Keep polling. |
| `slow_down` | You poll faster than `interval`. Wait longer. |
| `expired_token` | The code ran out (10 minutes, or 5 wrong codes). Get a new one, see below. |
| `access_denied` | The person refused. Stop. |
| `200` | Confirmed: `access_token`, `refresh_token`, `client_id`, `expires_in`, `scope`. |

New code: `POST https://terra-lab.ai/agent/identity/claim` with JSON `{ "claim_token": "clm_...", "email": "person@example.com" }` returns a `claim_attempt` block with a new `user_code` and `verification_uri`; the previous link stops working. It answers `410 claim_expired` once the registration itself is over (1 hour): register again.

Step 4. Use the access token as `Authorization: Bearer <token>` on the MCP server and the REST API. Renew it with `grant_type=refresh_token&refresh_token=...&client_id=<the client_id from the token answer>` at the token endpoint; each refresh returns a new refresh token. If a `200` answer was lost, the same `claim_token` returns tokens again for 60 seconds.

Other registration types answer an error: `anonymous_not_enabled` for `anonymous`, `issuer_not_enabled` for an `identity_assertion` signed by an agent provider. `429 rate_limited`: back off and retry later.

## Check the account you act for

`https://terra-lab.ai` is itself a protected resource, described at https://terra-lab.ai/.well-known/oauth-protected-resource . One endpoint takes your access token:

```http
GET https://terra-lab.ai/oauth/userinfo
Authorization: Bearer <access token>
```

```json
{
  "sub": "8f1c...",
  "client_id": "agent_...",
  "scope": "data",
  "month": { "calls": 12, "included": 100, "remaining": 88, "resets_at": "2026-12-01T00:00:00.000Z" },
  "balance": { "currency": "EUR", "amount": 4.2, "micros": 4200000 },
  "top_up_needed": false,
  "top_up_url": "https://terra-lab.ai/dashboard/mcp"
}
```

`sub` is a stable, opaque id of the person's account; there is no email. When `top_up_needed` is true, the included calls are used and the balance cannot pay one more call: send the person to `top_up_url`. It accepts an access token minted for the MCP resource or one minted with `resource=https://terra-lab.ai`; the second kind is refused by the MCP server and the REST API. Without a valid token it answers `401` with `WWW-Authenticate: Bearer resource_metadata="https://terra-lab.ai/.well-known/oauth-protected-resource", scope="data"`, plus `error="invalid_token"` when the token is expired or revoked. API keys are not accepted here: use `https://mcp.terra-lab.ai/v1/me`.

## Method 2: OAuth 2.1 in a browser

1. Register the client: dynamic client registration at `https://terra-lab.ai/oauth/register`, or a client ID metadata document (the client ID is an HTTPS URL).
2. Send the person through the authorization code flow with PKCE (`S256`), scope `data`. They sign in or sign up at TerraLab and approve once.
3. Use the access token as `Authorization: Bearer <token>` on the MCP server and the REST API.

## API keys

Scripts and back ends can use an API key instead. The person creates it at https://terra-lab.ai/dashboard/mcp (Advanced, API keys). It starts with `tlk_` and is sent as `Authorization: Bearer <key>`. An agent never creates keys itself.

## Public without a token

- `https://mcp.terra-lab.ai/v1/openapi.json`
- `https://mcp.terra-lab.ai/v1/meta/pricing`

Terms: https://terra-lab.ai/legal/api-terms
