import { AsyncLocalStorage } from "node:async_hooks"; type McpHttpAuthContext = { authorization?: string; cookie?: string; xApiKey?: string; anthropicVersion?: string; }; const mcpHttpAuthContext = new AsyncLocalStorage(); function headerValue(request: Request, name: string): string | undefined { const value = request.headers.get(name); return value && value.trim().length > 0 ? value : undefined; } export function getMcpHttpAuthHeadersForInternalFetch(): Record { const context = mcpHttpAuthContext.getStore(); const headers: Record = {}; if (context?.authorization) headers.Authorization = context.authorization; if (context?.cookie) headers.Cookie = context.cookie; if (context?.xApiKey && context?.anthropicVersion) { headers["x-api-key"] = context.xApiKey; headers["anthropic-version"] = context.anthropicVersion; } return headers; } export async function withMcpHttpAuthContext( request: Request, callback: () => Promise ): Promise { return mcpHttpAuthContext.run( { authorization: headerValue(request, "authorization"), cookie: headerValue(request, "cookie"), xApiKey: headerValue(request, "x-api-key"), anthropicVersion: headerValue(request, "anthropic-version"), }, callback ); }