@mcp-z/oauth-google
    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:gmail:token")

    Implements

    • OAuth2TokenStorageProvider
    Index
    • Create authentication middleware for MCP tools, resources, and prompts

      Returns position-aware middleware wrappers that enrich handlers with authentication context. The middleware handles token retrieval, refresh, and AuthRequiredError automatically.

      Single-user middleware for desktop/CLI apps where ONE user runs the entire process:

      • Desktop applications (Claude Desktop)
      • CLI tools (Gmail CLI)
      • Personal automation scripts

      All requests use token lookups based on the active account or account override.

      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 Google's userinfo endpoint (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