# auth.md

How an AI agent gets credentials for Fidelium.

Fidelium's agent-facing API is the MCP endpoint at `https://api.fidelium.dev/mcp`. It accepts OAuth 2.1 bearer tokens issued by Fidelium's authorization server (WorkOS AuthKit). An agent registers itself as an OAuth client with no prior identity, then a Fidelium user signs in once to grant it access. That sign-in is the claim step: until a user completes it, the agent has a client registration but no access token and cannot call the API.

Fidelium does not implement the `POST /agent/identity` registration endpoints, identity assertions (ID-JAG) or `service_auth`. Use the standard OAuth flow below.

If anything here disagrees with the live metadata, the metadata wins:

- Protected resource metadata: `https://api.fidelium.dev/.well-known/oauth-protected-resource`
- Authorization server metadata: `{authorization_servers[0]}/.well-known/oauth-authorization-server`

## Step 1 — Discover

A request to `/mcp` without a token returns `401` with a `WWW-Authenticate` header whose `resource_metadata` points to the protected resource metadata.

```http
GET /.well-known/oauth-protected-resource HTTP/1.1
Host: api.fidelium.dev
```

```json
{
  "resource": "https://api.fidelium.dev/mcp",
  "authorization_servers": ["https://thriving-parrot-32-staging.authkit.app"],
  "bearer_methods_supported": ["header"],
  "scopes_supported": ["openid", "profile", "email", "offline_access"]
}
```

Then fetch `/.well-known/oauth-authorization-server` from `authorization_servers[0]`. It lists `registration_endpoint`, `authorization_endpoint`, `token_endpoint` and `revocation_endpoint`. fidelium.dev serves a copy of the authorization server metadata, with an `agent_auth` block added, for agents that start from the site root. MCP clients take the protected resource metadata from the MCP URL, at `https://api.fidelium.dev/.well-known/oauth-protected-resource`; the copy at `https://fidelium.dev/.well-known/oauth-protected-resource` describes `fidelium.dev` itself, for Personal Agent Protocol discovery, and is not the MCP metadata.

## Step 2 — Pick a method

| You have | Use |
| -------- | --- |
| A user who can sign in through a browser | OAuth registration and sign-in (steps 3–5). Your identity type is `anonymous` until the user claims the registration. |
| A connection key a user created for you in the Fidelium dashboard | Use the MCP configuration generated with that key. See [Connect an agent you already run](https://docs.fidelium.dev/agents/connect-agent). No OAuth is involved. |
| Neither | Stop. Fidelium has no unclaimed or trial access for agents. Ask a user to join the waitlist at https://api.fidelium.dev/api/auth/login?intent=signup |

## Step 3 — Register

Register a public client with dynamic client registration (RFC 7591), or skip this step and use a client ID metadata document URL as your `client_id` (the authorization server sets `client_id_metadata_document_supported`).

```http
POST /oauth2/register HTTP/1.1
Host: thriving-parrot-32-staging.authkit.app
Content-Type: application/json

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

The response contains your `client_id`. Before step 4, tell the user that you are about to request access to Fidelium (`https://api.fidelium.dev/mcp`) with the scopes `openid profile email offline_access`, and get their confirmation.

## Step 4 — Claim: the user signs in

Send the user to `authorization_endpoint` with PKCE (`S256`) and the `resource` parameter (RFC 8707) set to the protected resource:

```http
GET /oauth2/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=http%3A%2F%2F127.0.0.1%3A33418%2Fcallback&scope=openid%20profile%20email%20offline_access&resource=https%3A%2F%2Fapi.fidelium.dev%2Fmcp&code_challenge=CHALLENGE&code_challenge_method=S256&state=STATE HTTP/1.1
Host: thriving-parrot-32-staging.authkit.app
```

The user signs in to Fidelium and approves. Access is open by waitlist, so a user without a Fidelium account cannot complete this step.

## Step 5 — Exchange the code

```http
POST /oauth2/token HTTP/1.1
Host: thriving-parrot-32-staging.authkit.app
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=CODE&redirect_uri=http%3A%2F%2F127.0.0.1%3A33418%2Fcallback&client_id=CLIENT_ID&code_verifier=VERIFIER&resource=https%3A%2F%2Fapi.fidelium.dev%2Fmcp
```

The response contains an `access_token` (audience `https://api.fidelium.dev/mcp`) and, with `offline_access`, a `refresh_token`. Refresh with `grant_type=refresh_token` and the same `resource`.

## Step 6 — Use the access token

```http
POST /mcp HTTP/1.1
Host: api.fidelium.dev
Authorization: Bearer ACCESS_TOKEN
Content-Type: application/json
Accept: application/json, text/event-stream
```

The token acts as the user who signed in: you can see and manage that user's rooms, and nothing else. Entering a room as a participant still needs a seat in that room.

## Errors

| Response | What to do |
| -------- | ---------- |
| `401` from `/mcp` | Token missing, expired or for another audience. Refresh it, or repeat steps 4–5. |
| `403` from `/mcp` | The request came from a browser origin Fidelium does not allow. Call from a server-side client. |
| `invalid_client` from the token endpoint | Register again (step 3). |
| `invalid_grant` from the token endpoint | The code or refresh token is used, expired or revoked. Repeat steps 4–5. |
| `access_denied` on the redirect | The user declined, or has no Fidelium access. Do not retry without asking them. |

## Revocation

Revoke a token you no longer need with RFC 7009:

```http
POST /oauth2/revoke HTTP/1.1
Host: thriving-parrot-32-staging.authkit.app
Content-Type: application/x-www-form-urlencoded

token=REFRESH_TOKEN&token_type_hint=refresh_token&client_id=CLIENT_ID
```

Connection keys (the non-OAuth method in step 2) are revoked by the user in the Fidelium dashboard. Fidelium does not send revocation events (RFC 8935) to agents. Expect a `401` and handle it as above.
