: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(mcp): strip prompt framing from execute-sql tool results (#66526)
Georgiy Tarasovb32aa4d911ce
diff --git a/ee/hogai/context/insight/context.py b/ee/hogai/context/insight/context.pyindex ab44edcf2d8d..dbd048252ee1 100644--- a/ee/hogai/context/insight/context.py+++ b/ee/hogai/context/insight/context.py@@ -78,6 +78,7 @@ class InsightContext:prompt_template: str = INSIGHT_RESULT_TEMPLATE,return_exceptions: bool = False,truncate_results: bool = True,+ include_prompt_framing: bool = True,) -> str:"""Execute query and format results."""effective_query = await self._get_effective_query()@@ -90,6 +91,7 @@ class InsightContext:insight_id=self.insight_model_id,truncate_results=truncate_results,user=self.user,+ include_prompt_framing=include_prompt_framing,)except Exception as e:error_message = f"Error executing query: {str(e)}"diff --git a/ee/hogai/context/insight/query_executor.py b/ee/hogai/context/insight/query_executor.pyindex df3c0a091128..eeb9189e729d 100644--- a/ee/hogai/context/insight/query_executor.py+++ b/ee/hogai/context/insight/query_executor.py@@ -615,6 +615,7 @@ async def execute_and_format_query(insight_id: Optional[int] = None,truncate_results: bool = True,user: Optional["User"] = None,+ include_prompt_framing: bool = True,) -> str:"""Executes a supported query and formats the results for the AI assistant:@@ -630,6 +631,9 @@ async def execute_and_format_query(query: The query to execute.execution_mode: The execution mode to use.insight_id: The insight ID to use.+ include_prompt_framing: When False, return only the formatted results table without the+ example-format description and the surrounding `QUERY_RESULTS_PROMPT` system reminder.+ Used by the MCP `execute_sql` tool, which returns data straight to an external agent.Returns:The formatted query results."""@@ -640,6 +644,8 @@ async def execute_and_format_query(results, used_fallback = await query_runner.arun_and_format_query(query, execution_mode, insight_id, truncate_results=truncate_results)+ if not include_prompt_framing:+ return resultsexample_prompt = FALLBACK_EXAMPLE_PROMPT if used_fallback else get_example_prompt(query)currency = team.base_currency or CurrencyCode.USD.valuediff --git a/ee/hogai/tools/execute_sql/mcp_tool.py b/ee/hogai/tools/execute_sql/mcp_tool.pyindex 18f9c7898cfd..96c93d71aeaa 100644--- a/ee/hogai/tools/execute_sql/mcp_tool.py+++ b/ee/hogai/tools/execute_sql/mcp_tool.py@@ -83,7 +83,7 @@ class ExecuteSQLMCPTool(HogQLOutputParserMixin, MCPTool[ExecuteSQLMCPToolArgs]):user=self._user,)results = await insight_context.execute_and_format(- prompt_template="{{{results}}}", truncate_results=args.truncate+ prompt_template="{{{results}}}", truncate_results=args.truncate, include_prompt_framing=False)return _prepend_taxonomy_warnings(results, taxonomy_warnings)diff --git a/ee/hogai/tools/execute_sql/test/test_mcp_tool.py b/ee/hogai/tools/execute_sql/test/test_mcp_tool.pyindex 86643711ecc6..e8f2c0e2b530 100644--- a/ee/hogai/tools/execute_sql/test/test_mcp_tool.py+++ b/ee/hogai/tools/execute_sql/test/test_mcp_tool.py@@ -35,6 +35,19 @@ class TestExecuteSQLMCPTool(ClickhouseTestMixin, NonAtomicBaseTest):self.assertIn("test_event", content)+ async def test_result_has_no_prompt_framing(self):+ _create_event(team=self.team, distinct_id="user1", event="test_event")++ content = await self.tool.execute(+ ExecuteSQLMCPToolArgs(query="SELECT event, count() as cnt FROM events GROUP BY event"),+ )++ # The MCP tool returns the data table straight to an external agent, so the human-assistant+ # framing (format description + "Here is the results table of the ... insight:" reminder) is stripped.+ self.assertIn("test_event", content)+ self.assertNotIn("You are given a table with the results of a SQL query", content)+ self.assertNotIn("Here is the results table", content)+async def test_validation_error_for_invalid_query(self):with self.assertRaises(MaxToolRetryableError) as ctx:await self.tool.execute(