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.

fix(mcp): preserve error metadata when adding compatibility hints (#114089)

Vasco de Krijger76fd0eb6b70e
diff --git a/ee/hogai/tools/execute_sql/mcp_tool.py b/ee/hogai/tools/execute_sql/mcp_tool.py
index 34f7c7ebe591..7243bdf3610e 100644
--- a/ee/hogai/tools/execute_sql/mcp_tool.py
+++ b/ee/hogai/tools/execute_sql/mcp_tool.py
@@ -118,7 +118,7 @@ class ExecuteSQLMCPTool(HogQLOutputParserMixin, MCPTool[ExecuteSQLMCPToolArgs]):
hint = build_compatibility_hint(str(e))
if not hint:
raise
- raise MaxToolRetryableError(f"{e}\n\n{hint}") from e
+ raise MaxToolRetryableError(f"{e}\n\n{hint}", error_type=e.error_type, error_code=e.error_code) from e
return MCPToolResult(
content=_prepend_taxonomy_warnings(results, taxonomy_warnings),
diff --git a/products/posthog_ai/backend/api/test_mcp_tools.py b/products/posthog_ai/backend/api/test_mcp_tools.py
index 7948c46c4e1e..92317714d303 100644
--- a/products/posthog_ai/backend/api/test_mcp_tools.py
+++ b/products/posthog_ai/backend/api/test_mcp_tools.py
@@ -438,6 +438,18 @@ class TestMCPToolsAPI(APIBaseTest):
"You may retry with adjusted inputs.",
expected_error_code="s3_error",
),
+ param(
+ "deferred_compatibility_rejection",
+ None,
+ "Function 'greatest' expects 2 arguments, found 3",
+ None,
+ "Tool failed: MaxToolRetryableError: Function 'greatest' expects 2 arguments, found 3\n\n"
+ "<hogql_compatibility_hint>\n"
+ "`greatest` takes exactly 2 arguments in HogQL, unlike ClickHouse. Nest two-argument calls "
+ "instead: greatest(x1, greatest(x2, x3)).\n"
+ "</hogql_compatibility_hint>. You may retry with adjusted inputs.",
+ query_args={"query": "SELECT greatest(1, 2, 3)", "connectionId": "example-connection"},
+ ),
param(
"memory_limit",
None,
@@ -477,6 +489,7 @@ class TestMCPToolsAPI(APIBaseTest):
_mock_sleep: AsyncMock,
*,
expected_error_code: str | None = None,
+ query_args: dict[str, str] | None = None,
expected_error_type: str = "internal",
) -> None:
mock_query.return_value = {"query_status": {"id": "test-query-id", "complete": False}}
@@ -491,7 +504,7 @@ class TestMCPToolsAPI(APIBaseTest):
response = self.client.post(
f"/api/environments/{self.team.id}/mcp_tools/execute_sql/",
- {"args": {"query": "SELECT 1"}},
+ {"args": query_args or {"query": "SELECT 1"}},
format="json",
)