Skip to content

Authentication

The access token, how to check it, errors and introspection.

Mayfain is the OAuth 2.1 authorization server for every MCP server that trusts it. Your server is the resource server: it checks Mayfain's tokens, and never issues its own. Live.

Discovery

URL What it is
https://mayfain.ie/.well-known/oauth-authorization-server Authorization server metadata (RFC 8414): issuer, endpoints, jwks_uri, supported scopes
https://mayfain.ie/.well-known/jwks.json Mayfain's public signing keys
<your server>/.well-known/oauth-protected-resource Your protected resource metadata (RFC 9728). You publish this; it names Mayfain as the authorization server

Your protected resource metadata:

{
  "resource": "https://mcp.example-electrical.ie/mcp",
  "authorization_servers": ["https://mayfain.ie"],
  "scopes_supported": ["profile.basic", "applications.write", "applications.read"],
  "bearer_methods_supported": ["header"]
}

resource is your server's canonical URL. It must match the token's aud exactly.

The access token

A JWT (RFC 9068 profile), signed ES256 with a key from the JWKS. It lasts at most one hour.

{
  "iss": "https://mayfain.ie",
  "sub": "<pairwise subject>",
  "aud": "https://mcp.example-electrical.ie/mcp",
  "client_id": "mayfain-agent",
  "scope": "applications.write profile.basic",
  "grant_id": "6f1c…",
  "iat": 1792000000,
  "exp": 1792003600,
  "jti": "tok_…"
}
Claim Meaning
iss Always https://mayfain.ie
sub A pairwise subject: stable for this person at your server, different at every other server. Store this to recognise a returning person
aud Your server's canonical URL. A token for another server is useless here
client_id The agent the person used (mayfain-agent for Mayfain's own)
scope Space-separated scopes the person granted
grant_id The person's grant to your business. Use it with sub when you call Mayfain
exp Expiry, at most an hour after iat
jti The token's ID

Never forward the token to any other service, and never store it.

Checking a token

On every private tool call:

  1. Read Authorization: Bearer <token>. If it's missing, return 401 (below).
  2. Verify the signature against the JWKS (cache the keys for 10 minutes). Only accept ES256.
  3. Check iss is https://mayfain.ie, aud is your canonical URL, and exp is in the future.
  4. Check the token's scope contains every scope the tool requires. If not, return 403.
  5. For write and sensitive tools, call introspection and refuse unless active is true. A person can withdraw permission at any time, and introspection makes that instant.
import { createRemoteJWKSet, jwtVerify } from "jose";

const JWKS = createRemoteJWKSet(new URL("https://mayfain.ie/.well-known/jwks.json"), {
  cacheMaxAge: 10 * 60 * 1000,
});

export async function verifyMayfainToken(token: string, resource: string) {
  const { payload } = await jwtVerify(token, JWKS, {
    issuer: "https://mayfain.ie",
    audience: resource,
    algorithms: ["ES256"],
  });
  return payload as {
    sub: string; aud: string; scope: string; grant_id: string; client_id: string; exp: number; jti: string;
  };
}

Errors

No token, or a bad one:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://mcp.example-electrical.ie/.well-known/oauth-protected-resource",
                  scope="applications.write profile.basic"

A valid token without enough scope:

HTTP/1.1 403 Forbidden
WWW-Authenticate: Bearer error="insufficient_scope", scope="applications.write profile.basic",
                  resource_metadata="https://mcp.example-electrical.ie/.well-known/oauth-protected-resource"

Agents that support MCP authorization read resource_metadata, find Mayfain, and send the person to sign in and approve. ChatGPT also reads the same challenge from a tool result: return an error result with _meta["mcp/www_authenticate"] = [<the header value>].

Introspection

Live. POST https://mayfain.ie/oauth/introspect (RFC 7662), with your server's own credentials.

POST /oauth/introspect HTTP/1.1
Host: mayfain.ie
Authorization: Basic base64(<client_id>:<client_secret>)
Content-Type: application/x-www-form-urlencoded

token=<the access token>
{
  "active": true,
  "grant_id": "6f1c…",
  "scope": "applications.write profile.basic",
  "sub": "<pairwise subject>",
  "client_id": "mayfain-agent",
  "exp": 1792003600,
  "aud": "https://mcp.example-electrical.ie/mcp",
  "iss": "https://mayfain.ie",
  "token_type": "Bearer"
}

active: false means the grant was withdrawn, has expired, or the token isn't for your business. Refuse the call.

Your client_id and client_secret are issued by Mayfain when your server is registered. Keep them on the server only.