:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(mcp): stamp $mcp_tool_category on mcp_tool_call events (#62595)
Daniel Visca82699c6fc2d8
diff --git a/services/mcp/src/hono/analytics.ts b/services/mcp/src/hono/analytics.tsindex ce06ddf95696..8a883e55f78b 100644--- a/services/mcp/src/hono/analytics.ts+++ b/services/mcp/src/hono/analytics.ts@@ -6,6 +6,7 @@ import {MCP_ANALYTICS_VERSION,type MCPAnalyticsContext,} from '@/lib/posthog/analytics'+import { getToolCategory } from '@/tools/toolDefinitions'import { buildMCPSessionAnalyticsProperties } from './mcp-context'import type { ResolvedState } from './request-state-resolver'@@ -102,6 +103,15 @@ export async function trackToolCall(const { properties, groups } = buildBaseProperties(state, analyticsContext)+ // `$mcp_tool_category` is the dashboard's grouping dimension (e.g. "Logs",+ // "Tracing"). The contract is: the producer stamps the category onto every+ // tool-call event; the MCP analytics dashboard reads it back verbatim and+ // never maps tool names to categories itself. PostHog's server derives it+ // from its own catalog here; external servers using the SDK declare it per+ // tool. Omitted when unknown (e.g. the `exec` wrapper) so the dashboard+ // buckets those as "Uncategorized".+ const toolCategory = getToolCategory(toolName)+getPostHogClient().capture({distinctId: state.distinctId,event: 'mcp_tool_call',@@ -112,6 +122,7 @@ export async function trackToolCall($mcp_duration_ms: durationMs,$mcp_is_error: isError,tool_name: toolName,+ ...(toolCategory ? { $mcp_tool_category: toolCategory } : {}),...(sessionUuid ? { $session_id: sessionUuid } : {}),...extraProperties,},diff --git a/services/mcp/src/hono/tool-executor.ts b/services/mcp/src/hono/tool-executor.tsindex f9eb0f21867f..79fcdfe233ee 100644--- a/services/mcp/src/hono/tool-executor.ts+++ b/services/mcp/src/hono/tool-executor.ts@@ -12,6 +12,7 @@ import {} from '@/lib/errors'import { AnalyticsEvent } from '@/lib/posthog/analytics'import { createExecTool, type ExecInnerCallTracker } from '@/tools/exec'+import { getToolCategory } from '@/tools/toolDefinitions'import type { Context, ZodObjectAny } from '@/tools/types'import { trackToolCall } from './analytics'@@ -209,11 +210,19 @@ export class ToolExecutor {toolCallsTotal.inc({ tool: toolName, status })toolCallDurationSeconds.observe({ tool: toolName, status }, properties.duration_ms / 1000)+ // Mirror the native path: stamp the inner tool's category so exec-routed+ // calls share the dashboard's `$mcp_tool_category` grouping dimension.+ const toolCategory = getToolCategory(toolName)+void (async () => {const freshContext = await state.reqCtx.getAnalyticsContextSafe(state.context)await state.reqCtx.trackEvent(AnalyticsEvent.MCP_TOOL_CALL,- { tool_name: toolName, ...properties },+ {+ tool_name: toolName,+ ...(toolCategory ? { $mcp_tool_category: toolCategory } : {}),+ ...properties,+ },freshContext,undefined,state.distinctIddiff --git a/services/mcp/src/tools/toolDefinitions.ts b/services/mcp/src/tools/toolDefinitions.tsindex cd4935553dbf..26fd1a35de69 100644--- a/services/mcp/src/tools/toolDefinitions.ts+++ b/services/mcp/src/tools/toolDefinitions.ts@@ -104,6 +104,16 @@ export function getToolDefinition(toolName: string): ToolDefinition {return definition}+/**+ * The product category a tool belongs to (e.g. "Logs", "Tracing"), or undefined+ * for tools without a catalogued definition (e.g. the `exec` wrapper). Unlike+ * {@link getToolDefinition} this never throws, so it is safe to call from the+ * analytics hot path where a missing definition must not break the request.+ */+export function getToolCategory(toolName: string): string | undefined {+ return getToolDefinitions()[toolName]?.category+}+export interface ToolFilterOptions {features?: string[] | undefinedtools?: string[] | undefineddiff --git a/services/mcp/tests/hono/analytics.test.ts b/services/mcp/tests/hono/analytics.test.tsindex d46fe0854cd7..3e5ddd1dddea 100644--- a/services/mcp/tests/hono/analytics.test.ts+++ b/services/mcp/tests/hono/analytics.test.ts@@ -96,4 +96,16 @@ describe('Hono MCP analytics contexts', () => {expect(properties.mcp_session_client_name).toBeUndefined()expect(properties.mcp_session_vendor_client).toBeUndefined()})++ it('stamps $mcp_tool_category from the catalogued tool definition', async () => {+ await trackToolCall('query-logs', 5, false, makeState())++ expect(mockCapture.mock.calls[0]![0].properties.$mcp_tool_category).toBe('Logs')+ })++ it('omits $mcp_tool_category for tools without a catalogued definition', async () => {+ await trackToolCall('exec', 5, false, makeState())++ expect(mockCapture.mock.calls[0]![0].properties).not.toHaveProperty('$mcp_tool_category')+ })})