: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
feat(experiments): attach shared metrics via experiment-update MCP tool (#65479)
Juraj Majerik3fe3a4c19357
diff --git a/products/experiments/mcp/tools.yaml b/products/experiments/mcp/tools.yamlindex 26fbc4c5f972..005b1946ef27 100644--- a/products/experiments/mcp/tools.yaml+++ b/products/experiments/mcp/tools.yaml@@ -844,6 +844,7 @@ tools:- running_time_calculation- metrics- metrics_secondary+ - saved_metrics_ids- stats_config- exposure_criteria- holdout_id@@ -863,6 +864,8 @@ tools:recommended_running_time (days), and exposure_estimate_config. These values are kept in sync withthe legacy parameters.* keys during the deprecation window, so prefer this field over writing thecalculator keys inside parameters.+ saved_metrics_ids:+ input_schema: SavedMetricsAttachSchemaui_app: experimentresponse:include:@@ -880,6 +883,7 @@ tools:- running_time_calculation- metrics- metrics_secondary+ - saved_metrics- conclusion- conclusion_commentexperiments-create-exposure-cohort:diff --git a/services/mcp/schema/tool-inputs.json b/services/mcp/schema/tool-inputs.jsonindex c4e748b2fd2d..79c78a88ce10 100644--- a/services/mcp/schema/tool-inputs.json+++ b/services/mcp/schema/tool-inputs.json@@ -688,6 +688,34 @@"properties": {},"description": "No input required. Returns core data warehouse schemas."},+ "SavedMetricsAttachSchema": {+ "type": "array",+ "items": {+ "type": "object",+ "properties": {+ "id": {+ "type": "integer",+ "minimum": -9007199254740991,+ "maximum": 9007199254740991,+ "description": "ID of an existing shared/saved metric. Discover IDs with experiment-saved-metrics-list."+ },+ "metadata": {+ "description": "Optional per-link metadata. Omit to default this metric to primary.",+ "type": "object",+ "properties": {+ "type": {+ "type": "string",+ "enum": ["primary", "secondary"],+ "description": "Whether this metric is a primary or secondary metric on the experiment."+ }+ },+ "required": ["type"]+ }+ },+ "required": ["id"]+ },+ "description": "The complete desired set of shared (saved) metrics for the experiment — this REPLACES all existing saved-metric links, it does not append. To add or remove one, first read the experiment's current saved_metrics via experiment-get and resend the full set. Pass an empty array to detach all shared metrics."+ },"ScoreDefinitionConfigSchema": {"anyOf": [{diff --git a/services/mcp/src/schema/tool-inputs.ts b/services/mcp/src/schema/tool-inputs.tsindex d52d99a69cc1..1be482d0163b 100644--- a/services/mcp/src/schema/tool-inputs.ts+++ b/services/mcp/src/schema/tool-inputs.ts@@ -251,11 +251,30 @@ export const FeedbackSubmitSchema = z.object({details: z.string().optional()- .describe(- "Any additional context that doesn't fit the other fields. Keep it to clear, concise bullet points."- ),+ .describe("Any additional context that doesn't fit the other fields. Keep it to clear, concise bullet points."),})+const SavedMetricAttachItemSchema = z.object({+ id: z+ .number()+ .int()+ .describe('ID of an existing shared/saved metric. Discover IDs with experiment-saved-metrics-list.'),+ metadata: z+ .object({+ type: z+ .enum(['primary', 'secondary'])+ .describe('Whether this metric is a primary or secondary metric on the experiment.'),+ })+ .optional()+ .describe('Optional per-link metadata. Omit to default this metric to primary.'),+})++export const SavedMetricsAttachSchema = z+ .array(SavedMetricAttachItemSchema)+ .describe(+ "The complete desired set of shared (saved) metrics for the experiment — this REPLACES all existing saved-metric links, it does not append. To add or remove one, first read the experiment's current saved_metrics via experiment-get and resend the full set. Pass an empty array to detach all shared metrics."+ )+export const ExperimentResultsGetSchema = z.object({id: z.number().describe('The ID of the experiment to get comprehensive results for'),refresh: zdiff --git a/services/mcp/src/tools/generated/experiments.ts b/services/mcp/src/tools/generated/experiments.tsindex 9dcaa8216a92..4d1b2ae71656 100644--- a/services/mcp/src/tools/generated/experiments.ts+++ b/services/mcp/src/tools/generated/experiments.ts@@ -35,6 +35,7 @@ import {ExperimentsUnarchiveCreateParams,} from '@/generated/experiments/api'import { withUiApp } from '@/resources/ui-apps'+import { SavedMetricsAttachSchema } from '@/schema/tool-inputs'import { castStringToInt } from '@/tools/cast-helpers'import { withPostHogUrl, pickResponseFields, type WithPostHogUrl } from '@/tools/tool-utils'import type { Context, ToolBase, ZodObjectAny } from '@/tools/types'@@ -737,7 +738,6 @@ const ExperimentUpdateSchema = ExperimentsPartialUpdateParams.omit({ project_id:feature_flag_key: true,excluded_variants: true,secondary_metrics: true,- saved_metrics_ids: true,filters: true,deleted: true,type: true,@@ -753,6 +753,7 @@ const ExperimentUpdateSchema = ExperimentsPartialUpdateParams.omit({ project_id:running_time_calculation: ExperimentsPartialUpdateBody.shape['running_time_calculation'].describe("Persist a running-time / sample-size plan onto the experiment (the planning target shown in the experiment's running-time panel). Object with optional keys: minimum_detectable_effect (percentage, e.g. 20 for a 20% lift), recommended_sample_size (total across all variants), recommended_running_time (days), and exposure_estimate_config. These values are kept in sync with the legacy parameters.* keys during the deprecation window, so prefer this field over writing the calculator keys inside parameters."),+ saved_metrics_ids: SavedMetricsAttachSchema.optional(),})const experimentUpdate = (): ToolBase<typeof ExperimentUpdateSchema, WithPostHogUrl<Schemas.Experiment>> =>@@ -777,6 +778,9 @@ const experimentUpdate = (): ToolBase<typeof ExperimentUpdateSchema, WithPostHogif (params.running_time_calculation !== undefined) {body['running_time_calculation'] = params.running_time_calculation}+ if (params.saved_metrics_ids !== undefined) {+ body['saved_metrics_ids'] = params.saved_metrics_ids+ }if (params.archived !== undefined) {body['archived'] = params.archived}@@ -824,6 +828,7 @@ const experimentUpdate = (): ToolBase<typeof ExperimentUpdateSchema, WithPostHog'running_time_calculation','metrics','metrics_secondary',+ 'saved_metrics','conclusion','conclusion_comment',]) as typeof resultdiff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-update.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-update.jsonindex b3cd1190c071..c9c331f4a42a 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-update.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-update.json@@ -3760,6 +3760,34 @@],"description": "Persist a running-time / sample-size plan onto the experiment (the planning target shown in the experiment's running-time panel). Object with optional keys: minimum_detectable_effect (percentage, e.g. 20 for a 20% lift), recommended_sample_size (total across all variants), recommended_running_time (days), and exposure_estimate_config. These values are kept in sync with the legacy parameters.* keys during the deprecation window, so prefer this field over writing the calculator keys inside parameters."},+ "saved_metrics_ids": {+ "description": "The complete desired set of shared (saved) metrics for the experiment — this REPLACES all existing saved-metric links, it does not append. To add or remove one, first read the experiment's current saved_metrics via experiment-get and resend the full set. Pass an empty array to detach all shared metrics.",+ "items": {+ "properties": {+ "id": {+ "description": "ID of an existing shared/saved metric. Discover IDs with experiment-saved-metrics-list.",+ "maximum": 9007199254740991,+ "minimum": -9007199254740991,+ "type": "integer"+ },+ "metadata": {+ "description": "Optional per-link metadata. Omit to default this metric to primary.",+ "properties": {+ "type": {+ "description": "Whether this metric is a primary or secondary metric on the experiment.",+ "enum": ["primary", "secondary"],+ "type": "string"+ }+ },+ "required": ["type"],+ "type": "object"+ }+ },+ "required": ["id"],+ "type": "object"+ },+ "type": "array"+ },"stats_config": {},"update_feature_flag_params": {"description": "When true, sync feature flag configuration from parameters to the linked feature flag. Draft experiments always sync regardless of update_feature_flag_params, so only required for non-drafts.",