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.

feat(posthog_ai): send distinct mcp-consumer header for posthog_ai runs (#66622)

Georgiy Tarasov58a4930c82f9
diff --git a/products/tasks/backend/temporal/process_task/tests/test_utils.py b/products/tasks/backend/temporal/process_task/tests/test_utils.py
index 493b895a3caf..6d662ed8aebf 100644
--- a/products/tasks/backend/temporal/process_task/tests/test_utils.py
+++ b/products/tasks/backend/temporal/process_task/tests/test_utils.py
@@ -172,6 +172,7 @@ class TestGetSandboxMcpConfigs(TestCase):
("posthog-code", "posthog-code"),
("some-other-origin", "posthog-code"),
("slack", "slack"),
+ ("posthog_ai", "posthog_ai"),
]
)
def test_consumer_header_reflects_interaction_origin(
@@ -262,6 +263,7 @@ class TestFetchUserMcpServerConfigs(TestCase):
@parameterized.expand(
[
("slack", "slack"),
+ ("posthog_ai", "posthog_ai"),
("posthog_code", "posthog-code"),
(None, "posthog-code"),
]
diff --git a/products/tasks/backend/temporal/process_task/utils.py b/products/tasks/backend/temporal/process_task/utils.py
index 84fb5ac95810..fd38dc310df0 100644
--- a/products/tasks/backend/temporal/process_task/utils.py
+++ b/products/tasks/backend/temporal/process_task/utils.py
@@ -312,13 +312,18 @@ def get_user_mcp_server_configs(
def _resolve_mcp_consumer(interaction_origin: str | None) -> str:
"""Map the task's interaction origin to the `x-posthog-mcp-consumer` value.
- Slack-launched runs send `"slack"`; everything else (the PostHog Code UI,
- API callers, missing origin) is treated as PostHog Code. The MCP server
- gates UI-apps payloads on the literal `"posthog-code"` — keep in sync with
- `POSTHOG_CODE_CONSUMER` in `services/mcp/src/lib/client-detection.ts`.
+ Slack-launched runs send `"slack"` and posthog_ai (Max) runs send
+ `"posthog_ai"`; everything else (the PostHog Code UI, API callers, missing
+ origin) is treated as PostHog Code. Only `"posthog-code"` is a UI-apps host
+ on the MCP server — it gates UI-apps payload emission, so `"posthog_ai"` and
+ `"slack"` deliberately don't get UI apps. Keep the `"posthog-code"` literal
+ in sync with `POSTHOG_CODE_CONSUMER` in
+ `services/mcp/src/lib/client-detection.ts`.
"""
if interaction_origin == "slack":
return "slack"
+ if interaction_origin == "posthog_ai":
+ return "posthog_ai"
return "posthog-code"
diff --git a/services/mcp/src/lib/client-detection.ts b/services/mcp/src/lib/client-detection.ts
index b830d8f36d07..7dc53044a314 100644
--- a/services/mcp/src/lib/client-detection.ts
+++ b/services/mcp/src/lib/client-detection.ts
@@ -137,7 +137,9 @@ export function resolveEffectiveClientName(
// Value sent in `x-posthog-mcp-consumer` by PostHog Code (the Tasks sandbox
// wrapper around the Claude Agent SDK) when the task was launched from the
// PostHog Code UI. Used to force coding-agent behavior and to gate UI-apps
-// emission in single-exec mode. Slack-launched runs send `"slack"` instead.
+// emission in single-exec mode. Slack-launched runs send `"slack"` and
+// posthog_ai (Max) runs send `"posthog_ai"`; only PostHog Code renders MCP UI
+// apps, so this is the sole consumer that gates UI-apps payload emission.
export const POSTHOG_CODE_CONSUMER = 'posthog-code'
// OAuth application names (from token introspection) for upstream tools that
diff --git a/services/mcp/src/tools/exec.ts b/services/mcp/src/tools/exec.ts
index 9e8f9908587b..25d0306ea93f 100644
--- a/services/mcp/src/tools/exec.ts
+++ b/services/mcp/src/tools/exec.ts
@@ -420,8 +420,8 @@ export function createExecTool(
// carrying `structuredContent` + `_meta.ui.resourceUri`. Clients only see
// the `exec` tool registered in single-exec mode, so the UI metadata has to
// ride on the per-call response. Gated on the consumer because other
- // single-exec callers (direct Claude Code, cline, Slack-launched runs, etc.)
- // don't render UI apps — they should see plain text.
+ // single-exec callers (direct Claude Code, cline, Slack- and posthog_ai-launched
+ // runs, etc.) don't render UI apps — they should see plain text.
if (tool._meta?.ui?.resourceUri && isPostHogCodeConsumer(mcpConsumer)) {
const isStringResult = typeof result === 'string'
const distinctId = isStringResult ? undefined : await context.getDistinctId()
diff --git a/services/mcp/tests/unit/client-detection.test.ts b/services/mcp/tests/unit/client-detection.test.ts
index 07ccbbc8f0e9..7b1f1923da6c 100644
--- a/services/mcp/tests/unit/client-detection.test.ts
+++ b/services/mcp/tests/unit/client-detection.test.ts
@@ -163,8 +163,8 @@ describe('isPostHogCodeConsumer', () => {
expect(isPostHogCodeConsumer('posthog-code')).toBe(true)
})
- it.each([['posthog_code'], ['PostHog-Code'], ['posthog-code-v2'], ['posthog'], ['slack'], ['']])(
- 'returns false for %s (must be exact match)',
+ it.each([['posthog_code'], ['PostHog-Code'], ['posthog-code-v2'], ['posthog'], ['slack'], ['posthog_ai'], ['']])(
+ 'returns false for %s (must be exact match — posthog_ai is not a UI-apps host)',
(consumer) => {
expect(isPostHogCodeConsumer(consumer)).toBe(false)
}
@@ -351,9 +351,12 @@ describe('MCPClientProfile', () => {
expect(new MCPClientProfile({ consumer: POSTHOG_CODE_CONSUMER }).isPostHogCodeConsumer()).toBe(true)
})
- it.each([['slack'], ['posthog'], ['PostHog-Code'], ['']])('returns false for %s', (consumer) => {
- expect(new MCPClientProfile({ consumer }).isPostHogCodeConsumer()).toBe(false)
- })
+ it.each([['slack'], ['posthog'], ['PostHog-Code'], ['posthog_ai'], ['']])(
+ 'returns false for %s',
+ (consumer) => {
+ expect(new MCPClientProfile({ consumer }).isPostHogCodeConsumer()).toBe(false)
+ }
+ )
it('returns false when consumer is undefined', () => {
expect(new MCPClientProfile({}).isPostHogCodeConsumer()).toBe(false)
diff --git a/services/mcp/tests/unit/exec.test.ts b/services/mcp/tests/unit/exec.test.ts
index 460a74c4884d..2dc29f1e143c 100644
--- a/services/mcp/tests/unit/exec.test.ts
+++ b/services/mcp/tests/unit/exec.test.ts
@@ -204,7 +204,8 @@ describe('exec tool', () => {
expect(result.__execBuiltPayload).toBe(true)
})
- it.each([[undefined], ['cline'], ['claude-code'], ['slack'], ['posthog_code']])(
+ // posthog_ai is sent as its own consumer for attribution but is NOT a UI-apps host.
+ it.each([[undefined], ['cline'], ['claude-code'], ['slack'], ['posthog_code'], ['posthog_ai']])(
'returns plain text (no UI payload) when consumer is %s even if the inner tool has a UI app',
async (consumer) => {
const tool = makeMockTool({