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

    Class DeviceCodeProvider

    DeviceCodeProvider implements OAuth2TokenStorageProvider using Microsoft Device Code Flow

    This provider:

    • Initiates device code flow with Microsoft endpoint
    • Displays user_code and verification_uri for manual authentication
    • Polls token endpoint until user completes auth
    • Stores access tokens + refresh tokens in Keyv storage
    • Refreshes tokens when expired
    • Provides single static identity (minimal account management like service accounts)
    const provider = new DeviceCodeProvider({
    service: 'outlook',
    clientId: 'your-client-id',
    tenantId: 'common',
    scope: 'https://graph.microsoft.com/Mail.Read',
    logger: console,
    tokenStore: new Keyv(),
    headless: true,
    });

    // Get authenticated Microsoft Graph client
    const token = await provider.getAccessToken('default');

    Implements

    • OAuth2TokenStorageProvider
    Index
    • Create middleware wrapper for single-user authentication

      Middleware wraps tool, resource, and prompt handlers and injects authContext into extra parameter. Handlers receive MicrosoftAuthProvider via extra.authContext.auth for API calls.

      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

      // Server registration
      const middleware = provider.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);

      // Tool handler receives auth
      async function handler({ id }: In, extra: EnrichedExtra) {
      // extra.authContext.auth is MicrosoftAuthProvider (from middleware)
      const graph = Client.initWithMiddleware({ authProvider: extra.authContext.auth });
      }
    • Get access token for Microsoft Graph API

      Flow:

      1. Check token storage
      2. If valid token exists, return it
      3. If expired but has refresh token, try refresh
      4. Otherwise, start new device code flow

      Parameters

      • OptionalaccountId: string

        Account identifier. Defaults to 'device-code' (fixed identifier for device code flow).

      Returns Promise<string>

      Access token for API requests

    • Get user email from Microsoft Graph /me endpoint (pure query)

      Parameters

      • OptionalaccountId: string

        Account identifier

      Returns Promise<string>

      User's email address (userPrincipalName or mail field)

    • Create auth provider for Microsoft Graph SDK integration

      Device code provider ALWAYS uses fixed accountId='device-code' This is by design - device code is a single static identity pattern

      Parameters

      • OptionalaccountId: string

        Account identifier (must be 'device-code' or undefined, otherwise throws error)

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

      Auth provider with getAccessToken method