FastMCP provides utility classes for authentication, token management, and OAuth flows.
JWTIssuer
Issues and validates HS256-signed JWTs for the OAuth proxy.
import { JWTIssuer } from "fastmcp" ;
const issuer = new JWTIssuer ({
issuer: "https://api.example.com" ,
audience: "https://api.example.com" ,
signingKey: process . env . JWT_SIGNING_KEY ! ,
accessTokenTtl: 3600 , // 1 hour
refreshTokenTtl: 2592000 , // 30 days
});
// Issue access token
const accessToken = issuer . issueAccessToken (
"client-123" ,
[ "read" , "write" ],
{ role: "admin" }, // Additional claims
);
// Issue refresh token
const refreshToken = issuer . issueRefreshToken (
"client-123" ,
[ "read" , "write" ],
);
// Verify token
const result = await issuer . verify ( accessToken );
if ( result . valid ) {
console . log ( "Claims:" , result . claims );
}
Constructor
Issuer identifier (iss claim)
Audience identifier (aud claim)
Secret key for signing tokens (HS256)
Access token expiration in seconds
Refresh token expiration in seconds (30 days)
Methods
issueAccessToken()
Custom claims to include in JWT
Override default TTL (seconds)
HS256-signed JWT access token
issueRefreshToken()
Custom claims to include in JWT
Override default TTL (seconds)
HS256-signed JWT refresh token
verify()
interface TokenValidationResult {
valid : boolean ;
claims ?: JWTClaims ;
error ?: string ;
}
deriveKey() (static)
Derive a signing key from a secret using PBKDF2.
const derivedKey = await JWTIssuer . deriveKey ( "my-secret" , 100000 );
Secret to derive key from
Base64-encoded derived key
JWKSVerifier
Verifies JWTs using public keys from a JWKS endpoint.
import { JWKSVerifier } from "fastmcp" ;
const verifier = new JWKSVerifier ({
jwksUri: "https://accounts.google.com/.well-known/jwks.json" ,
audience: "your-client-id" ,
issuer: "https://accounts.google.com" ,
cacheDuration: 3600000 , // 1 hour
cooldownDuration: 30000 , // 30 seconds
});
const result = await verifier . verify ( idToken );
if ( result . valid ) {
console . log ( "User:" , result . claims ?. sub );
}
Note: Requires the jose package: npm install jose
Constructor
config
JWKSVerifierConfig
required
Cache duration in milliseconds (1 hour)
Cooldown between refetches in milliseconds (30 seconds)
Methods
verify()
interface TokenVerificationResult {
valid : boolean ;
claims ?: Record < string , unknown >;
error ?: string ;
}
refreshKeys()
Force refresh of JWKS cache.
await verifier . refreshKeys ();
getJwksUri()
Get the JWKS URI being used.
const uri = verifier . getJwksUri ();
PKCEUtils
PKCE (Proof Key for Code Exchange) utilities for OAuth 2.0.
import { PKCEUtils } from "fastmcp" ;
// Generate verifier and challenge
const { verifier , challenge } = PKCEUtils . generatePair ( "S256" );
// Validate challenge
const valid = PKCEUtils . validateChallenge ( verifier , challenge , "S256" );
Static Methods
generatePair()
Generate a complete PKCE pair (verifier + challenge).
method
'S256' | 'plain'
default: "S256"
Challenge method
interface PKCEPair {
verifier : string ; // Base64URL-encoded (43-128 chars)
challenge : string ; // Base64URL-encoded or plain
}
generateVerifier()
Generate a cryptographically secure code verifier.
Verifier length (43-128 characters)
Base64URL-encoded random string
generateChallenge()
Generate a code challenge from a verifier.
method
'S256' | 'plain'
default: "S256"
Challenge method
Base64URL-encoded challenge (S256) or verifier (plain)
validateChallenge()
Validate a code verifier against a challenge.
Code verifier to validate
Whether verifier matches challenge
ConsentManager
Manages consent screens and cookie signing for OAuth flows.
import { ConsentManager } from "fastmcp" ;
const manager = new ConsentManager ( "signing-key-secret" );
// Create consent response
const response = manager . createConsentResponse ( transaction , "Google" );
// Sign consent cookie
const cookie = manager . signConsentCookie ({
transactionId: "trans-123" ,
clientName: "My App" ,
provider: "Google" ,
scope: [ "openid" , "profile" ],
timestamp: Date . now (),
});
// Validate consent cookie
const data = manager . validateConsentCookie ( cookie );
if ( data ) {
console . log ( "Valid consent:" , data );
}
Constructor
Secret key for signing consent cookies
Methods
createConsentResponse()
Provider name for display
HTTP response with consent HTML
signConsentCookie()
validateConsentCookie()
Validated consent data or null if invalid/expired
Token Storage
Token storage backends for OAuth tokens and mappings.
MemoryTokenStorage
In-memory token storage with TTL support.
import { MemoryTokenStorage } from "fastmcp" ;
const storage = new MemoryTokenStorage ( 60000 ); // Cleanup every 60s
// Save value with TTL
await storage . save ( "key" , { data: "value" }, 3600 ); // 1 hour
// Get value
const value = await storage . get ( "key" );
// Delete value
await storage . delete ( "key" );
// Manual cleanup
await storage . cleanup ();
// Destroy storage
storage . destroy ();
Constructor
Cleanup interval in milliseconds
Methods
save
(key: string, value: unknown, ttl?: number) => Promise<void>
Save value with optional TTL (seconds)
get
(key: string) => Promise<unknown | null>
Retrieve value (returns null if expired/not found)
delete
(key: string) => Promise<void>
Delete value
Get number of stored items
Clear storage and stop cleanup interval
EncryptedTokenStorage
Encrypted wrapper for token storage using AES-256-GCM.
import { EncryptedTokenStorage , MemoryTokenStorage } from "fastmcp" ;
const backend = new MemoryTokenStorage ();
const storage = new EncryptedTokenStorage ( backend , "encryption-key" );
// Same interface as backend, but values are encrypted
await storage . save ( "key" , { secret: "data" }, 3600 );
const value = await storage . get ( "key" );
Constructor
Underlying storage backend
Encryption key (derived using scrypt)
Methods
Same as TokenStorage interface - all values are automatically encrypted/decrypted.
DiscoveryDocumentCache
Caches OAuth discovery documents with TTL and request coalescing.
import { DiscoveryDocumentCache } from "fastmcp" ;
const cache = new DiscoveryDocumentCache ({
ttl: 3600000 , // 1 hour
});
// Fetch and cache discovery document
const doc = await cache . get (
"https://accounts.google.com/.well-known/openid-configuration" ,
);
// Check if cached
if ( cache . has ( url )) {
console . log ( "Document is cached" );
}
// Clear cache
cache . clear (); // Clear all
cache . clear ( url ); // Clear specific URL
// Get cache size
console . log ( "Cached documents:" , cache . size );
Constructor
Time-to-live in milliseconds (1 hour)
Methods
get()
Fetch discovery document (uses cache if available).
Discovery document as JSON object
Features:
Returns cached value if valid
Coalesces concurrent requests for same URL
Auto-caches successful fetches
has()
Check if URL is cached and not expired.
Whether document is cached and valid
clear()
Clear cache.
Optional URL to clear (omit to clear all)
size
Get number of cached documents.
Number of cached documents