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.
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" }));DATABUDDY_API_KEY=dbdy_your_keyMake 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.
Wrap your server
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
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.
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.
Related
How is this guide?