From 10e464aac7460346dfd3b074a960c7de7217a38e Mon Sep 17 00:00:00 2001 From: MaheshtheDev <38828053+MaheshtheDev@users.noreply.github.com> Date: Tue, 29 Sep 2026 20:32:36 +0000 Subject: [PATCH] feat(mcp): group PostHog MCP tool calls by conversation id (#1718) The server is stateless HTTP, so every tool call landed in its own PostHog session. Enable conversation ids and let $mcp_conversation_id and $session_id through the metadata filter so echoed handles group calls. --- apps/mcp/src/server/analytics.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/apps/mcp/src/server/analytics.ts b/apps/mcp/src/server/analytics.ts index 7ceaa937..c2b30c0b 100644 --- a/apps/mcp/src/server/analytics.ts +++ b/apps/mcp/src/server/analytics.ts @@ -4,7 +4,6 @@ import { PostHog } from "posthog-node" import type { ActorContext, ServerEnv } from "./types" const DEFAULT_POSTHOG_HOST = "https://us.i.posthog.com" -// Must match the API's personless id hashing so MCP and API events share one id. const PERSONLESS_DISTINCT_ID_NAMESPACE = "supermemory-posthog-personless-v1" const MCP_EVENT_PROPERTIES = [ "$mcp_source", @@ -17,11 +16,14 @@ const MCP_EVENT_PROPERTIES = [ "$mcp_client_version", "$mcp_protocol_version", "$mcp_listed_tool_names", + "$mcp_conversation_id", + "$session_id", "$groups", ] as const export type WaitUntil = (promise: Promise) => void +// Must match the API's personless id hashing so MCP and API events share one id. async function personlessDistinctId(userId: string): Promise { const input = new TextEncoder().encode( `${PERSONLESS_DISTINCT_ID_NAMESPACE}:${userId}`, @@ -79,6 +81,7 @@ const metadataOnlyMcpEvent: NonNullable = ( } } +// Stateless HTTP builds a server per request, so only the echoed conversation id groups calls. export function instrumentPosthogMcp( server: McpServer, env: ServerEnv, @@ -101,7 +104,7 @@ export function instrumentPosthogMcp( }), context: false, captureModel: false, - enableConversationId: false, + enableConversationId: true, enableExceptionAutocapture: false, beforeSend: metadataOnlyMcpEvent, },