Skip to main content

init

Initializes mcp-tap and patches all detected SDKs. Must be called before any AI SDK calls.
interface InitOptions {
  apiKey: string;             // Project API key (required)
  userId?: string | null;     // Optional user identifier attached to events
  endpoint?: string;          // Event ingestion URL (default: mcp-tap cloud)
  signingKeyPath?: string;    // Path to Ed25519 PEM for AARM receipts
}
import { init } from "mcp-tap";

init({
  apiKey: "tap_abc123",
  userId: "user@example.com",
});

instrumentServer

Instruments an MCP Server object to capture all incoming tools/call requests.
interface InstrumentServerOptions {
  apiKey: string;        // Project API key (required)
  endpoint?: string;     // Event ingestion URL (default: mcp-tap cloud)
}
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { instrumentServer } from "mcp-tap";

const server = new Server({ name: "my-server", version: "1.0.0" });
instrumentServer(server, { apiKey: "tap_abc123" });

session

Scoped session context. All events emitted inside fn are tagged with the given session name.
import { session } from "mcp-tap";

const result = await session("user-query-123", async () => {
  return await anthropic.messages.create({ ... });
});
If called with only a function (no name), a random session ID is generated:
const result = await session(async () => {
  // auto-generated session ID
});

Emitter

Buffers events and sends them in batches. Created automatically by init() — you rarely need to use this directly.
import { Emitter } from "mcp-tap";

const emitter = new Emitter(apiKey, endpoint);
emitter.emit(event);    // Add to buffer
emitter.flush();         // Force-send current buffer
emitter.shutdown();      // Flush + stop timer

makeEvent

Creates a TapEvent object with sensible defaults.
import { makeEvent, generateEventId } from "mcp-tap";

const event = makeEvent({
  projectId: "tap_abc123",
  sessionId: "ses_abc123",
  userId: null,
  toolName: "get_weather",
  inputs: { city: "SF" },
  output: { temp: 72 },
  latencyMs: 150,
  source: "custom",
});

generateEventId

Returns a unique event ID in the format evt_<16 hex chars>.

generateSessionId

Returns a unique session ID in the format ses_<16 hex chars>.

Receipt utilities

import { ContextChain, ReceiptSigner, hashOutput, enhanceEvent } from "mcp-tap";

// SHA-256 hash chain for ordered integrity
const chain = new ContextChain();
const contextHash = chain.chain(eventObj);

// Ed25519 signing
const signer = new ReceiptSigner(privateKeyPem);
const sig = signer.sign(eventObj);
// → { algorithm: "Ed25519", key_id: "...", value: "base64..." }

// Hash an event's output field
const outputHash = hashOutput(someOutput);

// Add context_hash, output_hash, and signature to an event
enhanceEvent(event, chain, signer);

Individual patch functions

Each patch can be applied independently if you don’t want init() to patch everything:
import { patchAnthropic, patchOpenAI, patchMCP, patchComposio, patchHTTP } from "mcp-tap";

patchAnthropic();  // Patches @anthropic-ai/sdk Messages.create
patchOpenAI();     // Patches openai Chat.Completions.create
patchMCP();        // Patches @modelcontextprotocol/sdk Client.callTool
patchComposio();   // Patches composio-core ComposioToolSet.executeAction
patchHTTP();       // Patches globalThis.fetch for known AI hosts