: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): accept call flags after the tool name in exec (#123263)
Richard Solomou978fc9709153
diff --git a/services/mcp/src/tools/exec.ts b/services/mcp/src/tools/exec.tsindex 4dcd550ec6b5..a6fa28453abf 100644--- a/services/mcp/src/tools/exec.ts+++ b/services/mcp/src/tools/exec.ts@@ -425,6 +425,21 @@ function parseCallFlags(input: string): { forceJson: boolean; confirmed: booleanreturn { forceJson, confirmed, rest }}+const TRAILING_CALL_FLAG = /(?:^|\s)(--json|--confirm)$/++/** Reads `--json` and `--confirm` written after the tool name, before or after the JSON body.+ * Agents put flags there because most CLIs accept them anywhere. No JSON value starts or ends+ * with either token, so removing them never changes a valid body. */+function parseCallBodyFlags(input: string): { forceJson: boolean; confirmed: boolean; rest: string } {+ let { forceJson, confirmed, rest } = parseCallFlags(input)+ for (let match = TRAILING_CALL_FLAG.exec(rest); match; match = TRAILING_CALL_FLAG.exec(rest)) {+ forceJson ||= match[1] === '--json'+ confirmed ||= match[1] === '--confirm'+ rest = rest.slice(0, match.index).trimEnd()+ }+ return { forceJson, confirmed, rest }+}+// Extracts the inner tool name from an exec `call` command, e.g.// "call my-tool {...}" → "my-tool". Returns undefined for other verbs or// malformed input. Used by analytics to surface the real tool being invoked@@ -459,7 +474,7 @@ export function parseExecCallInnerArgs(command: string): Record<string, unknown>if (!callArgs) {return}- const { rest: jsonBody } = parseCommand(callArgs)+ const jsonBody = parseCallBodyFlags(parseCommand(callArgs).rest).restif (!jsonBody) {return {}}@@ -2035,11 +2050,15 @@ export function createExecTool(// belongs in the `internal` bucket its siblings are kept out of.throw new Error('Cannot call PostHog tools without an API context')}- const { forceJson, confirmed, rest: callArgs } = parseCallFlags(rest)- if (!callArgs) {+ const leadingFlags = parseCallFlags(rest)+ if (!leadingFlags.rest) {throw new ExecCommandError(CALL_USAGE, 'usage')}- const { verb: toolName, rest: jsonBody } = parseCommand(callArgs)+ const { verb: toolName, rest: afterToolName } = parseCommand(leadingFlags.rest)+ const bodyFlags = parseCallBodyFlags(afterToolName)+ const forceJson = leadingFlags.forceJson || bodyFlags.forceJson+ const confirmed = leadingFlags.confirmed || bodyFlags.confirmed+ const jsonBody = bodyFlags.restconst tool = findTool(await resolveTools(),scopeGatedTools,diff --git a/services/mcp/tests/unit/exec.test.ts b/services/mcp/tests/unit/exec.test.tsindex 2e42f87e6a25..1bc5dd15d383 100644--- a/services/mcp/tests/unit/exec.test.ts+++ b/services/mcp/tests/unit/exec.test.ts@@ -529,6 +529,9 @@ describe('exec tool', () => {await expect(exec.handler(mockContext, { command: 'call --confirm --json mock-tool' })).resolves.toEqual(JSON.stringify({ id: 1, name: 'test', items: [{ a: 1 }, { a: 2 }] }))+ await expect(exec.handler(mockContext, { command: 'call mock-tool --confirm --json' })).resolves.toEqual(+ JSON.stringify({ id: 1, name: 'test', items: [{ a: 1 }, { a: 2 }] })+ )})it('keeps UI data in structuredContent (not _meta) when the tool has no formatted table', async () => {@@ -725,18 +728,27 @@ describe('exec tool', () => {expect(calls[0]!.properties.output_tokens).not.toBe(calls[1]!.properties.output_tokens)})- it('passes inline JSON arguments to the inner tool', async () => {+ it.each([+ 'call --json mock-tool {"name":"foo","tags":["a","b"]}',+ 'call mock-tool --json {"name":"foo","tags":["a","b"]}',+ 'call mock-tool {"name":"foo","tags":["a","b"]} --json',+ 'call mock-tool --confirm {"name":"foo","tags":["a","b"]} --json',+ ])('passes inline JSON arguments to the inner tool for %s', async (command) => {const tool = makeMockTool({schema: z.object({ name: z.string(), tags: z.array(z.string()) }),handler: async (_ctx, params) => params,})const exec = createExec([tool])- const result = await exec.handler(mockContext, {- command: 'call --json mock-tool {"name":"foo","tags":["a","b"]}',- })+ const result = await exec.handler(mockContext, { command })expect(JSON.parse(result as string)).toEqual({ name: 'foo', tags: ['a', 'b'] })})+ it('reads a flag after the tool name when no JSON follows it', async () => {+ const exec = createExec()+ const result = await exec.handler(mockContext, { command: 'call mock-tool --json' })+ expect(JSON.parse(result as string)).toEqual({ id: 1, name: 'test', items: [{ a: 1 }, { a: 2 }] })+ })+it('preserves quote-heavy and multi-line content in inline JSON', async () => {const tool = makeMockTool({schema: z.object({ name: z.string(), content: z.string() }),diff --git a/services/mcp/tests/unit/skill-analytics.test.ts b/services/mcp/tests/unit/skill-analytics.test.tsindex 0ecc07159eb8..eec5a8ae17d4 100644--- a/services/mcp/tests/unit/skill-analytics.test.ts+++ b/services/mcp/tests/unit/skill-analytics.test.ts@@ -111,6 +111,7 @@ describe('parseExecCallInnerArgs', () => {['a call command', 'call skill-get {"skill_name":"conductor"}', { skill_name: 'conductor' }],['a --json flag', 'call --json skill-get {"skill_name":"conductor"}', { skill_name: 'conductor' }],['a --confirm flag', 'call --confirm skill-get {"skill_name":"conductor"}', { skill_name: 'conductor' }],+ ['a flag after the JSON body', 'call skill-get {"skill_name":"conductor"} --json', { skill_name: 'conductor' }],['extra whitespace and more keys','call skill-get {"skill_name":"conductor","version":4}',