Install
@modelcontextprotocol/sdk as a peer dependency. If your tools use Zod 4 raw-shape schemas, use MCP SDK 1.29 or later — 1.20 silently drops fields added to Zod 4 raw shapes.
Set your API key
The SDK reads your key from the environment:Wrap your server
If you build your server in a factory function, wrap the factory withcreateMcpAnalyticsServer. Every tool registered inside is instrumented automatically:
server.ts
Other server shapes
Existing server + tool registry
Existing server + tool registry
If you already have an
McpServer instance and a list of tool definitions, instrument them in place:Custom dispatcher
Custom dispatcher
If you handle
tools/list and tools/call yourself, use the recorder directly:Mastra
Mastra
Wrap a Mastra tool map with the dedicated adapter:The adapter reads MCP request context from
context.mcp.extra or context.requestContext.get("mcp.extra") automatically.Running on serverless or stateless HTTP? See Stateless & serverless servers for session handling, and set
delivery: "await" so events are sent before the function freezes.Configuration
Pass options under thearmature key:
Flush on shutdown
With background delivery, drain pending events before the process exits:Troubleshooting
`Mixed Zod versions detected in object shape`
`Mixed Zod versions detected in object shape`
Fixed in current releases — update the SDK. Older versions crashed when a tool declared a Zod 4 raw-shape schema while the SDK injected its telemetry field with Zod 3.
Sessions show up but telemetry fields are empty
Sessions show up but telemetry fields are empty
Make sure
@modelcontextprotocol/sdk is 1.29+. Version 1.20 silently drops fields the SDK adds to Zod 4 raw-shape schemas, so agents never see them.No data arrives
No data arrives
Check that
ANALYTICS_INGEST_API_KEY is set in the server’s environment (missing keys no-op silently by design) and add an onError handler to surface delivery failures.
