The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix MCP search's source card fields for metrics (#85509)
Chris Truter22dacc7768ed
diff --git a/src/metabase/mcp/v2/tools/search.clj b/src/metabase/mcp/v2/tools/search.cljindex bda0289ed7e..5d43ad5255c 100644--- a/src/metabase/mcp/v2/tools/search.clj+++ b/src/metabase/mcp/v2/tools/search.clj@@ -71,8 +71,10 @@(m/remove-vals nil? (select-keys row concise-row-keys)));; The v1 engine enriches some rows (metrics) with its own field vocabulary; the v2 surface;; names entity ids `entity_id` and never emits portable FK arrays, so translate/strip here.+ ;; A metric's source card is referenced by its numeric `source_card_id` here, so its portable+ ;; entity id goes too.:detailed (fn [row]- (cond-> (dissoc row :portable_entity_id :base_table_portable_fk)+ (cond-> (dissoc row :portable_entity_id :base_table_portable_fk :source_card_portable_entity_id)(and (:portable_entity_id row) (nil? (:entity_id row)))(assoc :entity_id (:portable_entity_id row)))):sample {:id 1@@ -101,7 +103,10 @@:entity_id "x":base_table_id 1:base_table_name "x"- :base_table_schema "x"}})+ :base_table_schema "x"+ :source_card_id 1+ :source_card_name "x"+ :source_unavailable true}});;; --------------------------------------------- Collection paths -------------------------------------------------diff --git a/src/metabase/metabot/db.clj b/src/metabase/metabot/db.cljindex 79d0b878fe0..57fa7c8640d 100644--- a/src/metabase/metabot/db.clj+++ b/src/metabase/metabot/db.clj@@ -507,10 +507,11 @@"Return the ID, name, schema, and database ID of each table in `table-ids`.Rows also include `:is_published` and `:collection_id`, which `can-read?` and `can-query?` need to check access- through a collection. Both keys must be present, even when nil."+ through a collection. Both keys must be present, even when nil.+ Both are user values, so the rows read through the user-settings overlay."[table-ids :- [:sequential ::lib.schema.id/table]](t2/select [:model/Table :id :name :schema :db_id :is_published :collection_id] :id [:in table-ids]- {:from [(warehouse-schema-overlay/table-query {:user-settings? false})]}))+ {:from [(warehouse-schema-overlay/table-query)]}))(mu/defn table-curation-rows"The ID, published flag, data layer, and data authority of the Tables with `table-ids`."diff --git a/src/metabase/metabot/tools/construct.clj b/src/metabase/metabot/tools/construct.cljindex 4a1bca9069d..b88e9a96d9e 100644--- a/src/metabase/metabot/tools/construct.clj+++ b/src/metabase/metabot/tools/construct.clj@@ -1001,7 +1001,8 @@diagnostics over every custom column / aggregation / filter([[repr.repair/assert-editor-accepts-expressions!]]), then reject a temporal bucketsitting on a column that is not temporal- ([[repr.repair/assert-temporal-buckets-on-temporal-columns!]]). Any of these+ ([[repr.repair/assert-temporal-buckets-on-temporal-columns!]]), and reject a metric whose+ definition can't run on the stage's source ([[incompatible-metrics]]). Any of thesefailing is a retryable `:agent-error?` - success on a query the editor rejects isBOT-1442.7. Export that final numeric MBQL 5 back to the portable form for the LLM-facingdiff --git a/test/metabase/mcp/v2/tools/search_test.clj b/test/metabase/mcp/v2/tools/search_test.cljindex 9bbd3ca69f3..9c1008797a5 100644--- a/test/metabase/mcp/v2/tools/search_test.clj+++ b/test/metabase/mcp/v2/tools/search_test.clj@@ -5,6 +5,8 @@[metabase.activity-feed.core :as activity-feed][metabase.activity-feed.models.recent-views :as recent-views][metabase.collections.test-utils :as collections.tu]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata][metabase.mcp.v2.message :as message][metabase.mcp.v2.registry :as registry][metabase.mcp.v2.test-util :as v2.tu]@@ -14,11 +16,14 @@[metabase.search.ingestion :as search.ingestion][metabase.search.test-util :as search.tu][metabase.test :as mt]+ [metabase.test.fixtures :as fixtures][metabase.util.json :as json][toucan2.core :as t2]))(set! *warn-on-reflection* true)+(use-fixtures :once (fixtures/initialize :db :test-users))+(def ^:private add-collection-paths#'tools.search/add-collection-paths)@@ -514,6 +519,32 @@(is (= [inside-id outside-id](->> content :content first :text v2.tu/strip-data-boundary json/decode+kw :data (map :id)))))))))))+(deftest metric-source-card-is-a-numeric-id-test+ (testing "a metric defined on a model names the model by its numeric id, which `fields` can pick"+ (binding [search.ingestion/*force-sync* true]+ (search.tu/with-appdb-search-if-available-otherwise-legacy+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {model-id :id} {:type :model+ :name "mcpcardmetric model"+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id :venues)))}+ :model/Card {metric-id :id} {:type :metric+ :name "mcpcardmetric count"+ :dataset_query (-> (lib/query mp (lib.metadata/card mp model-id))+ (lib/aggregate (lib/count)))}]+ (mt/with-current-user (mt/user->id :crowberto)+ (let [search (fn [args]+ (->> (tools.search/search-tool (merge {:term_queries ["mcpcardmetric"] :type ["metric"]} args)+ {:token-scopes #{"agent:content:read"}})+ :content first :text v2.tu/strip-data-boundary json/decode+kw :data+ (filter #(= metric-id (:id %)))))]+ (testing "in the detailed row, without the portable entity id the v2 tools don't accept"+ (let [row (first (search {:response_format "detailed"}))]+ (is (=? {:source_card_id model-id, :source_card_name "mcpcardmetric model"} row))+ (is (not (contains? row :source_card_portable_entity_id)))))+ (testing "and as a picked field"+ (is (= [{:id metric-id, :source_card_id model-id}]+ (search {:fields ["id" "source_card_id"]}))))))))))))+(deftest collection-row-path-omits-unreadable-ancestors-test(testing "GHY-4137: a collection row builds its path from its own :location — that path mustalso omit unreadable ancestors"diff --git a/test/metabase/metabot/db_test.clj b/test/metabase/metabot/db_test.cljindex 5ab7061eb43..3a48bfcb007 100644--- a/test/metabase/metabot/db_test.clj+++ b/test/metabase/metabot/db_test.clj@@ -1,6 +1,7 @@(ns metabase.metabot.db-test(:require[clojure.test :refer :all]+ [metabase.collections.test-utils :as collections.tu][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.metabot.db :as metabot.db]@@ -10,8 +11,11 @@[metabase.permissions.models.permissions :as perms][metabase.permissions.models.permissions-group :as perms-group][metabase.test :as mt]+ [metabase.test.fixtures :as fixtures][toucan2.core :as t2]))+(use-fixtures :once (fixtures/initialize :db))+(deftest table-schema-rows-are-permission-checkable-test(testing "Narrowed table rows preserve query and read permission checks"(let [table-id (mt/id :orders)@@ -28,6 +32,16 @@(is (= (mi/can-read? :model/Table table-id) (mi/can-read? row))(str "can-read? disagrees for " user))))))))+(deftest table-schema-rows-read-published-state-test+ (testing "Rows carry a table's published state, which is stored with the user's table settings"+ (collections.tu/with-library [{data :data}]+ (mt/with-temp [:model/Table {table-id :id} {:db_id (mt/id), :name "metabot published", :active true}+ :model/TableUserSettings _ {:table_id table-id+ :is_published true+ :collection_id (:id data)}]+ (is (=? [{:id table-id, :is_published true, :collection_id (:id data)}]+ (metabot.db/table-schema-rows [table-id])))))))+(deftest card-source-info-classifies-legacy-metrics-test(testing "Reading stored metric sources preserves stage structure without upgrading curated dimensions"(let [mp (mt/metadata-provider)]