Skip to main content
Glama

Carbon Voice

by PhononX
request-context.ts1.76 kB
import { AsyncLocalStorage } from 'async_hooks'; import crypto from 'crypto'; export interface RequestContext { traceId: string; sessionId?: string; userId?: string; startTime: number; } // Create AsyncLocalStorage to store request context const requestContextStorage = new AsyncLocalStorage<RequestContext>(); /** * Generate a trace ID for distributed tracing */ export const generateTraceId = (): string => { return crypto.randomUUID(); }; /** * Get the current request context */ export const getRequestContext = (): RequestContext | undefined => { return requestContextStorage.getStore(); }; /** * Get the current trace ID (serves as both traceId and requestId) */ export const getTraceId = (): string | undefined => { return getRequestContext()?.traceId; }; /** * Get the current session ID */ export const getSessionId = (): string | undefined => { return getRequestContext()?.sessionId; }; /** * Get the current user ID */ export const getUserId = (): string | undefined => { return getRequestContext()?.userId; }; /** * Run a function with request context */ export const runWithContext = <T>( context: RequestContext, fn: () => T | Promise<T>, ): T | Promise<T> => { return requestContextStorage.run(context, fn); }; /** * Update the request context with new values */ export const updateRequestContext = ( updates: Partial<RequestContext>, ): void => { const context = getRequestContext(); if (context) { Object.assign(context, updates); } }; /** * Create a new request context */ export const createRequestContext = ( sessionId?: string, userId?: string, ): RequestContext => { return { traceId: generateTraceId(), sessionId, userId, startTime: Date.now(), }; };

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/PhononX/cv-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server