Tool arguments type (inferred from tool module)
Enriched extra type with auth context and logger
Plain tool module to wrap with auth middleware
Wrapped tool module with enriched handler signature
Auth middleware wrappers typically:
// 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
}
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.