SDK reference

MCP analytics

Wrap your MCP server once, and the MCP analytics page shows every tool call AI clients make, with its duration, size and errors.

server.ts
import { trackMcp } from "@databuddy/sdk/mcp";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";

const server = trackMcp(new McpServer({ name: "my-server", version: "1.0.0" }));
.env
DATABUDDY_API_KEY=dbdy_your_key

Make the key with the Event Tracking scope in Organization settings → API keys. Arguments and results never leave your server; only the length of the returned text is recorded. Needs @databuddy/sdk 3.2.0 or later.

  1. Client calls a tool
  2. Your handler runs
  3. trackMcp records it
  4. MCP page

Wrap your server

Calls are sent in batches every second. If your server ends itself with process.exit, send the last batch first:

server.ts
import { flushMcp } from "@databuddy/sdk/mcp";

process.on("SIGTERM", async () => {
  await flushMcp();
  process.exit(0);
});

trackMcp returns the server you pass it, and also accepts the low-level Server. Without an API key it leaves the server untouched. Each recorded call counts as one event on your plan.

Link calls to users and accounts

trackMcp(server, {
  profileId: ({ authInfo }) => authInfo?.extra?.userId,
  groups: ({ authInfo }) => ({ workspace: authInfo?.extra?.workspaceId }),
});

Pass the user ID you give identify() and the accounts you give group(), as values or as functions of each call's request. Functions return the value itself, not a promise. Calls must belong to a website first, through websiteId or the client ID env variable.

authInfo.extra holds whatever your token verifier put there, such as Clerk's userId. A user ID over 128 characters is left out, and the call is still recorded. Needs @databuddy/sdk 3.5.4 or later.

Your route may refuse a call before the server sees it, such as for a missing scope. To count it, call recordMcpCall() with the same options:

import { recordMcpCall } from "@databuddy/sdk/mcp";

recordMcpCall(
  { tool: "get_data", durationMs: 3, errorCode: "rate_limited", serverName: "my-server" },
  { ...trackingOptions, request: { authInfo } }
);

Configure tracking

OptionDefaultWhat it does
apiKeyDATABUDDY_API_KEYAPI key with the Event Tracking scope
websiteIdFrom the client ID env variable, then DATABUDDY_WEBSITE_IDThe website calls belong to
environmentVERCEL_ENV, then NODE_ENVKeeps local and preview calls apart from production
profileIdThe user, or a function of each call's request
groupsThe user's accounts, or a function of each call's request
beforeSendEdits a call before it's sent; return null to drop it
waitUntilYour platform's waitUntil, for serverless functions
debugfalseLogs skipped users, failed sends and other problems to stderr
apiUrlhttps://basket.databuddy.ccWhere calls are sent

Calls belong to the organization that owns the key and are split by server name, website and environment. A key limited to some websites can only send calls for one of them, and an unknown website rejects the whole batch.

Recorded per call
Tool and serverTool name, server name and version
ClientName and version from clientInfo, or the user agent; scripts show as Node.js script, curl and so on
OutcomeWhether it failed, error code, and up to 512 characters of error message
Size and timeCharacters of text returned (about 4 per token) and handler duration
Session and userMCP session ID, and the user and accounts you pass

Tracking never changes what a client receives. It doesn't throw or delay responses, and sends in the background with a 5 second timeout.

Group failures by error code

Return an isError result whose text is JSON with a code, and the MCP page groups failures by it.

return {
  isError: true,
  content: [{ type: "text", text: JSON.stringify({ error: { code: "not_found", message: "Website not found" } }) }],
};

Use short, stable snake_case codes with no IDs in them; codes are cut to 64 characters. Protocol errors arrive as invalid_params, method_not_found and invalid_output, and cancelled calls as cancelled.

Keep error messages private

Error messages can include input your handler put in them. To keep them on your server, leave the message out:

trackMcp(server, {
  beforeSend: ({ error, ...call }) => call,
});

The call still counts as failed, because it keeps its error code.

How is this guide?