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

    Type Alias AuthMiddlewareWrapper<TArgs, TExtra>

    AuthMiddlewareWrapper: (
        toolModule: ToolModule,
    ) => ToolModule<ToolConfig, ToolHandler<TArgs, TExtra>>

    Middleware wrapper that enriches tool modules with authentication context.

    Wraps plain tool modules to inject authentication, logging, and request metadata. The wrapper pattern allows separation of business logic from cross-cutting concerns.

    Type Parameters

    • TArgs = unknown

      Tool arguments type (inferred from tool module)

    • TExtra = RequestHandlerExtra<ServerRequest, ServerNotification>

      Enriched extra type with auth context and logger

    Type Declaration

    Auth middleware wrappers typically:

    • Extract auth context from MCP request or OAuth provider
    • Inject logger instance for structured logging
    • Handle authentication errors with proper MCP error responses
    • Preserve tool configuration and metadata
    // Actual usage pattern from OAuth providers (LoopbackOAuthProvider, ServiceAccountProvider, DcrOAuthProvider)
    const provider = new LoopbackOAuthProvider({ service: 'gmail', ... });
    const authMiddleware = provider.authMiddleware();

    // Apply middleware to tools (handlers receive enriched extra with authContext)
    const tools = toolFactories.map(f => f()).map(authMiddleware.withToolAuth);
    const resources = resourceFactories.map(f => f()).map(authMiddleware.withResourceAuth);
    const prompts = promptFactories.map(f => f()).map(authMiddleware.withPromptAuth);

    // Tool handler receives enriched extra with guaranteed authContext
    async function handler({ id }: In, extra: EnrichedExtra) {
    // extra.authContext.auth is OAuth2Client (from middleware)
    const gmail = google.gmail({ version: 'v1', auth: extra.authContext.auth });
    // ... business logic with authenticated context
    }