@mcp-z/oauth-microsoft
    Preparing search index...

    Class LoopbackOAuthProvider

    Loopback OAuth Client (RFC 8252 Section 7.3)

    Implements OAuth 2.0 Authorization Code Flow with PKCE for native applications using loopback interface redirection. Manages ephemeral OAuth flows and token persistence with Keyv for key-based token storage using compound keys.

    Token key format: {accountId}:{service}:token (e.g., "user@example.com:outlook:token")

    Implements

    • OAuth2TokenStorageProvider
    Index
    • Create auth middleware for single-user context (single active account per service)

      Single-user mode:

      • Maintains per-service active accounts in storage
      • Supports backchannel account override via extra.mcpReq._meta.accountId
      • Automatically enhances output schemas with auth_required branch

      Example:

      const loopback = new LoopbackOAuthProvider({ service: 'outlook', ... });
      const middleware = loopback.authMiddleware();
      const tools = toolFactories.map(f => f()).map(middleware.withToolAuth);
      const resources = resourceFactories.map(f => f()).map(middleware.withResourceAuth);
      const prompts = promptFactories.map(f => f()).map(middleware.withPromptAuth);

      Returns {
          withPromptAuth: <
              T extends { config: unknown; handler: unknown; name: string },
          >(
              module: T,
          ) => T;
          withResourceAuth: <
              T extends
                  {
                      config?: unknown;
                      handler: unknown;
                      name: string;
                      template?: unknown;
                  },
          >(
              module: T,
          ) => T;
          withToolAuth: <
              T extends { config: unknown; handler: unknown; name: string },
          >(
              module: T,
          ) => T;
      }

      Object with withToolAuth, withResourceAuth, withPromptAuth methods

    • Get access token from Keyv using compound key

      Parameters

      • OptionalaccountId: string

        Account identifier (email address). Required for loopback OAuth.

      Returns Promise<string>

      Access token for API requests

    • Get user email from Microsoft Graph API (pure query) Used to query email for existing authenticated account

      Parameters

      • OptionalaccountId: string

        Account identifier to get email for

      Returns Promise<string>

      User's email address

    • Handle OAuth callback from persistent endpoint. Used by HTTP servers with configured redirectUri.

      Parameters

      • params: { code: string; state?: string }

        OAuth callback parameters

      Returns Promise<{ email: string; token: CachedToken }>

      Email and cached token

    • Convert to Microsoft Graph-compatible auth provider

      Parameters

      • OptionalaccountId: string

        Account identifier for multi-account support (e.g., 'user@example.com')

      Returns { getAccessToken: () => Promise<string> }

      Auth provider configured for the specified account