npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mcp-idp

v0.1.0

Published

Universal OAuth2 identity provider for Cloudflare Workers with Durable Objects. Provides authentication management for any OAuth2-protected resource without chat completion functionality.

Readme

MCP IDP

Universal OAuth2 identity provider for Cloudflare Workers with Durable Objects. Provides authentication management for any OAuth2-protected resource without chat completion functionality.

Features

  • Universal OAuth2: Automatic OAuth2 flow for any API returning 401 with WWW-Authenticate
  • Dynamic Client Registration: Automatically registers OAuth2 clients with authorization servers
  • Token Management: Handles access token storage, refresh, and expiration
  • PKCE Support: Secure authorization code flow with PKCE
  • Multi-Resource: Store and manage auth for multiple protected resources per user
  • Metadata Support: Attach custom metadata to each provider
  • Path-Based Matching: Find the most specific provider for any URL

Installation

npm install mcp-idp

Quick Start

import { createMCPIdpHandler, OAuthProviders, MCPIdpEnv } from "mcp-idp";

export { OAuthProviders };

export default {
  fetch: async (request: Request, env: MCPIdpEnv, ctx: ExecutionContext) => {
    const idpHandler = createMCPIdpHandler(
      {
        userId: "user-123",
        baseUrl: new URL(request.url).origin,
        clientInfo: { name: "My App", version: "1.0.0" },
      },
      env,
    );

    // Handle OAuth callbacks
    const oauthResponse = await idpHandler?.middleware(request, env, ctx);
    if (oauthResponse) return oauthResponse;

    // Your application logic here
    return new Response("Hello World");
  },
};

Wrangler Configuration

{
  "durable_objects": {
    "bindings": [{ "name": "OAuthProviders", "class_name": "OAuthProviders" }],
  },
  "migrations": [{ "tag": "v1", "new_sqlite_classes": ["OAuthProviders"] }],
}

Usage

Initialize Handler

const idpHandler = createMCPIdpHandler(
  {
    userId: "user-123",
    baseUrl: "https://your-app.com",
    clientInfo: { name: "My App", version: "1.0.0" },
    pathPrefix: "/oauth", // Optional, defaults to "/oauth"

    // Optional: Extract metadata after successful auth
    onAuthSuccess: async (resourceUrl: string, accessToken: string) => {
      // Fetch additional info about the resource
      return {
        name: "Custom Name",
        metadata: { foo: "bar" },
      };
    },
  },
  env,
);

Get Authorization Header

// Get auth for a specific URL (finds most specific matching provider)
const auth = await idpHandler.getAuthorizationForUrl(
  "https://api.example.com/users/me",
);

if (auth) {
  // Use the Authorization header
  const response = await fetch("https://api.example.com/users/me", {
    headers: auth,
  });
}

List Providers

const providers = await idpHandler.getProviders();

for (const provider of providers) {
  console.log({
    url: provider.resource_url,
    name: provider.name,
    hasAuth: !!provider.access_token,
    isPublic: provider.public === 1,
    metadata: provider.metadata,
    reauthorizeUrl: provider.reauthorizeUrl,
  });
}

Refresh Tokens

// Refresh tokens for specific URLs (auto-refreshes if expiring within 5 minutes)
await idpHandler.refreshProviders([
  "https://api.example.com",
  "https://api.another.com",
]);

Remove Provider

await idpHandler.removeProvider("https://api.example.com");

Direct Durable Object Access

// Get the Durable Object stub for advanced operations
const stub = idpHandler.getStub();

// Find provider for URL with path-based matching
const provider = await stub.findProviderForUrl(
  "https://api.example.com/v1/users/123",
);

OAuth Flow

  1. User initiates login by visiting /oauth/login?url=https://api.example.com
  2. Handler discovers OAuth2 metadata using .well-known endpoints
  3. Dynamic client registration creates OAuth2 client
  4. User is redirected to authorization endpoint with PKCE challenge
  5. After authorization, callback at /oauth/callback/{hostname} exchanges code for tokens
  6. Tokens are stored in Durable Object per user
  7. Future requests can retrieve auth headers via getAuthorizationForUrl()

Path-Based Provider Matching

The IDP uses path-based matching to find the most specific provider for a URL:

// Stored providers:
// - https://api.example.com
// - https://api.example.com/v1

// URL: https://api.example.com/v1/users/123
// Matches: https://api.example.com/v1 (most specific)

// URL: https://api.example.com/v2/posts
// Matches: https://api.example.com (fallback to base)

Public Resources

Resources that don't require authentication are automatically detected and marked as public:

// HEAD request returns 200
// -> Provider created with public: true, no auth flow needed

Metadata

Attach custom metadata to providers using the onAuthSuccess callback:

onAuthSuccess: async (resourceUrl, accessToken) => {
  // Fetch server info, available tools, etc.
  const info = await fetchResourceInfo(resourceUrl, accessToken);

  return {
    name: info.name,
    metadata: {
      type: "api",
      version: info.version,
      capabilities: info.capabilities,
    },
  };
};

API Reference

createMCPIdpHandler(config, env)

Creates an IDP handler instance.

Config:

  • userId: string - Unique identifier for the user
  • baseUrl: string - Base URL for OAuth callbacks (optional, defaults to request origin)
  • clientInfo: { name: string; version: string } - Client metadata for OAuth registration
  • pathPrefix?: string - Path prefix for OAuth endpoints (default: "/oauth")
  • onAuthSuccess?: (url, token) => Promise<{name, metadata}> - Called after successful auth

Returns: MCPIdpHandlers | null

MCPIdpHandlers

  • middleware(request, env, ctx) - Handle OAuth requests, returns Response or null
  • getAuthorizationForUrl(url) - Get Authorization header for URL
  • getProviders() - List all providers with metadata
  • refreshProviders(urls) - Refresh tokens for specific URLs
  • removeProvider(url) - Remove a provider
  • getStub() - Get Durable Object stub for advanced operations

Utility Functions

  • getAuthorizationForUrl(env, userId, url) - Get auth header without handler instance
  • parseWWWAuthenticate(header) - Parse WWW-Authenticate header
  • constructAuthorizationUrl(resourceUrl, callbackUrl, clientInfo, options) - Build OAuth URL
  • exchangeCodeForToken(code, authFlowData, redirectUri) - Exchange auth code for tokens
  • refreshAccessToken(refreshToken, clientId, clientSecret, tokenEndpoint) - Refresh an access token

License

MIT