Protect your MCP server
Make your MCP server an OAuth 2.1 resource server that accepts Tessera access tokens. MCP clients (coding agents, Claude, Cursor) find Tessera from your server's metadata, sign the user in, and cal...
On this page
Make your MCP server an OAuth 2.1 resource server that accepts Tessera access tokens. MCP clients (coding agents, Claude, Cursor) find Tessera from your server's metadata, sign the user in, and call your tools with a token bound to your server.
Requires: Node.js 20 or later (or any runtime with the Fetch Request and Response types) and the jose package.
1. Register your server as a resource
An MCP server is a resource server: it checks tokens and holds no secret. What your realm must know is your server's URL (https://mcp.example.com below; use http://localhost:3001 while developing) and how MCP clients will identify themselves. MCP clients cannot register themselves until you turn that on. Both ways of doing it, Client ID Metadata Documents and Dynamic Client Registration, are off by default in every realm, and a realm administrator has to enable one (or both).
Your realm's name and issuer are on its page in the console, https://platform.kurthlabs.com. You need an admin API token for your realm: audience tessera-admin, scope tessera:realm:write. Then, with a realm administrator's sign-in (a workspace realm also asks for a passkey or authenticator code less than five minutes old):
export ISSUER=https://login.kurthlabs.com/realms/YOUR_REALM
export ADMIN=https://login.kurthlabs.com/admin/v1/realms/YOUR_REALM
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"
}'cimd_trusted_domainslists the domains whose clients may identify themselves with a Client ID Metadata Document, andcimd_allowed_resourceslists your server URL. Add one entry per server, and replace any entries already there only on purpose.- To accept Dynamic Client Registration instead, set
"dcr_policy"to"allowlist"(clients need an initial access token from you) or"open"(anyone can register: choose it deliberately). - Afterwards the realm's discovery document at
$ISSUER/.well-known/openid-configurationshowsclient_id_metadata_document_supportedand aregistration_endpoint. If they are missing, the setting did not take. - The full list of fields, who may change them and what each client may ask for is in protect an MCP server.
If you have the tessera CLI (during early access it is by invitation), tessera init --stack mcp-server --resource https://mcp.example.com adds the URL to cimd_allowed_resources and writes TESSERA_ISSUER and TESSERA_MCP_RESOURCE to .env.local. It does not turn on client registration: the step above is still yours. Without the CLI, set the two variables yourself, as shown under Environment.
2. Publish the metadata and check tokens
npm install josemcp-auth.ts
import { createRemoteJWKSet, jwtVerify } from "jose";
const issuer = process.env.TESSERA_ISSUER!;
const resource = process.env.TESSERA_MCP_RESOURCE!;
const jwks = createRemoteJWKSet(new URL(`${issuer}/jwks`));
const challenge = `Bearer resource_metadata="${resource}/.well-known/oauth-protected-resource"`;
const deny = () => new Response("unauthorized", { status: 401, headers: { "WWW-Authenticate": challenge } });
/** Serve at GET /.well-known/oauth-protected-resource */
export const metadata = () =>
Response.json({ resource, authorization_servers: [issuer], bearer_methods_supported: ["header"] });
/** Call first in your /mcp handler: returns the token claims, or a 401 Response. */
export async function authenticate(req: Request) {
const token = /^Bearer (.+)$/i.exec(req.headers.get("authorization") ?? "")?.[1];
if (!token) return deny();
try {
return (await jwtVerify(token, jwks, { issuer, audience: resource, typ: "at+jwt" })).payload;
} catch {
return deny();
}
}In your handler: const claims = await authenticate(req); if (claims instanceof Response) return claims;. The claims carry sub (the user or agent), scope, and act when an agent acts for a user.
What the checks do
- The signature comes from the realm's keys at
<issuer>/jwks(EdDSA or RS256). issmust be your realm;audmust be your resource, so a token minted for another server is refused.- The header
typmust beat+jwt, so an ID token is never accepted as an access token. - A missing or bad token gets
401with aWWW-Authenticateheader that points MCP clients at your metadata.
Environment
Set these in .env.local (tessera init writes them too):
TESSERA_ISSUER=https://login.kurthlabs.com/realms/YOUR_REALM
TESSERA_MCP_RESOURCE=https://mcp.example.com