# auth.md

Authentication guide for the Agent Registrar identity broker.

- Production host: `https://agentregistrar.dev`
- Isolated public sandbox broker: `https://alpha.agentregistrardemobroker.com` (workbench at `https://agentregistrardemobroker.com/workbench`)

Agent Registrar authenticates MCP clients via OAuth 2.1 with PKCE (`S256`) and authenticates provider integration routes (`/api/provider/v1/*`) via Provider API keys issued in the Provider Portal. The broker issues short-lived identity assertions and records delegated terms-of-service acceptances; it does not store or proxy relying-party credentials.

## 1. Discover

Start by calling the protected MCP endpoint without a token or fetching the discovery metadata documents:

1. `POST https://agentregistrar.dev/mcp` without credentials returns `401 Unauthorized` with a `WWW-Authenticate` header pointing to `https://agentregistrar.dev/.well-known/oauth-protected-resource`.
2. Fetch `GET https://agentregistrar.dev/.well-known/oauth-protected-resource` (RFC 9728) to confirm `resource` (`https://agentregistrar.dev/mcp`), `authorization_servers`, and supported scopes.
3. Fetch `GET https://agentregistrar.dev/.well-known/oauth-authorization-server` (RFC 8414) for `authorization_endpoint`, `token_endpoint`, `registration_endpoint`, `jwks_uri`, and `client_id_metadata_document_supported: true`.
4. Fetch `GET https://agentregistrar.dev/.well-known/mcp/server-card.json` for the machine-readable MCP Server Card before connecting.

## 2. Pick a method

Choose the credential flow that matches your caller role:

- MCP agents and developer harnesses (OAuth 2.1 + PKCE): use Client ID Metadata Documents (CIMD) if your harness publishes an HTTPS client metadata URL, or Dynamic Client Registration (DCR) if it needs a registered `client_id`.
- Software providers calling `/api/provider/v1/*`: create a scoped Provider API key in the Provider Portal (`https://agentregistrar.dev/provider`) under an onboarded provider account and send it as `Authorization: Bearer <provider_api_key>`.
- Sandbox testing: use the isolated sandbox workbench at `https://agentregistrardemobroker.com/workbench` and sandbox MCP endpoint `https://alpha.agentregistrardemobroker.com/mcp` to test OAuth consent, agent binding, and brokered signups without affecting production accounts.

## 3. Register

### Option A: Client ID Metadata Document (CIMD)

If your MCP harness hosts an HTTPS Client ID Metadata Document, pass that HTTPS metadata URL directly as `client_id` in the authorization and token requests without calling `/api/oauth/register`.

### Option B: Dynamic Client Registration (DCR)

Register an OAuth client with `POST https://agentregistrar.dev/api/oauth/register`:

```http
POST /api/oauth/register HTTP/1.1
Host: agentregistrar.dev
Content-Type: application/json

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

Response (`201 Created`):

```json
{
  "client_id": "cli_01j...",
  "client_id_issued_at": 1700000000,
  "client_name": "Example Agent Harness",
  "redirect_uris": ["http://127.0.0.1:43123/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none"
}
```

## 4. Claim

Direct the human owner to `GET https://agentregistrar.dev/oauth/authorize` with `client_id`, `redirect_uri`, `response_type=code`, `code_challenge`, `code_challenge_method=S256`, `scope`, and `state`.

During consent at `/oauth/authorize`, the human owner:

1. Completes a one-time verified-email check if they are new (no KYC is required).
2. Reviews and accepts the current Master Agent Delegation Agreement.
3. Binds the grant to a specific agent sub-identity (or creates a new agent sub-identity) before the broker redirects back to `redirect_uri` with `code` and `state`.

## 5. Exchange

Exchange the one-time authorization code and PKCE `code_verifier` at `POST https://agentregistrar.dev/api/oauth/token` (accepts `application/x-www-form-urlencoded` or `application/json`):

```http
POST /api/oauth/token HTTP/1.1
Host: agentregistrar.dev
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=<authorization_code>&redirect_uri=http%3A%2F%2F127.0.0.1%3A43123%2Fcallback&client_id=cli_01j...&code_verifier=<pkce_verifier>
```

Response (`200 OK`, `Cache-Control: no-store`):

```json
{
  "access_token": "mca_...",
  "token_type": "Bearer",
  "expires_in": 86400,
  "refresh_token": "mcr_...",
  "scope": "mcp:tools tos:accept signup:free_tier"
}
```

## 6. Use the access_token

Send the issued `access_token` as a Bearer token against the Streamable HTTP MCP endpoint:

```http
POST /mcp HTTP/1.1
Host: agentregistrar.dev
Authorization: Bearer <access_token>
Content-Type: application/json
```

- Supported MCP OAuth scopes: `mcp:tools`, `tos:accept`, `signup:free_tier`.
- Rate-limit headers: public broker verification, receipt, abuse, and Provider v1 endpoints return `RateLimit` and `RateLimit-Policy` response headers.

## 7. Errors

- HTTP errors: broker API endpoints return JSON with `{ "error": "...", "error_description": "...", "code": "..." }` (OAuth endpoints return `{ "error": "...", "error_description": "..." }`).
- HTTP status codes: `401` indicates missing, expired, or invalid credentials (with `WWW-Authenticate` on `/mcp`); `403` indicates a missing scope, role, entitlement, or suspended account; `429` indicates rate limiting and includes a `Retry-After` header alongside `RateLimit` and `RateLimit-Policy`.
- MCP tool failures: tool calls return `isError: true` with a JSON text envelope containing `{ "error": { "code", "message", "retryable", "side_effects", "next_step", "docs" } }`. Gate errors such as `human_suspended`, `human_email_unverified`, `master_tos_unsigned`, `agent_revoked`, and `scope_not_granted` require human action and are not retryable; transient codes such as `ledger_email_failed` or `wait_timeout` report `retryable: true` and exact `side_effects`.

## 8. Revocation

- Refresh-token rotation: exchange a refresh token at `POST https://agentregistrar.dev/api/oauth/token` with `grant_type=refresh_token`, `refresh_token`, and `client_id`. Refresh tokens rotate on each use and prior tokens are invalidated.
- Per-agent kill switch: a human owner can suspend or revoke an individual agent sub-identity at `https://agentregistrar.dev/agents/[id]`, immediately blocking future identity assertions and MCP tool calls for that agent at the broker.
- Provider API keys: revoke or rotate scoped Provider API keys in the Provider Portal at `https://agentregistrar.dev/provider`.
- Relying-party session independence: revoking an agent or OAuth token at the broker stops future broker assertions and tool calls, while each relying party enforces local session revocation independently.
- Isolated sandbox: test consent, token exchange, and revocation flows without affecting production accounts at `https://agentregistrardemobroker.com/workbench` (MCP endpoint `https://alpha.agentregistrardemobroker.com/mcp`).

## 9. Pluggable `auth.md` Identity Provider (OAuth ID-JAG)

Services that already implement `auth.md` (RFC 9728 Protected Resource Metadata + RFC 8414 Authorization Server Metadata with `agent_auth`) can accept Agent Registrar as a trusted `oauth-id-jag+jwt` identity provider without rewriting existing signup endpoints.

- Broker issuer (`iss`): `https://agentregistrar.dev`
- Public JWKS (`jwks_uri`): `https://agentregistrar.dev/.well-known/jwks.json`
- Key types hosted in JWKS: active `ES256` (EC P-256) keys for `oauth-id-jag+jwt` and `secevent+jwt`, alongside active `EdDSA` (Ed25519) keys for native `agent-id+jwt` assertions.

### Trusted issuer configuration

Add `https://agentregistrar.dev` to your service's `trusted_issuers` allowlist:

```json
{
  "trusted_issuers": [
    {
      "issuer": "https://agentregistrar.dev",
      "jwks_uri": "https://agentregistrar.dev/.well-known/jwks.json",
      "alg": "ES256",
      "typ": "oauth-id-jag+jwt"
    }
  ]
}
```

### `oauth-id-jag+jwt` claim mapping

When an MCP agent calls `identity_signup` or `identity_login` for an `auth.md` origin (after human approval at `/approvals/auth-md/[id]`), Agent Registrar mints a short-lived `ES256` JWT (`typ: "oauth-id-jag+jwt"`) with:

- `iss`: `https://agentregistrar.dev`
- `sub`: verified human account UUID at Agent Registrar (consistent across agents owned by the same human)
- `aud`: Authorization Server `issuer` from your RFC 8414 metadata
- `resource`: Protected Resource Metadata `resource` URI
- `client_id`: resolved from `agent_auth.client_id` or `agent_auth.default_client_id` (falling back to the service origin)
- `email`: verified human email address
- `email_verified`: `true`
- `auth_time`: unix epoch seconds of the human authentication event
- `agent`: optional object (`{ id, name, email, harness }`) plus optional flat hints (`agent_platform`, `agent_context_id`, `agent_email`, `agent_name`)

### Optional `op_tos_uri` terms hashing

If your RFC 8414 Authorization Server Metadata advertises `op_tos_uri` (or `agent_auth.terms.url`), Agent Registrar fetches those terms bytes, computes a SHA-256 hash, embeds `delegation` (`master_tos_version`, `accepted_rp_tos_url`, `accepted_rp_tos_hash`, `accepted_at`) in the ID-JAG assertion, and emails a terms receipt to both the human owner and the agent inbox. When `op_tos_uri` is omitted or unfetchable, Agent Registrar records no terms hash and never fabricates one.

### Optional RFC 8935 SET revocation push

If your metadata advertises `event_notification_endpoint` or `set_endpoint` (or `events_endpoint`), revoking or suspending an agent at Agent Registrar sends a best-effort signed RFC 8935 / CAEP `secevent+jwt` (`Content-Type: application/secevent+jwt`, event `https://schemas.openid.net/secevent/caep/event-type/session-revoked`) with `iss: "https://agentregistrar.dev"`, `aud` equal to your Authorization Server `issuer`, and `sub_id` (`iss_sub`) matching `sub`.

Because `sub` identifies the verified human owner, multiple active agents belonging to the same human may share one `sub` at your service. When other active agents for that human have also asserted against the same service, revoking one agent stops its broker assertions immediately and skips the automatic SET push by default so sibling agents' sessions are not disrupted; the human owner can trigger an explicit full-service revocation from `/agents/[id]`. SET delivery is best-effort, and each relying party enforces local session revocation independently.

### Guides and demo network

- Provider `auth.md` integration guide: `https://docs.agentregistrar.dev/providers/auth-md/`
- Live demo `auth.md` relying party: `https://authmd.agentregistrardemorp.com`
- Demo network hub and sandbox workbench: `https://agentregistrardemobroker.com`
