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.
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:
OptionalaccountId: string
Account identifier. Defaults to 'device-code' (fixed identifier for device code flow).
Access token for API requests
Get user email from Microsoft Graph /me endpoint (pure query)
OptionalaccountId: string
Account identifier
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
OptionalaccountId: string
Account identifier (must be 'device-code' or undefined, otherwise throws error)
Auth provider with getAccessToken method
DeviceCodeProvider implements OAuth2TokenStorageProvider using Microsoft Device Code Flow
This provider:
Example