: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): guide agents through exec discovery (#72403)
Dylan Martin8553c28b5d0d
diff --git a/services/mcp/src/tools/exec.ts b/services/mcp/src/tools/exec.tsindex f855283adbb6..9b8e1f695cc2 100644--- a/services/mcp/src/tools/exec.ts+++ b/services/mcp/src/tools/exec.ts@@ -259,15 +259,22 @@ function stripOutputFormatProperty(jsonSchema: Record<string, unknown>): Record<return { ...jsonSchema, properties: rest }}-function findTool(tools: Tool<ZodObjectAny>[], name: string): Tool<ZodObjectAny> {+function findTool(tools: Tool<ZodObjectAny>[], scopeGatedTools: ScopeGatedTool[], name: string): Tool<ZodObjectAny> {const tool = tools.find((t) => t.name === name)if (!tool) {const redirect = DEPRECATED_TOOL_REDIRECTS[name]if (redirect) {throw new Error(redirect(tools))}- const available = tools.map((t) => t.name).join(', ')- throw new Error(`Unknown tool: "${name}". Available tools: ${available}`)+ const scopeGatedTool = scopeGatedTools.find((candidate) => candidate.name === name)+ if (scopeGatedTool) {+ throw new Error(+ `Tool "${name}" exists, but this MCP connection is missing the required scope(s): ${scopeGatedTool.missingScopes.join(', ')}. Reconnect or reauthorize the PostHog MCP connection and approve these scopes. Logging in to PostHog in a browser does not update MCP permissions.`+ )+ }+ throw new Error(+ `Unknown tool: "${name}". Run "search ${name}" to find the current tool name before claiming the capability is unavailable.`+ )}return tool}@@ -409,7 +416,7 @@ export function createExecTool(if (!infoArgs) {throw new Error('Usage: info [--json] <tool_name>')}- const tool = findTool(allTools, infoArgs)+ const tool = findTool(allTools, scopeGatedTools, infoArgs)// `io: 'input'` mirrors the advertised `tools/list` schema and the executor's// validation: fields with a Zod `.default()` (e.g. a query `kind` discriminator)// are optional and auto-filled. The default `io: 'output'` would list them as@@ -452,7 +459,7 @@ export function createExecTool(throw new Error('Usage: schema <tool_name> [field_path]')}const { verb: schemaToolName, rest: fieldPath } = parseCommand(rest)- const schemaTool = findTool(allTools, schemaToolName)+ const schemaTool = findTool(allTools, scopeGatedTools, schemaToolName)// See the `info` command: `io: 'input'` keeps this in sync with the advertised// schema and validation, so `.default()` fields aren't shown as required.const fullJsonSchema = stripOutputFormatProperty(@@ -502,7 +509,7 @@ export function createExecTool(throw new Error('Usage: call [--json] [--confirm] <tool_name> <json_input>')}const { verb: toolName, rest: jsonBody } = parseCommand(callArgs)- const tool = findTool(allTools, toolName)+ const tool = findTool(allTools, scopeGatedTools, toolName)if (options.requireDestructiveConfirmation && tool.annotations.destructiveHint && !confirmed) {throw new Error(`Tool "${tool.name}" is destructive. Re-run with "call --confirm ${tool.name} ..." after verifying the target IDs. Use "info ${tool.name}" to inspect the tool first.`diff --git a/services/mcp/tests/unit/exec.test.ts b/services/mcp/tests/unit/exec.test.tsindex a4123cc99d38..d9b6e0c85d0d 100644--- a/services/mcp/tests/unit/exec.test.ts+++ b/services/mcp/tests/unit/exec.test.ts@@ -1155,6 +1155,34 @@ describe('exec tool', () => {})})+ describe('unavailable tool recovery', () => {+ it.each(['info endpoint-create', 'schema endpoint-create', 'call endpoint-create {}'])(+ 'explains how to recover when %s is scope-gated',+ async (command) => {+ const exec = createExecTool([makeMockTool()], mockContext, 'desc', 'cmd', undefined, undefined, [+ {+ name: 'endpoint-create',+ title: 'Create endpoint',+ description: 'Create a new endpoint',+ missingScopes: ['endpoint:write'],+ },+ ])++ await expect(exec.handler(mockContext, { command })).rejects.toThrow(+ /exists[\s\S]*endpoint:write[\s\S]*reauthorize[\s\S]*browser does not update MCP permissions/i+ )+ }+ )++ it('directs unknown tool names back to catalog search', async () => {+ const exec = createExec()++ await expect(exec.handler(mockContext, { command: 'call endpoint-make {}' })).rejects.toThrow(+ /search endpoint-make[\s\S]*before claiming the capability is unavailable/i+ )+ })+ })+describe('parseExecCallInnerToolName', () => {it.each([['call my-tool {}', 'my-tool'],