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.
On this page
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. This page explains each part. Setup values (ISSUER, ADMIN) are on the overview.
What an MCP client does
- It calls your server with no token and gets
401with aWWW-Authenticateheader pointing at your resource metadata. - It reads that metadata (RFC 9728) and learns which authorization server to use.
- It reads the realm's discovery document:
$ISSUER/.well-known/openid-configuration(or$ISSUER/.well-known/oauth-authorization-server). - 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 for the exact setting.
- It sends the user to
$ISSUER/authorizewith PKCE (S256) andresource=<your server>. - The user signs in and approves access (consent).
- It checks that the response carries
issequal to the issuer it started with (RFC 9207), then redeems the code at$ISSUER/tokenwith the sameresourceand 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. - 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
tessera init --stack mcp-server --resource https://mcp.example.comThis 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:
{
"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/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). issis your realm's issuer. Compare it exactly; tokens carry the issuer they were minted under.audcontains your resource URL. A token minted for another server is refused.- The header
typisat+jwt, so an ID token is never taken for an access token. expandnbf, 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). The MCP quickstart 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):
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:
{
"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_idmust 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_resourcesand scopes inopenid,profile,email,offline_accessorcimd_scope_ceiling. Anything else isinvalid_targetorinvalid_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.
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"]
}'{
"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,httponlocalhostor a loopback address, or a private-use scheme that contains a dot (com.example.app:/callback). A bad one isinvalid_redirect_uri. A native client that picks a free port at run time may send anhttploopback IP address (127.0.0.1or[::1]) with any port, and it matches the registered one;localhostmust match exactly. - Auth methods are
none,client_secret_basic(default) andclient_secret_post. Grants areauthorization_code,refresh_tokenandclient_credentials. Asoftware_statementis refused. Scopes are cut down toopenid 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 atregistration_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:
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.