Skip to content

: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.

chore(mcp): sync event properties between trackEvent and mcpcat (#57600)

Georgiy Tarasov60bea9072f0d
diff --git a/services/mcp/src/lib/analytics.ts b/services/mcp/src/lib/analytics.ts
index 14247538da5c..738b5a8f5801 100644
--- a/services/mcp/src/lib/analytics.ts
+++ b/services/mcp/src/lib/analytics.ts
@@ -7,7 +7,7 @@ export enum AnalyticsEvent {
MCP_INIT = 'mcp init',
MCP_PROJECT_SWITCHED = 'mcp project switched',
MCP_ORGANIZATION_SWITCHED = 'mcp organization switched',
- MCP_TOOL_CALLED = 'mcp_tool_called', // matching mcpcat
+ MCP_TOOL_CALL = 'mcp_tool_call', // matching mcpcat
MCP_FEEDBACK_SUBMITTED = 'mcp feedback submitted',
}
diff --git a/services/mcp/src/lib/mcpcat.ts b/services/mcp/src/lib/mcpcat.ts
index 99adace9e272..9cbc1a417aaa 100644
--- a/services/mcp/src/lib/mcpcat.ts
+++ b/services/mcp/src/lib/mcpcat.ts
@@ -14,10 +14,12 @@ export type McpCatIdentityProvider = {
getRegion: () => Promise<string | undefined>
getAnalyticsContext: () => Promise<MCPAnalyticsContext | undefined>
getClientUserAgent: () => Promise<string | undefined>
- getVersion: () => Promise<number | undefined>
+ getMcpVersion: () => Promise<number | undefined>
getOAuthClientName: () => Promise<string | undefined>
getReadOnly: () => Promise<boolean | undefined>
getTransport: () => Promise<string | undefined>
+ getMcpConsumer: () => Promise<string | undefined>
+ getMcpMode: () => Promise<string | undefined>
}
export function redactSensitiveInformation(text: string): string {
@@ -75,8 +77,10 @@ export async function initMcpCatObservability(server: McpServer, identity: McpCa
oauthClientName,
readOnly,
transport,
+ mcpConsumer,
+ mcpMode,
] = await Promise.all([
- identity.getVersion(),
+ identity.getMcpVersion(),
identity.getClientUserAgent(),
identity.getMcpClientName(),
identity.getMcpClientVersion(),
@@ -86,6 +90,8 @@ export async function initMcpCatObservability(server: McpServer, identity: McpCa
identity.getOAuthClientName(),
identity.getReadOnly(),
identity.getTransport(),
+ identity.getMcpConsumer(),
+ identity.getMcpMode(),
])
// `$groups` is the raw event-payload key; mcpcat doesn't expose a typed
@@ -110,6 +116,8 @@ export async function initMcpCatObservability(server: McpServer, identity: McpCa
mcp_oauth_client_name: oauthClientName,
read_only: readOnly,
mcp_transport: transport,
+ mcp_consumer: mcpConsumer,
+ mcp_mode: mcpMode,
...(Object.keys(groups).length > 0 ? { $groups: groups } : {}),
}
},
diff --git a/services/mcp/src/mcp.ts b/services/mcp/src/mcp.ts
index 0b75413b0eaa..d673d1af41c1 100644
--- a/services/mcp/src/mcp.ts
+++ b/services/mcp/src/mcp.ts
@@ -89,6 +89,8 @@ export class MCP extends McpAgent<Env> {
private mcpClientName: string | undefined
private mcpClientVersion: string | undefined
private mcpProtocolVersion: string | undefined
+ private mcpMode: McpMode | undefined
+ private mcpVersion: number | undefined
get requestProperties(): RequestProperties {
return this.props as RequestProperties
@@ -325,6 +327,8 @@ export class MCP extends McpAgent<Env> {
...(this.mcpProtocolVersion ? { mcp_protocol_version: this.mcpProtocolVersion } : {}),
...(this.requestProperties.mcpConsumer ? { mcp_consumer: this.requestProperties.mcpConsumer } : {}),
...(this.requestProperties.transport ? { mcp_transport: this.requestProperties.transport } : {}),
+ ...(this.mcpMode ? { mcp_mode: this.mcpMode } : {}),
+ ...(this.mcpVersion !== undefined ? { mcp_version: this.mcpVersion } : {}),
...contextProperties,
...previousContextProperties,
...properties,
@@ -541,24 +545,13 @@ export class MCP extends McpAgent<Env> {
oauthClientName,
})
- // Restrict single-exec mode to coding agents only — Cursor and other clients that
- // render `structuredContent` in their UI need the full per-tool roster, not the
- // wrapped CLI. `resolveClientInfo` is awaited at the top of `init()` so this
- // decision sees the real value on first-connect. PostHog's agent wrapper
- // self-identifies via the `x-posthog-mcp-consumer` header and forces
- // single-exec regardless of the wrapped client's reported name. Vibe-coding
- // platforms (Lovable, Replit) are detected by OAuth client name since they
- // typically connect through a generic MCP client wrapper.
- // An explicit `mode` from the caller (header `x-posthog-mcp-mode` or query
- // param `mode`) wins over the flag + client-profile heuristic.
- const useSingleExec =
- mode === 'cli' ||
- (mode !== 'tools' &&
- singleExecFlagOn &&
- (clientProfile.isCodingAgent() ||
- clientProfile.isPostHogCodeConsumer() ||
- clientProfile.isVibeCodingClient()))
- const version = useSingleExec ? 2 : (flagVersion ?? clientVersion ?? 1)
+ const { useSingleExec, version } = this.resolveModeAndVersion({
+ mode,
+ singleExecFlagOn,
+ clientProfile,
+ flagVersion,
+ clientVersion,
+ })
// Fetch group types and metadata in parallel (cache is now seeded)
const resolvedProjectId = projectId || (await this.cache.get('projectId'))
@@ -681,7 +674,7 @@ export class MCP extends McpAgent<Env> {
(async () => {
const freshContext = await this.getAnalyticsContextSafe(await this.getContext())
await this.trackEvent(
- AnalyticsEvent.MCP_TOOL_CALLED,
+ AnalyticsEvent.MCP_TOOL_CALL,
{ tool_name: toolName, ...properties },
freshContext ? { context: freshContext } : undefined
)
@@ -722,10 +715,12 @@ export class MCP extends McpAgent<Env> {
getClientUserAgent: async () => this.requestProperties.clientUserAgent,
// Server-resolved version (may differ from the client-reported one because of
// the `mcp-version-2` feature flag), so mcpcat events line up with ours.
- getVersion: async () => version,
+ getMcpVersion: async () => version,
getOAuthClientName: async () => (await this.cache.get('clientName')) || undefined,
getReadOnly: async () => readOnly,
getTransport: async () => this.requestProperties.transport,
+ getMcpConsumer: async () => this.requestProperties.mcpConsumer,
+ getMcpMode: async () => this.mcpMode,
})
const initDurationMs = this.requestProperties.requestStartTime
@@ -741,8 +736,6 @@ export class MCP extends McpAgent<Env> {
AnalyticsEvent.MCP_INIT,
{
tool_count: allTools.length,
- mcp_version: version,
- mcp_mode: useSingleExec ? 'cli' : 'tools',
has_organization_id: !!organizationId,
has_project_id: !!projectId,
read_only: !!readOnly,
@@ -755,6 +748,45 @@ export class MCP extends McpAgent<Env> {
)
}
+ /**
+ * Decide single-exec mode and the protocol version for this connection,
+ * stashing both on the instance so `trackEvent` and the mcpcat identity
+ * provider can emit `mcp_mode` / `mcp_version` on every downstream event
+ * without re-deriving them.
+ *
+ * Single-exec is restricted to coding agents — Cursor and other clients
+ * that render `structuredContent` in their UI need the full per-tool roster,
+ * not the wrapped CLI. PostHog's agent wrapper self-identifies via the
+ * `x-posthog-mcp-consumer` header and forces single-exec regardless of the
+ * wrapped client's reported name. Vibe-coding platforms (Lovable, Replit)
+ * are detected by OAuth client name since they typically connect through a
+ * generic MCP client wrapper. An explicit `mode` from the caller (header
+ * `x-posthog-mcp-mode` or query param `mode`) wins over the flag +
+ * client-profile heuristic.
+ */
+ private resolveModeAndVersion(args: {
+ mode: McpMode | undefined
+ singleExecFlagOn: boolean
+ clientProfile: MCPClientProfile
+ flagVersion: number | undefined
+ clientVersion: number | undefined
+ }): { useSingleExec: boolean; version: number } {
+ const { mode, singleExecFlagOn, clientProfile, flagVersion, clientVersion } = args
+ const useSingleExec =
+ mode === 'cli' ||
+ (mode !== 'tools' &&
+ singleExecFlagOn &&
+ (clientProfile.isCodingAgent() ||
+ clientProfile.isPostHogCodeConsumer() ||
+ clientProfile.isVibeCodingClient()))
+ const version = useSingleExec ? 2 : (flagVersion ?? clientVersion ?? 1)
+
+ this.mcpMode = useSingleExec ? 'cli' : 'tools'
+ this.mcpVersion = version
+
+ return { useSingleExec, version }
+ }
+
private async resolveVersionFlag(): Promise<number | undefined> {
try {
const distinctId = await this.getDistinctId()
diff --git a/services/mcp/tests/unit/mcpcat.test.ts b/services/mcp/tests/unit/mcpcat.test.ts
index 00e7229ca15c..74e7eafe8724 100644
--- a/services/mcp/tests/unit/mcpcat.test.ts
+++ b/services/mcp/tests/unit/mcpcat.test.ts
@@ -54,10 +54,12 @@ describe('initMcpCatObservability', () => {
projectName: 'Project 101',
}),
getClientUserAgent: vi.fn().mockResolvedValue('test-agent/1.0'),
- getVersion: vi.fn().mockResolvedValue(2),
+ getMcpVersion: vi.fn().mockResolvedValue(2),
getOAuthClientName: vi.fn().mockResolvedValue('PostHog Code'),
getReadOnly: vi.fn().mockResolvedValue(true),
getTransport: vi.fn().mockResolvedValue('streamable-http'),
+ getMcpConsumer: vi.fn().mockResolvedValue('posthog-code'),
+ getMcpMode: vi.fn().mockResolvedValue('cli'),
...overrides,
}
}
@@ -186,6 +188,8 @@ describe('initMcpCatObservability', () => {
mcp_oauth_client_name: 'PostHog Code',
read_only: true,
mcp_transport: 'streamable-http',
+ mcp_consumer: 'posthog-code',
+ mcp_mode: 'cli',
$groups: {
organization: 'org-789',
project: 'proj-uuid-101',
@@ -212,13 +216,15 @@ describe('initMcpCatObservability', () => {
mcp_oauth_client_name: 'PostHog Code',
read_only: true,
mcp_transport: 'streamable-http',
+ mcp_consumer: 'posthog-code',
+ mcp_mode: 'cli',
$groups: { organization: 'org-789' },
},
},
{
name: 'no analytics context omits $groups entirely',
overrides: {
- getVersion: vi.fn().mockResolvedValue(undefined),
+ getMcpVersion: vi.fn().mockResolvedValue(undefined),
getClientUserAgent: vi.fn().mockResolvedValue(undefined),
getMcpClientName: vi.fn().mockResolvedValue(undefined),
getMcpClientVersion: vi.fn().mockResolvedValue(undefined),
@@ -228,6 +234,8 @@ describe('initMcpCatObservability', () => {
getOAuthClientName: vi.fn().mockResolvedValue(undefined),
getReadOnly: vi.fn().mockResolvedValue(undefined),
getTransport: vi.fn().mockResolvedValue(undefined),
+ getMcpConsumer: vi.fn().mockResolvedValue(undefined),
+ getMcpMode: vi.fn().mockResolvedValue(undefined),
},
expected: {
ai_product: 'mcp',
@@ -244,6 +252,8 @@ describe('initMcpCatObservability', () => {
mcp_oauth_client_name: undefined,
read_only: undefined,
mcp_transport: undefined,
+ mcp_consumer: undefined,
+ mcp_mode: undefined,
},
},
])('eventProperties: $name', async ({ overrides, expected }) => {