All files / src mcp-server-context.ts

100% Statements 8/8
100% Branches 0/0
100% Functions 3/3
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25          36x     36x     36x 167x       36x 17x       36x 64x    
/**
 * The MCP server object of the session a tool call belongs to, so a tool can ask its own
 * client something (an elicitation) or tell it its tool list changed.
 */
 
import { AsyncLocalStorage } from 'node:async_hooks';
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
 
const serverStorage = new AsyncLocalStorage<{ server: Server; signal?: AbortSignal }>();
 
/** Run `fn` as part of a request `server` received. */
export function runWithMcpServer<T>(server: Server, fn: () => T, signal?: AbortSignal): T {
  return serverStorage.run({ server, signal }, fn);
}
 
/** The server whose request is running; undefined outside one. */
export function currentMcpServer(): Server | undefined {
  return serverStorage.getStore()?.server;
}
 
/** Cancellation and deadline of this request, never another session's call. */
export function currentMcpAbortSignal(): AbortSignal | undefined {
  return serverStorage.getStore()?.signal;
}