---
title: Protect an MCP server
summary: How to make an MCP server accept Tessera access tokens, how MCP clients find Tessera and register themselves, and how tokens stay bound to your server.
section: agents
order: 6
---
# Protect an MCP server

How to make an MCP server accept Tessera access tokens, how MCP clients find Tessera and register themselves, and how tokens stay bound to your server.

Tessera is the authorization server and your MCP server is the resource server. For the short version with code to paste, follow the [MCP server quickstart](/docs/quickstart/mcp-server.md). This page explains each part. Setup values (`ISSUER`, `ADMIN`) are on the [overview](/docs/agents/overview.md#calling-the-admin-api).

## What an MCP client does

1. It calls your server with no token and gets `401` with a `WWW-Authenticate` header pointing at your resource metadata.
2. It reads that metadata (RFC 9728) and learns which authorization server to use.
3. It reads the realm's discovery document: `$ISSUER/.well-known/openid-configuration` (or `$ISSUER/.well-known/oauth-authorization-server`).
4. It identifies itself with a Client ID Metadata Document (CIMD) or registers with Dynamic Client Registration (DCR). **Both are off by default in every realm.** Until a realm administrator enables one, MCP clients cannot sign in to your server: see [how clients identify themselves](#how-clients-identify-themselves) for the exact setting.
5. It sends the user to `$ISSUER/authorize` with PKCE (`S256`) and `resource=<your server>`.
6. The user signs in and approves access (consent).
7. It checks that the response carries `iss` equal to the issuer it started with (RFC 9207), then redeems the code at `$ISSUER/token` with the same `resource` and gets an access token whose audience is your server. A code works once: presenting it a second time is refused and revokes what the first use produced.
8. It calls your server with `Authorization: Bearer <token>`.

You build steps 1 and 2 and the check in step 8. Tessera does the rest.

## Register your server as a resource

```sh
tessera init --stack mcp-server --resource https://mcp.example.com
```

This adds the URL to the realm's `cimd_allowed_resources`, keeping the ones already there, and writes `TESSERA_ISSUER` and `TESSERA_MCP_RESOURCE` to `.env.local`. There is no secret: a resource server only checks signatures. Run it again for each additional URL. Use the public origin of the server, `http://localhost:3001` while developing.

## Publish metadata and refuse bad tokens

Serve `GET /.well-known/oauth-protected-resource`:

```json
{
  "resource": "https://mcp.example.com",
  "authorization_servers": ["https://login.kurthlabs.com/realms/acme"],
  "bearer_methods_supported": ["header"]
}
```

Answer a missing or bad token with `401` and a challenge that points at it:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="MCP", error="invalid_token", resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource"
```

Leave out `error="invalid_token"` when the request carried no token at all. Check these on every request:

- The signature, against the keys at `$ISSUER/jwks` (access tokens are signed with EdDSA).
- `iss` is your realm's issuer. Compare it exactly; tokens carry the issuer they were minted under.
- `aud` contains your resource URL. A token minted for another server is refused.
- The header `typ` is `at+jwt`, so an ID token is never taken for an access token.
- `exp` and `nbf`, with a little clock leeway.

The claims give you `sub` (the user, or the service), `scope`, `client_id` and, when an agent acts for a person, `act` (see [acting for a person](/docs/agents/acting-for-a-person.md#read-the-chain-in-your-api)). The [MCP quickstart](/docs/quickstart/mcp-server.md) has the whole check in code (the helper libraries are planned, not published).

## Audiences are fixed at sign-in (RFC 8707)

Every MCP token is bound to the server it was requested for. A CIMD client, or a static client created with `"is_mcp_client": true`, must send `resource` at `/authorize`; without it the request is refused. At the token endpoint (code and refresh) the `resource` it sends must equal the set bound at `/authorize`, or the answer is `400 invalid_target`. A static MCP client may only use its `allowed_resources`. Each refusal is audited as `mcp.audience_rejected` with a reason: `missing`, `unlisted`, `mismatch` or `delisted` (a resource removed from the client's list since sign-in).

`resource` is for resource servers like yours. An ordinary sign-in app that has no API to name sends none, and its access token is for its own `client_id`.

## How clients identify themselves

Both are off until you turn them on, per realm, with `PATCH $ADMIN` (scope `tessera:realm:write`):

```sh
curl -sS -X PATCH "$ADMIN" \
  -H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
  -d '{
    "cimd_policy": "trusted_domains_only",
    "cimd_trusted_domains": ["mcp-clients.example"],
    "cimd_allowed_resources": ["https://mcp.example.com"],
    "cimd_scope_ceiling": ["mcp:tools"],
    "dcr_policy": "disabled"
  }'
```

When enabled, discovery shows `client_id_metadata_document_supported` and `registration_endpoint`.

Changing these settings, `dcr_policy` `open` included, is the realm administrator's call: a Tessera operator, a workspace owner or admin, or a user with the `tessera:realm-admin` grant for the realm. The administrator can hand the decision to another user of the realm by granting `tessera:dcr:write` (for `dcr_policy`) or `tessera:cimd:write` (for the CIMD settings). Anyone else gets `403` `permission_required`. In a workspace realm a change also needs a sign-in under five minutes old with a second step (a passkey or an authenticator app); otherwise the answer is `401` `reauth_required`: sign in again and retry. Sending the value that is already stored is not a change and needs neither. If the setting changes while your request is being checked, the answer is `409`: reload the realm and try again. Each trusted domain must be a host name like `mcp-clients.example`, not a public suffix like `com` or `github.io` or a shared hosting domain like `amazonaws.com`, and a workspace realm can trust at most 20.

### Client ID Metadata Documents

The client's `client_id` is an HTTPS URL that serves its own metadata. Tessera fetches it when it first sees the client:

```json
{
  "client_id": "https://app.mcp-clients.example/client.json",
  "client_name": "Example MCP Client",
  "redirect_uris": ["https://app.mcp-clients.example/callback"],
  "token_endpoint_auth_method": "none",
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"]
}
```

- The URL's host must be a trusted domain or a subdomain of one (`cimd_trusted_domains`). The URL must be canonical: lower-case host, no port, a path other than `/`, no query or fragment.
- The document's `client_id` must equal the URL. It must be a public client (`none`), and redirect URIs follow the DCR rules below.
- Tessera does not follow redirects, refuses private addresses, waits 5 seconds and reads at most 1 MiB. It caches the document for its `Cache-Control: max-age`, clamped to the realm's limits (15 minutes minimum by default).
- A CIMD client can only request resources in `cimd_allowed_resources` and scopes in `openid`, `profile`, `email`, `offline_access` or `cimd_scope_ceiling`. Anything else is `invalid_target` or `invalid_scope`.
- A realm holds at most 500 CIMD clients.

### Dynamic Client Registration

`dcr_policy` is `disabled` (default), `allowlist` (needs an initial access token as a Bearer token) or `open`. The realm's administrator chooses it, `open` included, and can let someone else decide by granting them `tessera:dcr:write`. On the tenant plane a change needs a recent two-step sign-in. `open` lets anyone register a client, so choose it deliberately. A registration is limited per address and per realm per hour (429 beyond); in a workspace-owned realm those limits cannot be set above 1000 an hour.

```sh
curl -sS -X POST "$ISSUER/register" -H "Content-Type: application/json" -d '{
  "client_name": "Example MCP Client",
  "redirect_uris": ["http://127.0.0.1:8123/callback"],
  "token_endpoint_auth_method": "none",
  "grant_types": ["authorization_code", "refresh_token"]
}'
```

```json
{
  "client_id": "0192f7e0-1c3d-7e5f-8a9b-7c6d5e4f3a21",
  "client_id_issued_at": 1790000000,
  "client_secret_expires_at": 0,
  "registration_access_token": "shown-once",
  "registration_client_uri": "https://login.kurthlabs.com/realms/acme/register/0192f7e0-1c3d-7e5f-8a9b-7c6d5e4f3a21",
  "redirect_uris": ["http://127.0.0.1:8123/callback"],
  "token_endpoint_auth_method": "none",
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"]
}
```

- Redirect URIs must be `https`, `http` on `localhost` or a loopback address, or a private-use scheme that contains a dot (`com.example.app:/callback`). A bad one is `invalid_redirect_uri`. A native client that picks a free port at run time may send an `http` loopback IP address (`127.0.0.1` or `[::1]`) with any port, and it matches the registered one; `localhost` must match exactly.
- Auth methods are `none`, `client_secret_basic` (default) and `client_secret_post`. Grants are `authorization_code`, `refresh_token` and `client_credentials`. A `software_statement` is refused. Scopes are cut down to `openid profile email offline_access`.
- A registered client always requires PKCE, and its resource is the realm issuer unless the registration used an initial access token with `resource`. Widen a client afterwards with the admin API.
- The registration access token lets the client read (`GET`) or delete (`DELETE`) its own registration at `registration_client_uri`.

## Consent

Clients that registered themselves (DCR or CIMD) are third parties. Tessera never issues them a code until the user has approved every scope on every resource requested. The consent page names the client, the host of its redirect URI, the user's address, the scopes and the resources. Approval is per resource, and a request for more scope or a new resource asks again. Your own first-party apps never see the page. Consent decisions are audited as `consent.granted` and `consent.denied`.

## DPoP

By default a token is a bearer token: anyone who holds it can use it. DPoP (RFC 9449) binds it to a key the client holds. The client sends a signed proof in a `DPoP` header with its token request:

```http
POST /realms/acme/token HTTP/1.1
Host: login.kurthlabs.com
Content-Type: application/x-www-form-urlencoded
DPoP: eyJ0eXAiOiJkcG9wK2p3dCIsImFsZyI6IkVTMjU2Iiwi...
```

The answer has `"token_type": "DPoP"` and the access token carries `cnf.jkt`, the thumbprint of the client's key. Proofs use ES256, ES384 or EdDSA, must be less than 5 minutes old, and cannot be reused. Set `"dpop_required": true` on a client to refuse token requests without a proof.

Your server then accepts that token only as `Authorization: DPoP <token>` with a fresh proof on each request: its `htm` and `htu` match the request, `ath` is the SHA-256 of the token, and it is signed by the key in `cnf.jkt`. A bound token sent as `Bearer` gets `401` with `WWW-Authenticate: DPoP error="use_dpop_proof"`.

Check the signature and claims of the token first, then the proof (RFC 9449 section 4.3) with a DPoP library, and keep one replay cache of proof `jti` values per process. A token with `cnf.jkt` is accepted only with a valid proof.

## Stop a bad token early

Access tokens are checked offline, so a revoked token is normally noticed only when it expires. Turn on the revocation feed in your validator to notice within seconds. See [revocation and the kill switch](/docs/agents/revocation-and-kill-switch.md#revocation-feed).
