UNPKG

mcp-use

Version:

Opinionated MCP Framework for TypeScript (@modelcontextprotocol/sdk compatible) - Build MCP Agents, Clients and Servers with support for ChatGPT Apps, Code Mode, OAuth, Notifications, Sampling, Observability and more.

61 lines 2.11 kB
/** * AsyncLocalStorage-based context management for HTTP requests * * This module provides a way to pass HTTP request context (Hono Context) * through async call chains without explicit parameter passing. * * This is particularly useful for: * - Passing authentication info from middleware to tool callbacks * - Accessing request headers, user data, etc. in deeply nested functions * - Maintaining request isolation in concurrent request handling */ import type { Context } from "hono"; /** * Execute a function with a request context stored in AsyncLocalStorage * * @param context - Hono Context object to store * @param fn - Function to execute within this context * @param sessionId - Optional session ID to store with the context * @returns Promise resolving to the function's return value * * @example * ```typescript * app.post('/mcp', async (c) => { * return runWithContext(c, async () => { * // Any async operations here can access context via getRequestContext() * await handleMcpRequest(); * return c.json({ success: true }); * }); * }); * ``` */ export declare function runWithContext<T>(context: Context, fn: () => Promise<T>, sessionId?: string): Promise<T>; /** * Get the current request context from AsyncLocalStorage * * @returns The Hono Context for the current async operation, or undefined if not in a request context * * @example * ```typescript * // Inside a tool callback * const context = getRequestContext(); * if (context) { * const user = context.get('user'); * console.log('Current user:', user); * } * ``` */ export declare function getRequestContext(): Context | undefined; /** * Get the current session ID from AsyncLocalStorage * * @returns The session ID for the current async operation, or undefined if not in a request context */ export declare function getSessionId(): string | undefined; /** * Check if currently executing within a request context * * @returns true if a request context is available */ export declare function hasRequestContext(): boolean; //# sourceMappingURL=context-storage.d.ts.map