: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
fix(mcp): attribute tracked events to the session client (#96535)
Georgis Andonis651ff4302057
diff --git a/services/mcp/src/hono/request-context.ts b/services/mcp/src/hono/request-context.tsindex 835373380057..eb953f386791 100644--- a/services/mcp/src/hono/request-context.ts+++ b/services/mcp/src/hono/request-context.ts@@ -20,6 +20,7 @@ import {buildMCPRequestContext,buildMCPSessionAnalyticsProperties,getEffectiveMCPClientContext,+ getEffectiveMCPClientIdentity,type MCPRequestContext,type MCPSessionContext,} from './mcp-context'@@ -221,23 +222,27 @@ export class RequestContext {requestContext: MCPRequestContext = this.requestContext,sessionContext: MCPSessionContext | null = this.sessionContext): Record<string, unknown> {+ // `clientInfo` arrives on `initialize` only, so a mid-session call carries none of+ // it. Resolve live-first with the session-pinned value per field, the way+ // `buildBaseProperties` does for tool calls, or these events record no client.+ const clientIdentity = getEffectiveMCPClientIdentity(requestContext, sessionContext)return {$ai_product: 'mcp',$mcp_source: MCP_ANALYTICS_SOURCE,$mcp_server_name: MCP_SERVER_NAME,$mcp_server_version: MCP_SERVER_VERSION,- $mcp_client_name: requestContext.mcpClientName,- $mcp_client_version: requestContext.mcpClientVersion,+ $mcp_client_name: clientIdentity.mcpClientName,+ $mcp_client_version: clientIdentity.mcpClientVersion,$mcp_client_user_agent: requestContext.clientUserAgent,- $mcp_protocol_version: requestContext.mcpProtocolVersion,+ $mcp_protocol_version: clientIdentity.mcpProtocolVersion,$mcp_transport: requestContext.transport,$mcp_session_id: requestContext.mcpSessionId,$mcp_conversation_id: requestContext.mcpConversationId,- $mcp_consumer: requestContext.mcpConsumer,+ $mcp_consumer: clientIdentity.mcpConsumer,$mcp_mode: requestContext.mode,$mcp_region: requestContext.region,mcp_runtime: 'hono',- $mcp_vendor_client: requestContext.mcpVendorClient,+ $mcp_vendor_client: clientIdentity.mcpVendorClient,...buildMCPSessionAnalyticsProperties(sessionContext),}}diff --git a/services/mcp/tests/hono/request-context.test.ts b/services/mcp/tests/hono/request-context.test.tsindex 80fdebcc66f4..cac9c3b6201b 100644--- a/services/mcp/tests/hono/request-context.test.ts+++ b/services/mcp/tests/hono/request-context.test.ts@@ -172,6 +172,43 @@ describe('RequestContext', () => {})})+ // `agent-feedback` and the context-switch events capture through `trackEvent`, which+ // reads these properties. `clientInfo` arrives on `initialize` only, so reading the+ // request alone records no client for every call after the first.+ describe('buildClientProperties', () => {+ it('falls back to the session client identity mid-session', () => {+ const ctx = new RequestContext(fakeRedis(), env, makeProps({ mcpClientName: undefined }))+ ctx.setMcpContexts(+ { authMethod: 'personal_api_key', mcpClientName: undefined },+ {+ mcpClientName: 'claude-code',+ mcpClientVersion: '1.0',+ mcpProtocolVersion: '2025-03-26',+ mcpConsumer: 'plugin',+ mcpVendorClient: 'ClaudeCode',+ }+ )++ expect(ctx.buildClientProperties()).toMatchObject({+ $mcp_client_name: 'claude-code',+ $mcp_client_version: '1.0',+ $mcp_protocol_version: '2025-03-26',+ $mcp_consumer: 'plugin',+ $mcp_vendor_client: 'ClaudeCode',+ })+ })++ it('keeps the live request identity when the call carries one', () => {+ const ctx = new RequestContext(fakeRedis(), env, makeProps({ mcpClientName: 'cursor' }))+ ctx.setMcpContexts(+ { authMethod: 'personal_api_key', mcpClientName: 'cursor' },+ { mcpClientName: 'claude-code' }+ )++ expect(ctx.buildClientProperties()).toMatchObject({ $mcp_client_name: 'cursor' })+ })+ })+describe('getDistinctId', () => {it('deduplicates concurrent calls into a single API request', async () => {mockMe.mockResolvedValue({ success: true, data: { distinct_id: 'user-123' } })