Skip to content

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

Add url to create dashboard and create question tool (#74748)

Phoomparin Manoeaead0153637
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 1cedc4a2e65..ebf776cc5d5 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -160,6 +160,7 @@
metabase.agent-api.init}
:uses #{api
auth-identity
+ channel
collections
dashboards
events
diff --git a/src/metabase/agent_api/api.clj b/src/metabase/agent_api/api.clj
index 14bebbbaaa7..81b96a820a5 100644
--- a/src/metabase/agent_api/api.clj
+++ b/src/metabase/agent_api/api.clj
@@ -10,6 +10,7 @@
[metabase.api.macros.scope :as scope]
[metabase.api.routes.common :as api.routes.common]
[metabase.auth-identity.core :as auth-identity]
+ [metabase.channel.urls :as channel.urls]
[metabase.collections.core :as collections]
[metabase.collections.models.collection :as collection]
[metabase.dashboards.autoplace :as autoplace]
@@ -613,6 +614,7 @@
[:map
[:id ms/PositiveInt]
[:name ms/NonBlankString]
+ [:url :string]
[:display :string]
[:collection_id [:maybe ms/PositiveInt]]
[:description [:maybe :string]]])
@@ -658,6 +660,7 @@
{:id api/*current-user-id*})]
{:id (:id card)
:name (:name card)
+ :url (channel.urls/card-url (:id card))
:display (name (:display card))
:collection_id (:collection_id card)
:description (:description card)})))
@@ -787,6 +790,7 @@
[:map
[:id ms/PositiveInt]
[:name ms/NonBlankString]
+ [:url :string]
[:collection_id [:maybe ms/PositiveInt]]
[:description [:maybe :string]]
[:dashcard_ids [:sequential ms/PositiveInt]]])
@@ -800,7 +804,8 @@
: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.")}}
+ "Cards are auto-positioned on the dashboard grid. "
+ "Returns the dashboard URL.")}}
[_route-params
_query-params
{:keys [description collection_id question_ids]
@@ -832,6 +837,7 @@
(events/publish-event! :event/dashboard-create {:object dash :user-id api/*current-user-id*})
{:id (:id dash)
:name (:name dash)
+ :url (channel.urls/dashboard-url (:id dash))
:collection_id (:collection_id dash)
:description (:description dash)
:dashcard_ids (mapv :id (t2/select :model/DashboardCard :dashboard_id (:id dash)))}))
diff --git a/test/metabase/mcp/api_test.clj b/test/metabase/mcp/api_test.clj
index 394555c9ca9..61db9a8967a 100644
--- a/test/metabase/mcp/api_test.clj
+++ b/test/metabase/mcp/api_test.clj
@@ -14,6 +14,7 @@
[metabase.mcp.tools :as mcp.tools]
[metabase.oauth-server.core :as oauth-server]
[metabase.search.test-util :as search.tu]
+ [metabase.system.settings :as system.settings]
[metabase.test :as mt]
[metabase.test.data.users :as test.users]
[metabase.test.fixtures :as fixtures]
@@ -654,66 +655,73 @@
"create_question" "create_dashboard"
"update_question" "update_dashboard" "create_collection"})
-(deftest tools-call-smoke-test
+(deftest tools-call-smoke-test-covers-all-agent-api-backed-tools-test
(testing "every Agent API-backed tool is exercised by the smoke test"
(is (= (apply disj (set (map :name (mcp.tools/list-tools nil)))
["visualize_query" "render_drill_through"])
smoke-tested-tools)
- "Add the missing tool to `smoke-tested-tools` and the call sequence below."))
+ "Add the missing tool to `smoke-tested-tools` and the call sequence below.")))
+
+(deftest tools-call-smoke-test
(testing "every tool returns a successful response with valid parameters"
- (search.tu/with-legacy-search
- (mt/with-temp [:model/Card _metric {:name "Smoke Metric"
- :type :metric
- :database_id (mt/id)
- :dataset_query (orders-count-query)}]
- (let [[session-id _] (initialize!)
- db-name (t2/select-one-fn :name :model/Database (mt/id))
- orders-query {:lib/type "mbql/query"
- :stages [{:lib/type "mbql.stage/mbql"
- :source-table [db-name "PUBLIC" "ORDERS"]
- :limit 5}]}
- ;; Track write-tool outputs in atoms so the `finally` cleanup runs even if an
- ;; assertion in `call-tool` fails partway through the sequence.
- question-id (atom nil)
- dash-id (atom nil)
- coll-id (atom nil)]
- (try
- (let [;; Discovery tools — call-tool helper asserts (not :isError) internally.
- _ (call-tool session-id "search" {:term_queries ["orders"]})
- ;; Query construction + execution
- construct-data (call-tool session-id "construct_query" {:query orders-query})
- _ (call-tool session-id "query" {:query orders-query})
- _ (call-tool session-id "execute_query"
- {:query_handle (:query_handle construct-data)})
- _ (call-tool session-id "execute_sql"
- {:database_id (mt/id)
- :sql "SELECT 1"})
- _ (call-tool session-id "read_resource"
- {:uris ["metabase://databases"]})
- ;; Write tools — record IDs as soon as they're known so the `finally` block
- ;; can clean up even if a later step throws.
- question-data (call-tool session-id "create_question"
- {:name "Smoke Question"
- :query (mcp.session/read-handle session-id
- (mt/user->id :crowberto)
- (:query_handle construct-data))})
- _ (reset! question-id (:id question-data))
- _ (call-tool session-id "update_question"
- {:id (:id question-data)
- :description "Smoke updated description"})
- dash-data (call-tool session-id "create_dashboard"
- {:name "Smoke Dashboard"})
- _ (reset! dash-id (:id dash-data))
- _ (call-tool session-id "update_dashboard"
- {:id (:id dash-data)
- :description "Smoke updated dashboard"})
- coll-data (call-tool session-id "create_collection"
- {:name "Smoke Collection"})]
- (reset! coll-id (:id coll-data)))
- (finally
- (when-let [qid @question-id] (t2/delete! :model/Card :id qid))
- (when-let [did @dash-id] (t2/delete! :model/Dashboard :id did))
- (when-let [cid @coll-id] (t2/delete! :model/Collection :id cid)))))))))
+ (mt/with-temporary-setting-values [system.settings/site-url "https://stats.metabase.test"]
+ (search.tu/with-legacy-search
+ (mt/with-temp [:model/Card _metric {:name "Smoke Metric"
+ :type :metric
+ :database_id (mt/id)
+ :dataset_query (orders-count-query)}]
+ (let [[session-id _] (initialize!)
+ db-name (t2/select-one-fn :name :model/Database (mt/id))
+ orders-query {:lib/type "mbql/query"
+ :stages [{:lib/type "mbql.stage/mbql"
+ :source-table [db-name "PUBLIC" "ORDERS"]
+ :limit 5}]}
+ ;; Track write-tool outputs in atoms so the `finally` cleanup runs even if an
+ ;; assertion in `call-tool` fails partway through the sequence.
+ question-id (atom nil)
+ dash-id (atom nil)
+ coll-id (atom nil)]
+ (try
+ (let [;; Discovery tools — call-tool helper asserts (not :isError) internally.
+ _ (call-tool session-id "search" {:term_queries ["orders"]})
+ ;; Query construction + execution
+ construct-data (call-tool session-id "construct_query" {:query orders-query})
+ _ (call-tool session-id "query" {:query orders-query})
+ _ (call-tool session-id "execute_query"
+ {:query_handle (:query_handle construct-data)})
+ _ (call-tool session-id "execute_sql"
+ {:database_id (mt/id)
+ :sql "SELECT 1"})
+ _ (call-tool session-id "read_resource"
+ {:uris ["metabase://databases"]})
+ ;; Write tools — record IDs as soon as they're known so the `finally` block
+ ;; can clean up even if a later step throws.
+ question-data (call-tool session-id "create_question"
+ {:name "Smoke Question"
+ :query (mcp.session/read-handle session-id
+ (mt/user->id :crowberto)
+ (:query_handle construct-data))})
+ _ (reset! question-id (:id question-data))
+ _ (is (= (format "https://stats.metabase.test/question/%d" @question-id)
+ (:url question-data)))
+ _ (call-tool session-id "update_question"
+ {:id (:id question-data)
+ :description "Smoke updated description"})
+ dash-data (call-tool session-id "create_dashboard"
+ {:name "Smoke Dashboard"})
+ _ (reset! dash-id (:id dash-data))
+ _ (is (= (format "https://stats.metabase.test/dashboard/%d" @dash-id)
+ (:url dash-data)))
+ _ (call-tool session-id "update_dashboard"
+ {:id (:id dash-data)
+ :description "Smoke updated dashboard"})
+ coll-data (call-tool session-id "create_collection"
+ {:name "Smoke Collection"})]
+ (reset! coll-id (:id coll-data)))
+ (finally
+ (when-let [qid @question-id] (t2/delete! :model/Card :id qid))
+ (when-let [did @dash-id] (t2/delete! :model/Dashboard :id did))
+ (when-let [cid @coll-id] (t2/delete! :model/Collection :id cid))))))))))
(deftest tools-call-visualize-query-direct-test
(testing "visualize_query returns UI structured content"