Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Refuse to save a query handle that carries bound parameter values (#82434)

bryanf6caace66a80
diff --git a/src/metabase/mcp/v2/queries.clj b/src/metabase/mcp/v2/queries.clj
index bd2924817e9..0db25233c92 100644
--- a/src/metabase/mcp/v2/queries.clj
+++ b/src/metabase/mcp/v2/queries.clj
@@ -139,7 +139,17 @@
re-runs the shape and permission guards, and — unlike the MBQL read path — DOES allow a native
query through. `execute_sql` mints handles specifically so their SQL can be saved; the
native-reject guard would otherwise make those handles unsaveable. Returns
- `{:query <decoded map> :prompt <string-or-nil>}`, or throws a teaching error."
+ `{:query <decoded map> :prompt <string-or-nil>}`, or throws a teaching error.
+
+ Refuses a handle carrying bound `:parameters`. `execute_sql` re-attaches the values it ran
+ with so the handle re-runs and visualizes as what the agent saw, but `:parameters` is
+ runtime-only — [[metabase.lib.schema]]'s serialize-query strips it on the way into
+ `dataset_query`. Saving such a handle would therefore persist the query WITHOUT its filter and
+ without complaining: a card built from `WHERE quantity > 4` falls back to the tag's default and
+ returns rows the agent's own run excluded. That is a disclosure, not just a wrong row count, so
+ the save path fails closed rather than guessing at the card shape the values should have become
+ (a card `:parameters` entry or a template-tag `:default`, which differ between native and MBQL
+ handles)."
[mcp-session-id user-id handle]
(let [{:keys [encoded_query prompt]}
(or (mcp.session/resolve-query-handle mcp-session-id user-id handle)
@@ -147,6 +157,13 @@
query (decode-stored-query encoded_query)]
(query-guards/validate-serialized-query! query)
(query-guards/check-token-query-permissions! query)
+ (when (seq (:parameters query))
+ (common/throw-teaching-error
+ (str "This query_handle carries bound parameter values, which a saved query can't keep — "
+ "storing it would drop the filter and save a question that returns rows the run you "
+ "saw excluded. Save it with the filter built in instead: pass `native` with "
+ "`template_tags` giving each tag a `default`, or re-run the query with the values "
+ "written into the SQL/MBQL filter itself and save that handle.")))
{:query query :prompt prompt}))
;;; ------------------------------------------------ Raw-SQL kill switch -------------------------------------------
diff --git a/test/metabase/mcp/v2/tools/question_test.clj b/test/metabase/mcp/v2/tools/question_test.clj
index b2589f54460..5f7fc2274fe 100644
--- a/test/metabase/mcp/v2/tools/question_test.clj
+++ b/test/metabase/mcp/v2/tools/question_test.clj
@@ -272,6 +272,33 @@
(is (str/includes? (-> result :content first :text) "mcp-execute-sql-enabled"))
(is (zero? (t2/count :model/Card :name "Killed Handle Q")))))))))))
+;; not ^:parallel: mt/with-model-cleanup on the shared query-handle table
+(deftest create-via-parameterized-query-handle-is-refused-test
+ (testing "a handle carrying bound :parameters is refused rather than saved without them.
+ `execute_sql` re-attaches the values it ran with, but serialize-query strips
+ :parameters on the way into dataset_query — saving one would persist the query minus
+ its filter, so the card returns rows the agent's own run excluded (disclosure, not
+ just a wrong count). Fail closed instead of guessing the card shape."
+ (mt/with-model-cleanup [:model/Card :model/McpQueryHandle]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [mp (mt/metadata-provider)
+ query (lib/native-query mp "SELECT * FROM ORDERS WHERE QUANTITY > {{minq}}")
+ params [{:type :number :target [:variable [:template-tag "minq"]] :value 4}]
+ sid (str (random-uuid))
+ handle (v2.queries/mint-query-handle!
+ sid (mt/user->id :crowberto)
+ (v2.queries/encode-serialized-query
+ (assoc (lib/prepare-for-serialization query) :parameters params)))
+ result (call-tool #{"agent:content:write" "agent:sql:run"} sid "question_write"
+ {:method "create" :name "Parameterized Handle Q"
+ :query_handle handle})]
+ (is (:isError result))
+ (is (str/includes? (-> result :content first :text) "bound parameter values"))
+ (testing "the teaching error names the concrete alternative"
+ (is (str/includes? (-> result :content first :text) "template_tags")))
+ (testing "nothing is written"
+ (is (zero? (t2/count :model/Card :name "Parameterized Handle Q")))))))))
+
;; not ^:parallel: mt/with-model-cleanup on the shared query-handle table
(deftest update-via-native-query-handle-is-gated-test
(testing "the update path stores the resolved query the same way create does, so a native handle