Skip to content

Conversation

@fauziralpiandi
Copy link

@fauziralpiandi fauziralpiandi commented Oct 22, 2025

implemented a standalone DiscoveryDocumentCache utility class to help users reduce unnecessary network requests when fetching OAuth/OpenID Connect discovery documents during authentication:

  • TTL-based caching: configurable time-to-live (default: 1 hour)
  • promise coalescing: prevents duplicate fetches for concurrent requests to the same URL
  • proper timing: TTL calculated after fetch completes, not before
  • cache invalidation: support for clearing specific URLs or entire cache
  • simple API: just call cache.get(url) and it handles the rest

example:

import { FastMCP, DiscoveryDocumentCache } from "fastmcp";

// Create cache instance (reuse across requests)
const discoveryCache = new DiscoveryDocumentCache({
  ttl: 3600000, // 1 hour (default)
});

const mcp = new FastMCP("My Server");

// Use in authenticate handler
mcp.authenticate(async ({ sessionId }) => {
  const config = await discoveryCache.get('https://auth.example.com/.well-known/openid-configuration');
  const jwksUri = config.jwks_uri;
  // ... //
});

addresses the TODO in docs about caching discovery documents.

@fauziralpiandi
Copy link
Author

fauziralpiandi commented Oct 22, 2025

my bad... there are indeed some timing and concurrency issues for this implementation:

  1. TTL is calculated before fetch, causing timing issues
  2. concurrnt requests trigger duplicate fetches
  3. missing tests for fetch latenncy and concurrency

Copy link
Author

@fauziralpiandi fauziralpiandi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ready for review @punkpeye

@fauziralpiandi fauziralpiandi changed the title feat: add DiscoveryDocumentCache feat: add DiscoveryDocumentCache for OAuth Discovery Documents Oct 22, 2025
@punkpeye punkpeye merged commit 9079a85 into punkpeye:main Oct 22, 2025
1 check passed
@punkpeye
Copy link
Owner

Amazing. Thanks!

@fauziralpiandi fauziralpiandi deleted the feat/discovery-document-cache branch October 22, 2025 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants