// {{ display_name }} MCP server — generated by mcpify. Do not hand-edit.

import { AsyncLocalStorage } from 'node:async_hooks';
import { randomUUID } from 'node:crypto';

interface CorrelationContext {
  traceId: string;
}

const storage = new AsyncLocalStorage<CorrelationContext>();

/** Runs `fn` with a fresh (or given) correlation ID bound for its entire async call tree. */
export function withCorrelationId<T>(fn: () => T, traceId: string = randomUUID()): T {
  return storage.run({ traceId }, fn);
}

export function getCorrelationId(): string | undefined {
  return storage.getStore()?.traceId;
}
