: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(flags): accept key param aliases on feature-flag-get-definition-by-key (#73041)
posthog[bot]8b57eef4161e
diff --git a/services/mcp/src/tools/featureFlags/getDefinitionByKey.ts b/services/mcp/src/tools/featureFlags/getDefinitionByKey.tsindex fb21b5281926..06f176811ba7 100644--- a/services/mcp/src/tools/featureFlags/getDefinitionByKey.ts+++ b/services/mcp/src/tools/featureFlags/getDefinitionByKey.ts@@ -2,6 +2,7 @@ import { z } from 'zod'import type { Schemas } from '@/api/generated'import { ToolInputValidationError } from '@/lib/errors'+import { normalizeParamAliases } from '@/tools/cast-helpers'import { withPostHogUrl, type WithPostHogUrl } from '@/tools/tool-utils'import type { Context, ToolBase } from '@/tools/types'@@ -22,9 +23,20 @@ import type { Context, ToolBase } from '@/tools/types'* keep throwing where the caller expects the target to already exist, so a miss signals a* genuine problem rather than an expected outcome.*/-const schema = z.object({- key: z.string().describe('The feature flag key: the string identifier used in code (e.g. "new-checkout").'),-})+// Agents composing this call in exec mode reconstruct the param name from the tool+// name rather than reading the advertised schema, and reach for `flagKey` /+// `flag_key` / `feature_flag_key` where the schema requires `key` — production+// traces show that mismatch, not a genuinely absent key, as the dominant+// validation failure. Normalize the aliases onto `key` before validation (same+// pattern as the insight-id aliases; see normalizeParamAliases). The canonical+// `key` still wins on conflict, and the advertised JSON schema is unchanged —+// preprocess renders as the wrapped object, so it still shows only `key`.+const schema = z.preprocess(+ normalizeParamAliases({ key: ['flagKey', 'flag_key', 'feature_flag_key', 'featureFlagKey'] }),+ z.object({+ key: z.string().describe('The feature flag key: the string identifier used in code (e.g. "new-checkout").'),+ })+)type Params = z.infer<typeof schema>diff --git a/services/mcp/tests/unit/feature-flag-get-definition-by-key.test.ts b/services/mcp/tests/unit/feature-flag-get-definition-by-key.test.tsindex 3757992cca85..52f21bb45710 100644--- a/services/mcp/tests/unit/feature-flag-get-definition-by-key.test.ts+++ b/services/mcp/tests/unit/feature-flag-get-definition-by-key.test.ts@@ -82,4 +82,31 @@ describe('feature-flag-get-definition-by-key', () => {)expect(request).not.toHaveBeenCalled()})++ // Agents reconstruct this call from the tool name in exec mode and send the+ // key as `flagKey` / `flag_key` / `feature_flag_key`; production traces show+ // that mismatch as the dominant validation failure. Every alias must+ // normalize onto `key` (canonical wins on conflict) or those failures return.+ describe('key aliases', () => {+ it.each([+ ['key', { key: 'new-checkout' }],+ ['flagKey', { flagKey: 'new-checkout' }],+ ['flag_key', { flag_key: 'new-checkout' }],+ ['feature_flag_key', { feature_flag_key: 'new-checkout' }],+ ['featureFlagKey', { featureFlagKey: 'new-checkout' }],+ ['key over an alias on conflict', { key: 'new-checkout', flagKey: 'other' }],+ ])('normalizes %s to `key`', (_label, input) => {+ const result = tool.schema.safeParse(input)+ expect(result.success).toBe(true)+ const data = result.data as Record<string, unknown>+ expect(data.key).toBe('new-checkout')+ for (const alias of ['flagKey', 'flag_key', 'feature_flag_key', 'featureFlagKey']) {+ expect(data).not.toHaveProperty(alias)+ }+ })++ it('still rejects a call with no key under any accepted name', () => {+ expect(tool.schema.safeParse({}).success).toBe(false)+ })+ })})