Skip to content

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

Respect explicit null collection_id in MCP creation (#75727)

Chris Truter813d41e75574
diff --git a/src/metabase/agent_api/api.clj b/src/metabase/agent_api/api.clj
index d7ffc95d0df..e022805006d 100644
--- a/src/metabase/agent_api/api.clj
+++ b/src/metabase/agent_api/api.clj
@@ -756,6 +756,7 @@
or a base64-encoded MBQL string. MCP callers should always use the handle.
Optionally specify display type, description, collection, and visualization settings.
If `collection_id` is omitted the question is saved to the caller's personal collection.
+ Pass an explicit `null` to save it to the root collection.
The response `collection_path` is the saved location."
{:scope metabot/agent-question-create
:tool {:name "create_question"
@@ -763,15 +764,21 @@
"Pass the `query_handle` returned by `construct_query`. "
"Optionally set display type (table, bar, line, pie, etc.), "
"description, and target collection. "
- "If you omit collection_id it's saved to the user's personal collection. "
+ "If you omit collection_id it's saved to the user's personal collection; "
+ "pass an explicit null to save it to the root collection. "
"Report the saved location from the response `collection_path`.")}}
[_route-params
_query-params
- {:keys [query display description collection_id visualization_settings]
- question-name :name}
+ {:keys [query display description visualization_settings]
+ question-name :name
+ :as body}
:- ::create-question-request]
(let [dataset-query (-> query u/decode-base64 json/decode+kw)
- collection_id (or collection_id (personal-collection-id))]
+ ;; `nil` means the root collection, so only default to the personal collection when the
+ ;; key is absent. `(or ...)` would silently turn an explicit `null` into personal.
+ collection_id (if (contains? body :collection_id)
+ (:collection_id body)
+ (personal-collection-id))]
;; Mirror REST `POST /api/card/` pre-checks before calling `queries/create-card!`.
;; `create-card!` itself does NOT run permissions checks; without these mirroring the
;; REST endpoint, an LLM caller could (a) save a card whose query references data the
@@ -940,21 +947,28 @@
Pass `question_ids` to add existing saved questions as cards on the dashboard.
Cards are automatically positioned on the grid based on their display type.
If `collection_id` is omitted the dashboard is saved to the caller's personal collection.
+ Pass an explicit `null` to save it to the root collection.
The response `collection_path` is the saved location."
{:scope metabot/agent-dashboard-create
:tool {:name "create_dashboard"
:description (str "Create a dashboard in Metabase. "
"Optionally pass question_ids to add saved questions as cards. "
"Cards are auto-positioned on the dashboard grid. "
- "If you omit collection_id it's saved to the user's personal collection. "
+ "If you omit collection_id it's saved to the user's personal collection; "
+ "pass an explicit null to save it to the root collection. "
"Report the saved location from the response `collection_path`. "
"Returns the dashboard URL.")}}
[_route-params
_query-params
- {:keys [description collection_id question_ids]
- dashboard-name :name}
+ {:keys [description question_ids]
+ dashboard-name :name
+ :as body}
:- ::create-dashboard-request]
- (let [collection_id (or collection_id (personal-collection-id))]
+ ;; `nil` means the root collection, so only default to the personal collection when the
+ ;; key is absent. `(or ...)` would silently turn an explicit `null` into personal.
+ (let [collection_id (if (contains? body :collection_id)
+ (:collection_id body)
+ (personal-collection-id))]
(api/create-check :model/Dashboard {:collection_id collection_id})
(let [cards (when (seq question_ids)
(mapv #(api/read-check :model/Card %) question_ids))
diff --git a/test/metabase/agent_api/api_test.clj b/test/metabase/agent_api/api_test.clj
index 6758c8b1338..12ebc2ae4dd 100644
--- a/test/metabase/agent_api/api_test.clj
+++ b/test/metabase/agent_api/api_test.clj
@@ -620,6 +620,19 @@
:query (:query construct-resp)
:collection_id locked-id}))))))
+(deftest create-question-explicit-null-collection-test
+ (testing "An explicit null collection_id saves to the root collection, not the personal default"
+ (let [construct-resp (mt/user-http-request :rasta :post 200 "agent/v2/construct-query"
+ {:query (orders-query :limit 10)})
+ create-resp (mt/user-http-request :rasta :post 200 "agent/v1/question"
+ {:name "Agent Root Question"
+ :query (:query construct-resp)
+ :collection_id nil})]
+ (is (=? {:collection_id nil
+ :collection_path "Our analytics"}
+ create-resp))
+ (t2/delete! :model/Card :id (:id create-resp)))))
+
(deftest create-question-collection-path-test
(testing "collection_path is the full breadcrumb, mirroring the app's location"
(mt/with-temp [:model/Collection {parent-id :id} {:name "Parent Coll"}
@@ -715,6 +728,16 @@
{:name "Bad Dashboard"
:question_ids [999999]})))
+(deftest create-dashboard-explicit-null-collection-test
+ (testing "An explicit null collection_id saves to the root collection, not the personal default"
+ (let [resp (mt/user-http-request :rasta :post 200 "agent/v1/dashboard"
+ {:name "Agent Root Dashboard"
+ :collection_id nil})]
+ (is (=? {:collection_id nil
+ :collection_path "Our analytics"}
+ resp))
+ (t2/delete! :model/Dashboard :id (:id resp)))))
+
(deftest create-entity-url-test
(testing "create question/dashboard return a frontend URL"
(testing "absolute when site-url is set"