: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
chore(mcp): add mcp analytics sdk guardrails (#104378)
Georgis Andonis8872766beaad
diff --git a/services/mcp/AGENTS.md b/services/mcp/AGENTS.mdindex d43df609dcde..0b457b3b7bb5 100644--- a/services/mcp/AGENTS.md+++ b/services/mcp/AGENTS.md@@ -8,3 +8,14 @@ Run these commands from the repository root:- Run `pnpm --filter=@posthog/mcp run format` to apply Oxfmt only. Use `lint` and `format:check` for verification; neither command rewrites files.- Run `pnpm --filter=@posthog/mcp run lint:fix` to apply safe Oxlint fixes. It does not apply suggestion fixes that may change behavior.- Run `pnpm --filter=@posthog/mcp run fix` before committing JavaScript, TypeScript, JSON, YAML, CSS, or SCSS changes. It applies safe Oxlint fixes, always runs Oxfmt, and fails if either tool fails.++## MCP Analytics++- Before adding or changing MCP Analytics instrumentation, read the [tracking and observability architecture](ARCHITECTURE.md#tracking-and-observability).+- Prefer `@posthog/mcp-analytics` for all MCP telemetry it supports. The SDK should own reusable MCP Analytics behavior. Server code should only provide the relevant typed data and PostHog-specific integration.+- Do not duplicate SDK behavior with custom event capture, property mapping, session handling, identity handling, or other parallel instrumentation.+- Event and property names beginning with `$mcp_` are owned by MCP Analytics SDKs. Do not invent, add, rename, reinterpret, or manually populate `$mcp_*` fields in server code unless the SDK explicitly exposes them for that purpose.+- Before introducing a new MCP Analytics event, property, or concept, check whether the SDK already supports it or whether it belongs in the SDK.+- If the SDK does not support a required signal or event, discuss the design with `@team-mcp-analytics` in Slack (`#team-mcp-analytics`) before implementing it. Prefer extending the SDK when the signal could be useful to other MCP Analytics customers. A custom event can be more appropriate for a product-specific or one-off need, but sync with the team first.+- Assign [`PostHog/mcp-analytics`](https://github.com/orgs/PostHog/teams/mcp-analytics) as a reviewer on every PR that changes MCP Analytics instrumentation.+- In the PR description, include a short **MCP Analytics** note that summarizes the changed analytics behavior, why it is needed, and whether it uses existing SDK support or requires something new.diff --git a/services/mcp/ARCHITECTURE.md b/services/mcp/ARCHITECTURE.mdindex 50f4b1b70668..8fb8da068621 100644--- a/services/mcp/ARCHITECTURE.md+++ b/services/mcp/ARCHITECTURE.md@@ -90,17 +90,16 @@ There are three independent layers that emit signals about each MCP request:2. **Outbound API headers** — propagated when the MCP server calls PostHog's Django backend, so backend log lines and OTLP spans can correlate with the originating MCP request.3. **Wide structured logs** — single JSON record per request from the Worker itself (see [Wide Logging Pattern](#wide-logging-pattern) above).-#### `$mcp_tool_call` event paths+#### MCP Analytics SDK integration-The canonical event is `$mcp_tool_call`.-The legacy unprefixed `mcp_tool_call` alias is no longer emitted — the transition shim that dual-emitted it through the cutover has been removed (only pre-2026-06-16 history remains under that name).-The path that fires depends on the server mode and on the `mcp-posthog-analytics-sdk` feature flag:+The public [event and property reference](https://posthog.com/docs/mcp-analytics/events) owns the shared wire contract.+The [custom server integration guide](https://posthog.com/docs/mcp-analytics/custom-servers) documents the `PostHogMCP` API used by this Hono server.-- **`hono/analytics.ts`** — homegrown PostHog capture. Used by the exec-mode wrapper to emit events for inner tool calls. Properties use the bare form: `mcp_session_id`, `mcp_conversation_id`, `mcp_client_name`, etc.-- **`lib/mcpcat.ts`** — legacy MCPcat SDK path. Same bare property names.-- **`lib/posthog-mcp-analytics.ts`** — the [`@posthog/mcp-analytics`](https://github.com/PostHog/mcp-analytics) SDK. Property names are `$`-prefixed (`$mcp_session_id`, `$mcp_conversation_id`, …). This is the path most live traffic flows through today.--Adding a new property to events means wiring it into the `McpCatIdentityProvider` interface and the property-builder in **all three** emitters, then sourcing the value on `requestProperties` (or pulling it from another DO-level source).+The server uses one shared `PostHogMCP` client from `src/lib/posthog/client.ts`.+The `src/hono/analytics.ts` module calls SDK helpers for initialization, tool calls, and tool listings.+Direct tool calls and inner exec calls use the same `trackToolCall()` path.+The SDK creates the canonical `$mcp_*` event fields and applies its sanitization and truncation rules.+New `$mcp_*` events and properties belong in the SDK, not in this server.#### Three correlation identifiers