Skip to content

: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.

feat(mcp): accept object form for *_override params on all insight/dashboard tools (#57445)

Vasco de Krijgerc4268b70ddc3
diff --git a/posthog/api/openapi_parameters.py b/posthog/api/openapi_parameters.py
index f84557da27b2..a4928bf76c2e 100644
--- a/posthog/api/openapi_parameters.py
+++ b/posthog/api/openapi_parameters.py
@@ -17,6 +17,15 @@ posthog/api/insight_variable.py, so they're documented once here:
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter
+# OpenAPI extension marking a query parameter whose wire format is a stringified
+# JSON object. Consumers (currently the MCP codegen at
+# services/mcp/scripts/generate-tools.ts) widen the input schema to accept either
+# a pre-encoded JSON string or a plain object; the runtime then JSON.stringify-s
+# objects before placing them on the URL. The extension is intentionally generic
+# rather than MCP-scoped — frontend codegen and any other downstream consumer
+# can read the same fact about the param.
+ACCEPTS_STRINGIFIED_JSON = {"x-accepts-stringified-json": True}
+
def make_variables_override_param(*, subject_label: str, tool_name: str) -> OpenApiParameter:
"""OpenAPI definition for `variables_override`.
@@ -31,12 +40,13 @@ def make_variables_override_param(*, subject_label: str, tool_name: str) -> Open
"variables_override",
OpenApiTypes.STR,
description=(
- f"JSON object to override {subject_label} variables for this request only (not persisted). "
+ f"Object (or pre-encoded JSON string) to override {subject_label} variables for this request only (not persisted). "
'Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. '
"Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is "
f"to call `{tool_name}` first, copy the matching entry from the response, and mutate `value`. "
"Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token."
),
+ extensions=ACCEPTS_STRINGIFIED_JSON,
)
@@ -51,9 +61,10 @@ def make_filters_override_param(*, subject_label: str) -> OpenApiParameter:
"filters_override",
OpenApiTypes.STR,
description=(
- f"JSON object to override {subject_label} filters for this request only (not persisted). "
+ f"Object (or pre-encoded JSON string) to override {subject_label} filters for this request only (not persisted). "
"Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. "
"Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). "
"Ignored when accessed via a sharing token."
),
+ extensions=ACCEPTS_STRINGIFIED_JSON,
)
diff --git a/products/dashboards/frontend/generated/api.schemas.ts b/products/dashboards/frontend/generated/api.schemas.ts
index b0f6142ac70a..8c54d9deb307 100644
--- a/products/dashboards/frontend/generated/api.schemas.ts
+++ b/products/dashboards/frontend/generated/api.schemas.ts
@@ -602,12 +602,12 @@ export const DashboardsCreateFormat = {
export type DashboardsRetrieveParams = {
/**
- * JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string
format?: DashboardsRetrieveFormat
/**
- * JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string
}
@@ -703,7 +703,7 @@ export const DashboardsReorderTilesCreateFormat = {
export type DashboardsRunInsightsRetrieveParams = {
/**
- * JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string
format?: DashboardsRunInsightsRetrieveFormat
@@ -716,7 +716,7 @@ export type DashboardsRunInsightsRetrieveParams = {
*/
refresh?: DashboardsRunInsightsRetrieveRefresh
/**
- * JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string
}
@@ -760,7 +760,7 @@ export const DashboardsSnapshotCreateFormat = {
export type DashboardsStreamTilesRetrieveParams = {
/**
- * JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string
format?: DashboardsStreamTilesRetrieveFormat
@@ -769,7 +769,7 @@ export type DashboardsStreamTilesRetrieveParams = {
*/
layoutSize?: DashboardsStreamTilesRetrieveLayoutSize
/**
- * JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string
}
diff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.ts
index d87eb564d65c..92ffbade4d71 100644
--- a/products/product_analytics/frontend/generated/api.schemas.ts
+++ b/products/product_analytics/frontend/generated/api.schemas.ts
@@ -9942,7 +9942,7 @@ export const InsightsCreateFormat = {
export type InsightsRetrieveParams = {
/**
- * JSON object to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string
format?: InsightsRetrieveFormat
@@ -9965,7 +9965,7 @@ Background calculation can be tracked using the `query_status` response field.
*/
refresh?: InsightsRetrieveRefresh
/**
- * JSON object to override the insight's HogQL variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override the insight's HogQL variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string
}
diff --git a/services/mcp/schema/tool-inputs.json b/services/mcp/schema/tool-inputs.json
index c41b161c3d7e..0451a2024c48 100644
--- a/services/mcp/schema/tool-inputs.json
+++ b/services/mcp/schema/tool-inputs.json
@@ -858,12 +858,34 @@
"enum": ["optimized", "json"]
},
"variables_override": {
- "description": "JSON object to override the insight's HogQL variables for this run only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response's query variables, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.",
- "type": "string"
+ "description": "Object (or pre-encoded JSON string) to override the insight's HogQL variables for this run only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response's query variables, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.",
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "object",
+ "propertyNames": {
+ "type": "string"
+ },
+ "additionalProperties": {}
+ }
+ ]
},
"filters_override": {
- "description": "JSON object to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.",
- "type": "string"
+ "description": "Object (or pre-encoded JSON string) to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.",
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "object",
+ "propertyNames": {
+ "type": "string"
+ },
+ "additionalProperties": {}
+ }
+ ]
}
},
"required": ["insightId"]
diff --git a/services/mcp/scripts/generate-tools.ts b/services/mcp/scripts/generate-tools.ts
index f3952225507b..39f9dea0a50e 100644
--- a/services/mcp/scripts/generate-tools.ts
+++ b/services/mcp/scripts/generate-tools.ts
@@ -51,6 +51,13 @@ interface OpenApiParam {
required?: boolean
description?: string
schema: OpenApiSchema
+ /**
+ * Set by the Django factories in posthog/api/openapi_parameters.py for
+ * params whose wire format is a URL-encoded JSON string but whose semantic
+ * shape is an object (e.g. variables_override / filters_override). The
+ * codegen widens these into z.union([z.string(), z.record(...)]).
+ */
+ 'x-accepts-stringified-json'?: boolean
}
interface OpenApiSchema {
@@ -267,6 +274,16 @@ function toCamelCase(str: string): string {
return pascal.charAt(0).toLowerCase() + pascal.slice(1)
}
+/**
+ * Escape a description string for embedding inside a single-quoted Zod
+ * `.describe('...')` call. Backslashes first, then single quotes, then collapse
+ * line breaks to spaces. Order matters — escape the escape character before
+ * anything else.
+ */
+function escapeForDescribe(desc: string): string {
+ return desc.trim().replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n\s*/g, ' ')
+}
+
/**
* Parse enrich_url template into prefix, field, and source.
* '{id}' → { prefix: '', field: 'id', source: 'result' }
@@ -512,12 +529,7 @@ function composeToolSchema(
expr += `.default(${JSON.stringify(override.default)}).optional()`
}
if (override.description) {
- const escaped = override.description
- .trim()
- .replace(/\\/g, '\\\\')
- .replace(/'/g, "\\'")
- .replace(/\n\s*/g, ' ')
- expr += `.describe('${escaped}')`
+ expr += `.describe('${escapeForDescribe(override.description)}')`
}
if (override.optional) {
expr += '.optional()'
@@ -544,6 +556,37 @@ function composeToolSchema(
}
}
+ // Query params marked `x-accepts-stringified-json` (set by Django factories
+ // in posthog/api/openapi_parameters.py) are stringified-JSON-objects on the
+ // wire but conceptually objects. Widen the schema to accept either shape so
+ // LLM agents can pass the object literal they just read from a sibling
+ // tool's response, without a JSON.stringify round-trip that frequently
+ // breaks on escaping. ApiClient.request() in services/mcp/src/api/client.ts
+ // JSON-stringify-s object query params automatically, so the schema-side
+ // union is sufficient — no handler changes required. Skipped when the YAML
+ // config also defines a param_overrides entry for the field, so explicit
+ // YAML always wins.
+ const explicitOverrideKeys = new Set(Object.keys(config.param_overrides ?? {}))
+ const stringifiedJsonQueryParams = queryParams.filter(
+ (p) =>
+ p['x-accepts-stringified-json'] === true &&
+ // queryParamNames is the post-include/exclude set, so this drops
+ // params the YAML excluded via include_params / exclude_params.
+ queryParamNames.includes(p.name) &&
+ !explicitOverrideKeys.has(p.name)
+ )
+ if (stringifiedJsonQueryParams.length > 0) {
+ const overrideEntries = stringifiedJsonQueryParams.map((p) => {
+ let expr = 'z.union([z.string(), z.record(z.string(), z.unknown())]).optional()'
+ const desc = p.description ?? p.schema?.description
+ if (desc) {
+ expr += `.describe('${escapeForDescribe(desc)}')`
+ }
+ return `${p.name}: ${expr}`
+ })
+ schemaExpr = `(${schemaExpr}).extend({ ${overrideEntries.join(', ')} })`
+ }
+
return {
orvalImports,
toolInputsImports,
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index d8c01c53f4a1..63d23f4cc940 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -38163,12 +38163,12 @@ export namespace Schemas {
export type EnvironmentsDashboardsRetrieveParams = {
/**
- * JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string;
format?: EnvironmentsDashboardsRetrieveFormat;
/**
- * JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string;
};
@@ -38267,7 +38267,7 @@ export namespace Schemas {
export type EnvironmentsDashboardsRunInsightsRetrieveParams = {
/**
- * JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string;
format?: EnvironmentsDashboardsRunInsightsRetrieveFormat;
@@ -38280,7 +38280,7 @@ export namespace Schemas {
*/
refresh?: EnvironmentsDashboardsRunInsightsRetrieveRefresh;
/**
- * JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string;
};
@@ -38324,7 +38324,7 @@ export namespace Schemas {
export type EnvironmentsDashboardsStreamTilesRetrieveParams = {
/**
- * JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string;
format?: EnvironmentsDashboardsStreamTilesRetrieveFormat;
@@ -38333,7 +38333,7 @@ export namespace Schemas {
*/
layoutSize?: EnvironmentsDashboardsStreamTilesRetrieveLayoutSize;
/**
- * JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string;
};
@@ -39365,7 +39365,7 @@ export namespace Schemas {
export type EnvironmentsInsightsRetrieveParams = {
/**
- * JSON object to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string;
format?: EnvironmentsInsightsRetrieveFormat;
@@ -39388,7 +39388,7 @@ export namespace Schemas {
*/
refresh?: EnvironmentsInsightsRetrieveRefresh;
/**
- * JSON object to override the insight's HogQL variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override the insight's HogQL variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string;
};
@@ -42311,12 +42311,12 @@ export namespace Schemas {
export type DashboardsRetrieveParams = {
/**
- * JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string;
format?: DashboardsRetrieveFormat;
/**
- * JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string;
};
@@ -42415,7 +42415,7 @@ export namespace Schemas {
export type DashboardsRunInsightsRetrieveParams = {
/**
- * JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string;
format?: DashboardsRunInsightsRetrieveFormat;
@@ -42428,7 +42428,7 @@ export namespace Schemas {
*/
refresh?: DashboardsRunInsightsRetrieveRefresh;
/**
- * JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string;
};
@@ -42472,7 +42472,7 @@ export namespace Schemas {
export type DashboardsStreamTilesRetrieveParams = {
/**
- * JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string;
format?: DashboardsStreamTilesRetrieveFormat;
@@ -42481,7 +42481,7 @@ export namespace Schemas {
*/
layoutSize?: DashboardsStreamTilesRetrieveLayoutSize;
/**
- * JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string;
};
@@ -43818,7 +43818,7 @@ export namespace Schemas {
export type InsightsRetrieveParams = {
/**
- * JSON object to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.
*/
filters_override?: string;
format?: InsightsRetrieveFormat;
@@ -43841,7 +43841,7 @@ export namespace Schemas {
*/
refresh?: InsightsRetrieveRefresh;
/**
- * JSON object to override the insight's HogQL variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
+ * Object (or pre-encoded JSON string) to override the insight's HogQL variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.
*/
variables_override?: string;
};
diff --git a/services/mcp/src/generated/dashboards/api.ts b/services/mcp/src/generated/dashboards/api.ts
index 73e6f2f2615b..fc6970c4dda4 100644
--- a/services/mcp/src/generated/dashboards/api.ts
+++ b/services/mcp/src/generated/dashboards/api.ts
@@ -88,14 +88,14 @@ export const DashboardsRetrieveQueryParams = /* @__PURE__ */ zod.object({
.string()
.optional()
.describe(
- 'JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.'
+ 'Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.'
),
format: zod.enum(['json', 'txt']).optional(),
variables_override: zod
.string()
.optional()
.describe(
- 'JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
+ 'Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
),
})
@@ -197,7 +197,7 @@ export const DashboardsRunInsightsRetrieveQueryParams = /* @__PURE__ */ zod.obje
.string()
.optional()
.describe(
- 'JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.'
+ 'Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.'
),
format: zod.enum(['json', 'txt']).optional(),
output_format: zod
@@ -216,6 +216,6 @@ export const DashboardsRunInsightsRetrieveQueryParams = /* @__PURE__ */ zod.obje
.string()
.optional()
.describe(
- 'JSON object to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
+ 'Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
),
})
diff --git a/services/mcp/src/generated/product_analytics/api.ts b/services/mcp/src/generated/product_analytics/api.ts
index e3cdb2057959..34cfcf239701 100644
--- a/services/mcp/src/generated/product_analytics/api.ts
+++ b/services/mcp/src/generated/product_analytics/api.ts
@@ -188,7 +188,7 @@ export const InsightsRetrieveQueryParams = /* @__PURE__ */ zod.object({
.string()
.optional()
.describe(
- "JSON object to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
+ "Object (or pre-encoded JSON string) to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
),
format: zod.enum(['csv', 'json']).optional(),
from_dashboard: zod
@@ -215,7 +215,7 @@ export const InsightsRetrieveQueryParams = /* @__PURE__ */ zod.object({
.string()
.optional()
.describe(
- 'JSON object to override the insight\'s HogQL variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
+ 'Object (or pre-encoded JSON string) to override the insight\'s HogQL variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
),
})
diff --git a/services/mcp/src/schema/tool-inputs.ts b/services/mcp/src/schema/tool-inputs.ts
index 54c7ef94a70e..dcbc29409f94 100644
--- a/services/mcp/src/schema/tool-inputs.ts
+++ b/services/mcp/src/schema/tool-inputs.ts
@@ -335,16 +335,16 @@ export const InsightQueryInputSchema = z.object({
'Output format. "optimized" returns a human-readable summary from server-side formatters (recommended for analysis). "json" returns the raw query results as JSON.'
),
variables_override: z
- .string()
+ .union([z.string(), z.record(z.string(), z.unknown())])
.optional()
.describe(
- 'JSON object to override the insight\'s HogQL variables for this run only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response\'s query variables, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
+ 'Object (or pre-encoded JSON string) to override the insight\'s HogQL variables for this run only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response\'s query variables, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
),
filters_override: z
- .string()
+ .union([z.string(), z.record(z.string(), z.unknown())])
.optional()
.describe(
- "JSON object to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
+ "Object (or pre-encoded JSON string) to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
),
})
diff --git a/services/mcp/src/tools/generated/dashboards.ts b/services/mcp/src/tools/generated/dashboards.ts
index ac8a832d01dc..06be0ad0c926 100644
--- a/services/mcp/src/tools/generated/dashboards.ts
+++ b/services/mcp/src/tools/generated/dashboards.ts
@@ -123,9 +123,22 @@ const dashboardDelete = (): ToolBase<typeof DashboardDeleteSchema, Schemas.Dashb
},
})
-const DashboardGetSchema = DashboardsRetrieveParams.omit({ project_id: true }).extend(
- DashboardsRetrieveQueryParams.omit({ format: true }).shape
-)
+const DashboardGetSchema = DashboardsRetrieveParams.omit({ project_id: true })
+ .extend(DashboardsRetrieveQueryParams.omit({ format: true }).shape)
+ .extend({
+ filters_override: z
+ .union([z.string(), z.record(z.string(), z.unknown())])
+ .optional()
+ .describe(
+ 'Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.'
+ ),
+ variables_override: z
+ .union([z.string(), z.record(z.string(), z.unknown())])
+ .optional()
+ .describe(
+ 'Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
+ ),
+ })
const dashboardGet = (): ToolBase<typeof DashboardGetSchema, WithPostHogUrl<Schemas.Dashboard>> => ({
name: 'dashboard-get',
@@ -181,9 +194,22 @@ const dashboardGet = (): ToolBase<typeof DashboardGetSchema, WithPostHogUrl<Sche
},
})
-const DashboardInsightsRunSchema = DashboardsRunInsightsRetrieveParams.omit({ project_id: true }).extend(
- DashboardsRunInsightsRetrieveQueryParams.omit({ format: true }).shape
-)
+const DashboardInsightsRunSchema = DashboardsRunInsightsRetrieveParams.omit({ project_id: true })
+ .extend(DashboardsRunInsightsRetrieveQueryParams.omit({ format: true }).shape)
+ .extend({
+ filters_override: z
+ .union([z.string(), z.record(z.string(), z.unknown())])
+ .optional()
+ .describe(
+ 'Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.'
+ ),
+ variables_override: z
+ .union([z.string(), z.record(z.string(), z.unknown())])
+ .optional()
+ .describe(
+ 'Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
+ ),
+ })
const dashboardInsightsRun = (): ToolBase<
typeof DashboardInsightsRunSchema,
diff --git a/services/mcp/src/tools/generated/product_analytics.ts b/services/mcp/src/tools/generated/product_analytics.ts
index f594e54b6536..9d78b7b296ab 100644
--- a/services/mcp/src/tools/generated/product_analytics.ts
+++ b/services/mcp/src/tools/generated/product_analytics.ts
@@ -165,9 +165,22 @@ const insightDelete = (): ToolBase<typeof InsightDeleteSchema, Schemas.Insight>
},
})
-const InsightGetSchema = InsightsRetrieveParams.omit({ project_id: true }).extend(
- InsightsRetrieveQueryParams.omit({ format: true, from_dashboard: true, refresh: true }).shape
-)
+const InsightGetSchema = InsightsRetrieveParams.omit({ project_id: true })
+ .extend(InsightsRetrieveQueryParams.omit({ format: true, from_dashboard: true, refresh: true }).shape)
+ .extend({
+ filters_override: z
+ .union([z.string(), z.record(z.string(), z.unknown())])
+ .optional()
+ .describe(
+ "Object (or pre-encoded JSON string) to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
+ ),
+ variables_override: z
+ .union([z.string(), z.record(z.string(), z.unknown())])
+ .optional()
+ .describe(
+ 'Object (or pre-encoded JSON string) to override the insight\'s HogQL variables for this request only (not persisted). Format: {"<variable_id>": {"code_name": "<code_name>", "variableId": "<variable_id>", "value": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.'
+ ),
+ })
const insightGet = (): ToolBase<typeof InsightGetSchema, WithPostHogUrl<Schemas.Insight>> => ({
name: 'insight-get',
diff --git a/services/mcp/src/tools/insights/query.ts b/services/mcp/src/tools/insights/query.ts
index 2ec8c86d233f..f72cc2d0437f 100644
--- a/services/mcp/src/tools/insights/query.ts
+++ b/services/mcp/src/tools/insights/query.ts
@@ -14,6 +14,25 @@ type Params = z.infer<typeof schema>
type Result = WithPostHogUrl<{ query: unknown; insight: Insight & { url: string }; results: unknown }>
+// Accept either a pre-encoded JSON string or a plain object for the override
+// params. LLM agents reading the insight-get response see `variables` as an
+// object, so requiring them to JSON.stringify before sending is friction that
+// frequently breaks (escaping, double-encoding). Normalising here lets either
+// shape reach the backend as a properly-encoded query-string value.
+//
+// Transitional: the auto-generated tools rely on ApiClient.request() in
+// services/mcp/src/api/client.ts, which already JSON-stringify-s object query
+// params automatically. This helper exists because the bespoke insights().get()
+// in client.ts builds its own URLSearchParams and types the override params as
+// `string`. Once that endpoint migrates onto request(), normalizeOverride can
+// be deleted.
+function normalizeOverride(value: string | Record<string, unknown> | undefined): string | undefined {
+ if (value === undefined) {
+ return undefined
+ }
+ return typeof value === 'string' ? value : JSON.stringify(value)
+}
+
export const queryHandler: ToolBase<typeof schema, Result>['handler'] = async (context: Context, params: Params) => {
const { insightId, output_format, variables_override, filters_override } = params
const projectId = await context.stateManager.getProjectId()
@@ -23,9 +42,11 @@ export const queryHandler: ToolBase<typeof schema, Result>['handler'] = async (c
// apply_dashboard_filters_to_dict). The merged query is then POSTed to /query/
// as-is, so insight-query results reflect the overridden values without
// mutating the saved insight.
- const insightResult = await context.api
- .insights({ projectId })
- .get({ insightId, variables_override, filters_override })
+ const insightResult = await context.api.insights({ projectId }).get({
+ insightId,
+ variables_override: normalizeOverride(variables_override),
+ filters_override: normalizeOverride(filters_override),
+ })
if (!insightResult.success) {
throw new Error(`Failed to get insight: ${insightResult.error.message}`)
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/dashboard-get.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/dashboard-get.json
index 8bff77787813..3a65c2914419 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/dashboard-get.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/dashboard-get.json
@@ -2,16 +2,38 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"filters_override": {
- "description": "JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.",
- "type": "string"
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "additionalProperties": {},
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ }
+ ],
+ "description": "Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
},
"id": {
"description": "A unique integer value identifying this dashboard.",
"type": "number"
},
"variables_override": {
- "description": "JSON object to override dashboard variables for this request only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.",
- "type": "string"
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "additionalProperties": {},
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ }
+ ],
+ "description": "Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token."
}
},
"required": ["id"],
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/dashboard-insights-run.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/dashboard-insights-run.json
index b1f50c6827de..2fdee766e23e 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/dashboard-insights-run.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/dashboard-insights-run.json
@@ -2,8 +2,19 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"filters_override": {
- "description": "JSON object to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.",
- "type": "string"
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "additionalProperties": {},
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ }
+ ],
+ "description": "Object (or pre-encoded JSON string) to override dashboard filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
},
"id": {
"description": "A unique integer value identifying this dashboard.",
@@ -20,8 +31,19 @@
"type": "string"
},
"variables_override": {
- "description": "JSON object to override dashboard variables for this request only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.",
- "type": "string"
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "additionalProperties": {},
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ }
+ ],
+ "description": "Object (or pre-encoded JSON string) to override dashboard variables for this request only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `dashboard-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token."
}
},
"required": ["id"],
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/insight-get.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/insight-get.json
index 6c9927cb302b..2ac444800948 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/insight-get.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/insight-get.json
@@ -2,8 +2,19 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"filters_override": {
- "description": "JSON object to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.",
- "type": "string"
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "additionalProperties": {},
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ }
+ ],
+ "description": "Object (or pre-encoded JSON string) to override the insight's filters for this request only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
},
"id": {
"anyOf": [
@@ -17,8 +28,19 @@
"description": "Numeric primary key or 8-character `short_id` (for example `AaVQ8Ijw`) identifying the insight."
},
"variables_override": {
- "description": "JSON object to override the insight's HogQL variables for this request only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.",
- "type": "string"
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "additionalProperties": {},
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ }
+ ],
+ "description": "Object (or pre-encoded JSON string) to override the insight's HogQL variables for this request only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token."
}
},
"required": ["id"],
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/insight-query.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/insight-query.json
index 0984fd226757..2c03bfc86630 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/insight-query.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/insight-query.json
@@ -2,8 +2,19 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"filters_override": {
- "description": "JSON object to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token.",
- "type": "string"
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "additionalProperties": {},
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ }
+ ],
+ "description": "Object (or pre-encoded JSON string) to override the insight's filters for this run only (not persisted). Top-level keys replace; nested values are not deep-merged — pass the complete value for any key you override. Accepts the same keys as the dashboard filters schema (e.g., `date_from`, `date_to`, `properties`). Ignored when accessed via a sharing token."
},
"insightId": {
"description": "The insight ID or short_id to run.",
@@ -16,8 +27,19 @@
"type": "string"
},
"variables_override": {
- "description": "JSON object to override the insight's HogQL variables for this run only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response's query variables, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token.",
- "type": "string"
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "additionalProperties": {},
+ "propertyNames": {
+ "type": "string"
+ },
+ "type": "object"
+ }
+ ],
+ "description": "Object (or pre-encoded JSON string) to override the insight's HogQL variables for this run only (not persisted). Format: {\"<variable_id>\": {\"code_name\": \"<code_name>\", \"variableId\": \"<variable_id>\", \"value\": <new_value>}}. Each entry must include `code_name` — partial entries are silently dropped. The simplest workflow is to call `insight-get` first, copy the matching entry from the response's query variables, and mutate `value`. Top-level keys replace; nested values are not deep-merged. Ignored when accessed via a sharing token."
}
},
"required": ["insightId"],
diff --git a/services/mcp/tests/unit/generate-tools.test.ts b/services/mcp/tests/unit/generate-tools.test.ts
index 14f145d494af..9aa5ad821ba6 100644
--- a/services/mcp/tests/unit/generate-tools.test.ts
+++ b/services/mcp/tests/unit/generate-tools.test.ts
@@ -622,6 +622,126 @@ describe('rename_params', () => {
})
})
+describe('x-accepts-stringified-json query params', () => {
+ function resolvedWith(parameters: NonNullable<ResolvedOperation['operation']['parameters']>): ResolvedOperation {
+ return makeResolved({
+ operation: {
+ operationId: 'things_list',
+ parameters,
+ },
+ })
+ }
+
+ it('widens schema for params marked x-accepts-stringified-json', () => {
+ const config: ToolConfig = { operation: 'things_list', enabled: true }
+ const resolved = resolvedWith([
+ {
+ in: 'query',
+ name: 'filters_override',
+ schema: { type: 'string' },
+ description: 'Filters override.',
+ 'x-accepts-stringified-json': true,
+ },
+ ])
+
+ const result = composeToolSchema(config, resolved, makeSpec(), stubGetQuerySchema)
+
+ expect(result.schemaExpr).toContain(
+ "filters_override: z.union([z.string(), z.record(z.string(), z.unknown())]).optional().describe('Filters override.')"
+ )
+ })
+
+ it('does not widen sibling params that lack the extension', () => {
+ const config: ToolConfig = { operation: 'things_list', enabled: true }
+ const resolved = resolvedWith([
+ {
+ in: 'query',
+ name: 'filters_override',
+ schema: { type: 'string' },
+ 'x-accepts-stringified-json': true,
+ },
+ {
+ in: 'query',
+ name: 'plain_string',
+ schema: { type: 'string' },
+ },
+ ])
+
+ const result = composeToolSchema(config, resolved, makeSpec(), stubGetQuerySchema)
+
+ expect(result.schemaExpr).toContain('filters_override: z.union([z.string()')
+ expect(result.schemaExpr).not.toContain('plain_string: z.union(')
+ })
+
+ it('does not widen params named *_override without the extension', () => {
+ const config: ToolConfig = { operation: 'things_list', enabled: true }
+ const resolved = resolvedWith([
+ {
+ in: 'query',
+ name: 'filters_override',
+ schema: { type: 'string' },
+ // no x-accepts-stringified-json — magic naming alone must not trigger widening
+ },
+ ])
+
+ const result = composeToolSchema(config, resolved, makeSpec(), stubGetQuerySchema)
+
+ expect(result.schemaExpr).not.toContain('z.union([z.string()')
+ })
+
+ it('skips widening when the YAML config also defines a param_override for the same field', () => {
+ const config: ToolConfig = {
+ operation: 'things_list',
+ enabled: true,
+ param_overrides: {
+ filters_override: { description: 'Custom YAML description' },
+ },
+ }
+ const resolved = resolvedWith([
+ {
+ in: 'query',
+ name: 'filters_override',
+ schema: { type: 'string' },
+ description: 'OpenAPI description',
+ 'x-accepts-stringified-json': true,
+ },
+ ])
+
+ const result = composeToolSchema(config, resolved, makeSpec(), stubGetQuerySchema)
+
+ // YAML wins — describe() comes through, no union extension afterwards.
+ expect(result.schemaExpr).toContain('Custom YAML description')
+ expect(result.schemaExpr).not.toContain('z.union([z.string()')
+ })
+
+ it('respects exclude_params — excluded fields are not widened', () => {
+ const config: ToolConfig = {
+ operation: 'things_list',
+ enabled: true,
+ exclude_params: ['filters_override'],
+ }
+ const resolved = resolvedWith([
+ {
+ in: 'query',
+ name: 'filters_override',
+ schema: { type: 'string' },
+ 'x-accepts-stringified-json': true,
+ },
+ {
+ in: 'query',
+ name: 'variables_override',
+ schema: { type: 'string' },
+ 'x-accepts-stringified-json': true,
+ },
+ ])
+
+ const result = composeToolSchema(config, resolved, makeSpec(), stubGetQuerySchema)
+
+ expect(result.schemaExpr).not.toContain('filters_override: z.union(')
+ expect(result.schemaExpr).toContain('variables_override: z.union([z.string()')
+ })
+})
+
describe('QueryWrapperToolConfigSchema validation', () => {
it.each([true, false] as const)('accepts use_optimized_output: %s', (value) => {
const result = QueryWrapperToolConfigSchema.safeParse({
diff --git a/services/mcp/tests/unit/insights-query-handler.test.ts b/services/mcp/tests/unit/insights-query-handler.test.ts
new file mode 100644
index 000000000000..6a1dadea930e
--- /dev/null
+++ b/services/mcp/tests/unit/insights-query-handler.test.ts
@@ -0,0 +1,128 @@
+import { describe, expect, it, vi } from 'vitest'
+
+import { queryHandler } from '@/tools/insights/query'
+import type { Context } from '@/tools/types'
+
+const filtersOverrideObject = { date_from: '-7d' }
+const variablesOverrideObject = {
+ '019d4838-1da4-0000-33c7-2561bf01f1c9': {
+ code_name: 'eventname',
+ variableId: '019d4838-1da4-0000-33c7-2561bf01f1c9',
+ value: 'signed_up',
+ },
+}
+
+interface InsightsCallArgs {
+ insightId: string
+ variables_override?: string
+ filters_override?: string
+}
+
+interface MockHandles {
+ context: Context
+ insightsGet: ReturnType<typeof vi.fn>
+ insightsQuery: ReturnType<typeof vi.fn>
+}
+
+function createContext(): MockHandles {
+ const insightsGet = vi.fn().mockResolvedValue({
+ success: true,
+ data: {
+ id: 42,
+ short_id: 'abc12345',
+ query: { kind: 'HogQLQuery', query: 'select 1' },
+ },
+ })
+ const insightsQuery = vi.fn().mockResolvedValue({
+ success: true,
+ data: { columns: ['c'], results: [[1]] },
+ })
+ const context = {
+ api: {
+ insights: vi.fn().mockReturnValue({
+ get: insightsGet,
+ query: insightsQuery,
+ }),
+ request: vi.fn(),
+ getProjectBaseUrl: vi.fn().mockReturnValue('https://us.posthog.com/project/1'),
+ },
+ stateManager: {
+ getProjectId: vi.fn().mockResolvedValue('1'),
+ getOrgID: vi.fn(),
+ getRegion: vi.fn().mockResolvedValue('us'),
+ },
+ env: { POSTHOG_BASE_URL: 'https://us.posthog.com' },
+ sessionManager: {},
+ cache: {},
+ getDistinctId: async () => 'test',
+ } as unknown as Context
+
+ return { context, insightsGet, insightsQuery }
+}
+
+describe('queryHandler — overrides forwarding', () => {
+ it('forwards a string variables_override unchanged', async () => {
+ const { context, insightsGet } = createContext()
+ const variables_override = JSON.stringify(variablesOverrideObject)
+
+ await queryHandler(context, { insightId: '42', output_format: 'json', variables_override })
+
+ const call = insightsGet.mock.calls[0]![0] as InsightsCallArgs
+ expect(call.variables_override).toBe(variables_override)
+ expect(call.filters_override).toBeUndefined()
+ })
+
+ it('JSON.stringify-s an object variables_override before forwarding', async () => {
+ const { context, insightsGet } = createContext()
+
+ await queryHandler(context, {
+ insightId: '42',
+ output_format: 'json',
+ // Cast through unknown — at runtime this is what an LLM sends; the
+ // tool schema accepts it, but the inner client typing is string-only.
+ variables_override: variablesOverrideObject as unknown as string,
+ })
+
+ const call = insightsGet.mock.calls[0]![0] as InsightsCallArgs
+ expect(call.variables_override).toBe(JSON.stringify(variablesOverrideObject))
+ })
+
+ it('JSON.stringify-s an object filters_override before forwarding', async () => {
+ const { context, insightsGet } = createContext()
+
+ await queryHandler(context, {
+ insightId: '42',
+ output_format: 'json',
+ filters_override: filtersOverrideObject as unknown as string,
+ })
+
+ const call = insightsGet.mock.calls[0]![0] as InsightsCallArgs
+ expect(call.filters_override).toBe(JSON.stringify(filtersOverrideObject))
+ })
+
+ it('handles undefined overrides without coercing them to "undefined"', async () => {
+ const { context, insightsGet } = createContext()
+
+ await queryHandler(context, { insightId: '42', output_format: 'json' })
+
+ const call = insightsGet.mock.calls[0]![0] as InsightsCallArgs
+ expect(call.variables_override).toBeUndefined()
+ expect(call.filters_override).toBeUndefined()
+ })
+
+ it('forwards mixed string + object overrides correctly', async () => {
+ const { context, insightsGet } = createContext()
+ const filters_override = JSON.stringify(filtersOverrideObject)
+
+ await queryHandler(context, {
+ insightId: '42',
+ output_format: 'json',
+ variables_override: variablesOverrideObject as unknown as string,
+ filters_override,
+ })
+
+ const call = insightsGet.mock.calls[0]![0] as InsightsCallArgs
+ expect(call.variables_override).toBe(JSON.stringify(variablesOverrideObject))
+ expect(call.filters_override).toBe(filters_override)
+ })
+})