# auth.md

You are an agent. This page tells you how to get credentials for AIsa and use them. It follows the step order of the [auth.md walkthrough](https://github.com/workos/auth.md): discover, pick a method, register, claim, exchange, use the access_token, handle errors and revocation.

AIsa has two resource servers:

- `https://mcp.aisa.one/mcp`: the MCP server (Streamable HTTP). Sign in with OAuth 2.1, or send an AIsa API key.
- `https://api.aisa.one`: the REST API. Data APIs under `/apis/v1`, OpenAI-compatible LLMs under `/v1`, Gemini GenerateContent under `/v1beta`. Send an AIsa API key. Paths and schemas: [OpenAPI](https://aisa.one/openapi.json), [API reference](https://aisa.one/docs/api-reference).

AIsa does not run the `agent_auth` registration profile (no `identity_endpoint`, `identity_assertion`, ID-JAG or `service_auth` flow). An agent gets access in one of the three ways in Step 2: a person approves an OAuth sign-in or creates an API key, or the agent pays per request.

## Step 1 — Discover

Call the MCP server without a token. The 401 response names its Protected Resource Metadata (RFC 9728):

```http
POST /mcp HTTP/1.1
Host: mcp.aisa.one

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="OAuth", resource_metadata="https://mcp.aisa.one/.well-known/oauth-protected-resource/mcp", scope="profile email offline_access", error="invalid_token"
```

### 1a. Fetch the Protected Resource Metadata

```http
GET https://mcp.aisa.one/.well-known/oauth-protected-resource/mcp
```

```json
{
  "resource": "https://mcp.aisa.one/mcp",
  "authorization_servers": ["https://clerk.aisa.one"],
  "scopes_supported": ["profile", "email", "offline_access"],
  "bearer_methods_supported": ["header"],
  "resource_name": "AIsa"
}
```

### 1b. Fetch the Authorization Server metadata

```http
GET https://clerk.aisa.one/.well-known/oauth-authorization-server
```

It lists `authorization_endpoint` (`https://clerk.aisa.one/oauth/authorize`), `token_endpoint` (`https://clerk.aisa.one/oauth/token`), `registration_endpoint` (`https://clerk.aisa.one/oauth/register`, Dynamic Client Registration, RFC 7591), `revocation_endpoint` (`https://clerk.aisa.one/oauth/token/revoke`), the grant types `authorization_code` and `refresh_token`, and PKCE with `S256`.

The REST API at `https://api.aisa.one` takes an API key. A request without one gets `401` with `missing_api_key`, except on data routes that can be paid per request: those answer `402` with a payment challenge (Step 2, method 3).

## Step 2 — Pick a method

1. **Your client speaks MCP and can open a browser** → OAuth on the MCP server. No key needed. Go to Step 3a.
2. **No browser (CI, SSH, a container), or you call the REST API from code** → an AIsa API key that the user creates. Go to Step 3b.
3. **No account, and the route can be paid per request** → call it without a key and answer the `402` challenge. Under `https://api.aisa.one/apis/v1` the challenge is [MPP](https://mpp.dev) (`WWW-Authenticate: Payment`); under `https://api.aisa.one/apis/v2` it is [x402](https://www.x402.org) version 2 (`PAYMENT-REQUIRED` header). Pay as that protocol describes and retry. No Step 3 to 5.

Before you go on, tell the user in one line which method you picked: for 1 or 2, that an AIsa sign-in or key page is about to open; for 3, that each call will be paid from your wallet.

## Step 3 — Register

### 3a. OAuth client (MCP)

Most MCP clients register themselves: they read the metadata from Step 1 and post to `registration_endpoint`. To register by hand (the redirect URI is an example):

```http
POST https://clerk.aisa.one/oauth/register
Content-Type: application/json

{
  "client_name": "Your agent",
  "redirect_uris": ["http://127.0.0.1:33418/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none"
}
```

Keep the `client_id` from the response.

### 3b. API key

Ask the user to create a key at https://console.aisa.one/api-keys and set it as `AISA_API_KEY` in your environment. Never ask the user to paste the key into chat. Never print, log or commit it. A client with no MCP support can use the CLI instead: `npm install -g @aisa-one/cli && aisa login` (Node.js 18+).

## Step 4 — Claim

The person who owns the AIsa account approves every credential. There is no separate claim ceremony and no code to poll for:

- **OAuth:** open `authorization_endpoint` with `response_type=code`, your `client_id` and `redirect_uri`, `scope=profile email offline_access`, a `state`, and a PKCE `code_challenge` with `code_challenge_method=S256`. The user signs in to AIsa and clicks **Allow** (one click if the browser is already signed in). Your redirect URI receives the `code`.
- **API key:** the user creates the key in the console. Wait until they say it is set.

Do not call the API while you wait. Calls before sign-in only return `401`.

## Step 5 — Exchange

Exchange the authorization code for tokens:

```http
POST https://clerk.aisa.one/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=<code>&redirect_uri=<redirect_uri>&client_id=<client_id>&code_verifier=<code_verifier>
```

The response carries `access_token`, `expires_in` and, with `offline_access`, a `refresh_token`. An API key needs no exchange.

## Step 6 — Use the access_token

Send the OAuth access token or the API key as a Bearer token:

```http
POST https://mcp.aisa.one/mcp
Authorization: Bearer <access_token or AISA_API_KEY>
```

```http
GET https://api.aisa.one/v1/credits/balance
Authorization: Bearer <AISA_API_KEY>
```

Then make one free call to check the credential: the MCP tool `account`, `aisa balance` in the CLI, or `GET https://api.aisa.one/v1/credits/balance` (200 with the balance). A tool list or a 401 does not prove access.

**Refresh.** When the access token expires, post `grant_type=refresh_token&refresh_token=<refresh_token>&client_id=<client_id>` to `token_endpoint`. An API key needs no refresh.

The full setup, the tools and the spending rules: https://aisa.one/llms.txt.

## Errors

| Code                    | Where               | What to do                                                                                                                                                                                                        |
| ----------------------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `401` `invalid_token`   | MCP server          | No token, or it expired. Refresh it once (Step 6). If that fails, restart at Step 1.                                                                                                                              |
| `401` `missing_api_key` | REST API            | No key was sent. Set `AISA_API_KEY` (Step 3b).                                                                                                                                                                    |
| `401` `invalid_api_key` | REST API            | The key is wrong or revoked. Ask the user for a new key.                                                                                                                                                          |
| `402`                   | REST API, MCP tools | Low balance, or the operation needs the Go-to-Market plan. The response says which and links to the fix. Tell the user; do not retry. Without a key, a `402` is a pay-per-request challenge: pay it or set a key. |
| `429`                   | REST API            | Rate limited. Wait and retry with backoff. https://aisa.one/docs/api-reference/rate-limits                                                                                                                        |
| `invalid_grant`         | `token_endpoint`    | The code or refresh token expired or was revoked. Restart at Step 4.                                                                                                                                              |
| `invalid_client`        | `token_endpoint`    | The `client_id` is unknown. Register again (Step 3a).                                                                                                                                                             |

Retry 5xx responses with exponential backoff. Do not retry a 4xx with the same request, and do not retry a failed paid call without telling the user.

## Revocation

- **OAuth tokens:** post `token=<access_token or refresh_token>` (form-encoded) with your `client_id` to `https://clerk.aisa.one/oauth/token/revoke` (RFC 7009).
- **API keys:** the user deletes the key at https://console.aisa.one/api-keys. Every later call with it returns `401` `invalid_api_key`.

On a `401` for a credential that worked before, refresh once (OAuth). If the refresh fails, or the credential is an API key, ask the user to sign in again or create a new key.
