: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): tell PostHog app users to reconnect when project skills need a scope (#109279)
Jake Ruthfb726602bbdb
diff --git a/services/mcp/src/hono/instructions.ts b/services/mcp/src/hono/instructions.tsindex ddde0dd3dd83..b007292b14f2 100644--- a/services/mcp/src/hono/instructions.ts+++ b/services/mcp/src/hono/instructions.ts@@ -5,6 +5,7 @@ import { hasScope } from '@/lib/api'import { isPostHogCodeConsumer } from '@/lib/client-detection'import type { QueryToolInfo } from '@/lib/instructions'import { type InstructionsContext, InstructionsFormatter } from '@/lib/instructions-formatter'+import { isChatGptAppConnection } from '@/lib/oauth-constants'import { formatPrompt } from '@/lib/utils'import { RENDER_UI_RESOURCE_URI } from '@/resources/ui-apps.generated'import { ProjectSkillCatalog } from '@/skills/project-skill-catalog'@@ -37,6 +38,18 @@ const NOTEBOOK_RUN_TOOL = 'notebooks-run'const DOCS_SEARCH_TOOL = 'docs-search'const BUSINESS_KNOWLEDGE_SEARCH_TOOL = 'business-knowledge-documents-search'+// Human comment, not AI slop. Version 2.0.0 of the PostHog app for ChatGPT and Codex started+// requesting `llm_skill:read`, but existing connections never got re-prompted for it, so their+// agents hit a warning they can't act on. Telling them to "reconnect with that scope" doesn't+// work there, since the app picks the scopes, so this nudges them to the thing that does:+// disconnect the app and connect it again. Can be removed after 2027-01-01.+function projectSkillsScopeReason(oauthClientId: string | undefined): string {+ if (isChatGptAppConnection(oauthClientId)) {+ return 'This connection is missing the llm_skill:read scope. A new connection to the PostHog app includes it: disconnect the PostHog app in ChatGPT or Codex and connect it again to read project skills.'+ }+ return 'This connection is missing the llm_skill:read scope. Reconnect with that scope to read project skills.'+}+export class InstructionsBuilder {private readonly formatter: InstructionsFormatterprivate readonly guidelines: string@@ -150,7 +163,7 @@ export class InstructionsBuilder {project: canReadProjectSkills ? new ProjectSkillCatalog(state.context) : undefined,projectUnavailableReason: canReadProjectSkills? undefined- : 'This connection is missing the llm_skill:read scope. Reconnect with that scope to read project skills.',+ : projectSkillsScopeReason(state.oauthClientId),}: undefined,(invocation) => {diff --git a/services/mcp/src/lib/oauth-constants.ts b/services/mcp/src/lib/oauth-constants.tsindex c384ee5e441a..03d1d0b1507f 100644--- a/services/mcp/src/lib/oauth-constants.ts+++ b/services/mcp/src/lib/oauth-constants.ts@@ -44,6 +44,16 @@ export function getUserAgent(opts: GetUserAgentOptions = {}): string {return parts.join(' ')}+// The PostHog app on OpenAI's platform (ChatGPT connector and Codex plugin), same id in US+// and EU. It's the URL of the app's metadata document, so it only changes if we re-register+// the app. Django reads it off the token at introspection, so a client can't spoof it.+// Codex CLI installs register their own ids under `chatgpt.com/oauth/codex/`.+export const CHATGPT_APP_OAUTH_CLIENT_ID = 'https://chatgpt.com/oauth/kxnWncnkxM6t/client.json'++export function isChatGptAppConnection(oauthClientId: string | undefined): boolean {+ return oauthClientId === CHATGPT_APP_OAUTH_CLIENT_ID+}+export const POSTHOG_US_BASE_URL = 'https://us.posthog.com'export const POSTHOG_EU_BASE_URL = 'https://eu.posthog.com'diff --git a/services/mcp/tests/hono/tool-executor.test.ts b/services/mcp/tests/hono/tool-executor.test.tsindex 4f913529ba66..2a7ae9688edb 100644--- a/services/mcp/tests/hono/tool-executor.test.ts+++ b/services/mcp/tests/hono/tool-executor.test.ts@@ -20,6 +20,7 @@ import { ToolExecutor } from '@/hono/tool-executor'import { MCPClientProfile } from '@/lib/client-detection'import { PostHogApiError } from '@/lib/errors'import { buildToolDomainsCompact } from '@/lib/instructions'+import { CHATGPT_APP_OAUTH_CLIENT_ID } from '@/lib/oauth-constants'import { RENDER_UI_RESOURCE_URI, URI_MAP } from '@/resources/ui-apps.generated'import { makeSkillFile, SkillCatalog } from '@/skills/skill-catalog'import { GENERATED_TOOL_MAP } from '@/tools/generated'@@ -501,23 +502,44 @@ describe('ToolExecutor', () => {})})- it('tells the agent project skills need the read scope instead of failing silently', async () => {- const state = makeToolExecutorState([], {- useSingleExec: true,- toolFeatureFlags: { [MCP_EXEC_SKILLS_FEATURE_FLAG]: true },- apiKeyScopes: ['insight:read'],- })-- const result = (await executor.handleToolCall(- { name: 'exec', arguments: { command: 'learn skills' } },- state- )) as { content: { text: string }[] }+ it.each([+ {+ connection: 'the PostHog app for ChatGPT and Codex',+ oauthClientId: CHATGPT_APP_OAUTH_CLIENT_ID,+ reconnectHint: true,+ },+ {+ connection: "the Codex CLI's own OAuth client",+ oauthClientId: 'https://chatgpt.com/oauth/codex/51XaKixG06mz/client.json',+ reconnectHint: false,+ },+ { connection: 'a personal API key', oauthClientId: undefined, reconnectHint: false },+ ])(+ 'tells the agent project skills need the read scope, with a reconnect step only for $connection',+ async ({ oauthClientId, reconnectHint }) => {+ const state = makeToolExecutorState([], {+ useSingleExec: true,+ toolFeatureFlags: { [MCP_EXEC_SKILLS_FEATURE_FLAG]: true },+ apiKeyScopes: ['insight:read'],+ oauthClientId,+ })- expect(JSON.parse(result.content[0]!.text).project).toEqual({- available: false,- reason: expect.stringContaining('llm_skill:read'),- })- })+ const result = (await executor.handleToolCall(+ { name: 'exec', arguments: { command: 'learn skills' } },+ state+ )) as { content: { text: string }[] }++ const { project } = JSON.parse(result.content[0]!.text)+ expect(project.available).toBe(false)+ expect(project.reason).toContain('llm_skill:read')+ if (reconnectHint) {+ expect(project.reason).toContain('disconnect the PostHog app in ChatGPT or Codex')+ } else {+ expect(project.reason).not.toMatch(/ChatGPT|Codex/)+ expect(project.reason).toContain('Reconnect with that scope')+ }+ }+ )// Hosts cache one tool roster and serve it to other accounts, so nothing// account-specific may reach the advertised exec entry. The domain index stays