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
Revert "database mcp tool" (#72664)
Alexander Polyankinea831ed8d56b
diff --git a/src/metabase/agent_api/api.clj b/src/metabase/agent_api/api.cljindex 214869705cd..5a2251ad331 100644--- a/src/metabase/agent_api/api.clj+++ b/src/metabase/agent_api/api.clj@@ -130,7 +130,7 @@[:database_engine :string][:database_schema {:optional true} [:maybe :string]][:description {:optional true} [:maybe :string]]- [:fields {:optional true} [:maybe [:sequential ::field]]]+ [:fields [:sequential ::field]][:related_tables {:optional true} [:maybe [:sequential ::related-table]]][:metrics {:optional true} [:maybe [:sequential ::metric-summary]]][:measures {:optional true} [:maybe [:sequential ::measure]]]@@ -194,14 +194,6 @@[:data [:sequential ::search-result-item]][:total_count :int]])-(mr/def ::database- "Details of a database, optionally including its tables."- [:map {:encode/api #(update-keys % metabot.u/safe->snake_case_en)}- [:id :int]- [:name :string]- [:engine :string]- [:tables {:optional true} [:maybe [:sequential ::table]]]])-;;; --------------------------------------------------- Endpoints ----------------------------------------------------(api.macros/defendpoint :get "/v1/ping" :- [:map [:message :string]]@@ -210,29 +202,6 @@[]{:message "pong"})-(api.macros/defendpoint :get "/v1/database" :- [:sequential ::database]- "List all databases the current user has access to."- {:scope metabot/agent-database-read- :tool {:name "list_databases"}}- []- (check-tool-result (entity-details/list-databases)))--(api.macros/defendpoint :get "/v1/database/:id" :- ::database- "Get details for a database by ID."- {:scope metabot/agent-database-read- :tool {:name "get_database"}}- [{:keys [id]} :- [:map [:id ms/PositiveInt]]- {:keys [with-tables with-fields]- :or {with-tables false, with-fields false}}- :- [:map- [:with-tables {:optional true} [:maybe :boolean]]- [:with-fields {:optional true} [:maybe :boolean]]]]- (check-tool-result- (entity-details/get-database-details- {:database-id id- :with-tables? with-tables- :with-fields? with-fields})))-(api.macros/defendpoint :get "/v1/table/:id" :- ::table"Get details for a table by ID."{:scope metabot/agent-table-readdiff --git a/src/metabase/agent_api/reference.md b/src/metabase/agent_api/reference.mdindex ed1400da240..cbb7ee5460f 100644--- a/src/metabase/agent_api/reference.md+++ b/src/metabase/agent_api/reference.md@@ -9,8 +9,6 @@ Base path: /api/agent## Key concepts-- **Databases**: Data sources connected to Metabase. Use /v1/database to list- them, and /v1/database/{id} to inspect a database's tables.- **Tables**: Database tables visible to the user.- **Metrics**: Standalone saved queries that represent pre-defined aggregations(e.g., "Total Revenue"). Metrics are stored in collections and can be used@@ -105,73 +103,6 @@ Health check.Response: `{"message": "pong"}`-### GET /v1/database--List all databases the current user has access to.--Response:--```json-[- {- "id": 1,- "name": "Production DB",- "engine": "postgres"- }-]-```--### GET /v1/database/{id}--Get details for a database by ID, optionally including its tables.--Query parameters (all boolean, all optional):--| Parameter | Default | Description |-|---------------------|---------|-----------------------------------------|-| with-tables | false | Include tables in this database |-| with-fields | false | Include field metadata on each table |-| with-field-values | false | Include sample values on each field |-| with-related-tables | false | Include FK-related tables |-| with-metrics | false | Include metrics defined on each table |-| with-measures | false | Include measures on each table |-| with-segments | false | Include segments defined on each table |--Response (without tables):--```json-{- "id": 1,- "name": "Production DB",- "engine": "postgres"-}-```--Response (with-tables=true):--```json-{- "id": 1,- "name": "Production DB",- "engine": "postgres",- "tables": [- {- "type": "table",- "id": 42,- "name": "ORDERS",- "display_name": "Orders",- "database_id": 1,- "database_engine": "postgres",- "database_schema": "PUBLIC",- "fields": []- }- ]-}-```--When with-fields, with-metrics, etc. are enabled, each table in the response-follows the same shape as GET /v1/table/{id} with the corresponding options.-### GET /v1/table/{id}Get details for a table including fields, related tables, metrics, anddiff --git a/src/metabase/api/macros/defendpoint/tools_manifest.clj b/src/metabase/api/macros/defendpoint/tools_manifest.cljindex 80fbbf97cb9..30dfa71f31a 100644--- a/src/metabase/api/macros/defendpoint/tools_manifest.clj+++ b/src/metabase/api/macros/defendpoint/tools_manifest.clj@@ -249,10 +249,8 @@(cond-> {:name tool-name:description description:endpoint {:method (u/upper-case-en (name method))- :path full-path}- ;; MCP requires every tool to have an inputSchema object, so default to an empty- ;; object for no-arg endpoints- :inputSchema (or input-schema {:type "object" :properties {}})}+ :path full-path}}+ input-schema (assoc :inputSchema input-schema)resp-schema (assoc :responseSchema resp-schema)(seq annotations) (assoc :annotations annotations)task-support (assoc :execution {:taskSupport (name task-support)})diff --git a/src/metabase/metabot/core.clj b/src/metabase/metabot/core.cljindex dca34338a07..e62f87a80c1 100644--- a/src/metabase/metabot/core.clj+++ b/src/metabase/metabot/core.clj@@ -11,7 +11,6 @@metabase-provider?provider-and-model->provider][metabase.metabot.scope- agent-database-readagent-metric-readagent-query-constructagent-query-executediff --git a/src/metabase/metabot/scope.clj b/src/metabase/metabot/scope.cljindex 4365dff3ae3..74f256ca2f8 100644--- a/src/metabase/metabot/scope.clj+++ b/src/metabase/metabot/scope.clj@@ -96,10 +96,6 @@(api-scope/defscope agent-metric-read "agent:metric:read"(deferred-tru "View metric definitions"))-;; Database-(api-scope/defscope agent-database-read "agent:database:read"- (deferred-tru "View database metadata"))-;;; ──────────────────────────────────────────────────────────────────;;; Metabot permission type definitions;;; ──────────────────────────────────────────────────────────────────@@ -147,7 +143,7 @@"Map from metabot permission type to the wildcard scope strings granted whenthat permission is `:yes`."{:permission/metabot-sql-generation #{"agent:sql:*" "agent:transforms:*" "agent:snippets:*"}- :permission/metabot-nlq #{"agent:notebook:*" "agent:query:*" "agent:table:*" "agent:metric:*" "agent:database:*"}+ :permission/metabot-nlq #{"agent:notebook:*" "agent:query:*" "agent:table:*" "agent:metric:*"}:permission/metabot-other-tools #{"agent:viz:*" "agent:dashboard:*" "agent:document:*" "agent:alert:*"}})(def always-granted-scopesdiff --git a/src/metabase/metabot/tools/entity_details.clj b/src/metabase/metabot/tools/entity_details.cljindex 8db9b64c6f8..a5247aec92e 100644--- a/src/metabase/metabot/tools/entity_details.clj+++ b/src/metabase/metabot/tools/entity_details.clj@@ -192,7 +192,7 @@db-id (if metadata-provider (:db-id base) (:db_id base))db-engine (some-> (if metadata-provider(lib.metadata/database metadata-provider)- (metabot.tools.u/get-database db-id))+ (metabot.tools.u/get-database db-id :engine)):engine name)mp (when query-needed?(or metadata-provider@@ -207,9 +207,18 @@(metabot.tools.u/table-field-id-prefix id))related-tables (when with-related-tables?(related-tables table-query field-id-prefix with-fields? field-values-fn))]- (-> (metabot.tools.u/->result-table base db-engine)- (assoc :fields (into [] (map-indexed #(metabot.tools.u/->result-column table-query %2 %1 field-id-prefix)) cols))- (m/assoc-some :related_tables related-tables+ (-> {:id id+ :type :table+ :fields (into [] (map-indexed #(metabot.tools.u/->result-column table-query %2 %1 field-id-prefix)) cols)+ :name (:name base)+ ;; :display_name should be (lib/display-name table-query), but we want to avoid creating the query if possible+ :display_name (some->> (:name base)+ (u.humanization/name->human-readable-name :simple))+ :database_id db-id+ :database_engine db-engine+ :database_schema (:schema base)}+ (m/assoc-some :description (:description base)+ :related_tables related-tables:metrics (when with-metrics?(not-empty (mapv #(convert-metric % mp options)(lib/available-metrics table-query))))@@ -385,52 +394,6 @@{:output (ex-message e) :status-code 404}(metabot.tools.u/handle-agent-error e)))))-(defn list-databases- "Get a list of all databases the current user can read without including its tables."- []- (try- (let [dbs (metabot.tools.u/list-databases)]- {:structured-output (mapv metabot.tools.u/->result-database dbs)})- (catch Exception e- (metabot.tools.u/handle-agent-error e))))--(defn- database-table-details- "Build a table result map for `get-database-details`, optionally including its fields."- [table engine table-id->fields with-fields?]- (let [id (:id table)- prefix (metabot.tools.u/table-field-id-prefix id)- cols (->> (get table-id->fields id)- (sort-by (juxt (fn [f] (or (:position f) 0)) :id)))]- (cond-> (metabot.tools.u/->result-table table engine)- with-fields?- (assoc :fields- (into []- (map-indexed- (fn [i field]- (metabot.tools.u/->result-column field i prefix)))- cols)))))--(defn get-database-details- "Get details for a database by ID, optionally including its tables and fields.- Uses raw t2 queries so it stays efficient on databases with many tables and fields."- [{:keys [database-id with-tables? with-fields?]- :or {with-tables? false, with-fields? false}}]- (try- (let [db (metabot.tools.u/get-database database-id)- engine (some-> (:engine db) name)- tables (when with-tables?- (metabot.tools.u/list-database-tables database-id))- table-id->fields (when with-fields?- (group-by :table_id (metabot.tools.u/list-database-table-fields database-id)))]- {:structured-output- (cond-> (metabot.tools.u/->result-database db)- with-tables?- (assoc :tables (mapv #(database-table-details % engine table-id->fields with-fields?) tables)))})- (catch Exception e- (if (= (:status-code (ex-data e)) 404)- {:output (ex-message e) :status-code 404}- (metabot.tools.u/handle-agent-error e)))))-(defn get-metric-details"Get information about the metric with ID `metric-id`."[{:keys [metric-id] :as arguments}]diff --git a/src/metabase/metabot/tools/util.clj b/src/metabase/metabot/tools/util.cljindex 2ef3ef39c46..2492b3a7e26 100644--- a/src/metabase/metabot/tools/util.clj+++ b/src/metabase/metabot/tools/util.clj@@ -8,10 +8,8 @@[metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata]- [metabase.models.interface :as mi][metabase.premium-features.core :as premium-features][metabase.util :as u]- [metabase.util.humanization :as u.humanization][metabase.util.log :as log][toucan2.core :as t2]))@@ -54,60 +52,27 @@"A prefix pattern accepting columns from any entity."#"^.*-(\d+)")-(defn ->result-database- "Build the agent result map for a database."- [db]- {:id (:id db)- :name (:name db)- :engine (some-> (:engine db) name)})--(defn ->result-table- "Build the base agent result map for a table (without `:fields`).- Accepts either a raw t2 row (underscore keys) or a lib-shaped table (hyphenated keys)."- [table db-engine]- (let [table (u/kebab->snake-keys table)]- (cond-> {:id (:id table)- :type :table- :name (:name table)- :display_name (or (:display_name table)- (some->> (:name table) (u.humanization/name->human-readable-name :simple)))- :database_id (:db_id table)- :database_engine db-engine- :database_schema (:schema table)}- (:description table) (assoc :description (:description table)))))-(defn ->result-column- "Return a tool result column for `column`. Position is determined by `index`; the field ID is- `field-id-prefix` + `index`.-- Accepts either a raw t2 row (underscore keys) or a lib-shaped column (hyphenated keys).-- The 3-arity takes the display name directly from the column; the 4-arity computes it via- `(lib/display-name query column)`."- ([column index field-id-prefix]- (let [column (u/kebab->snake-keys column)- base-type (some-> (:base_type column) u/qualified-name)- effective-type (some-> (:effective_type column) u/qualified-name)- semantic-type (some-> (:semantic_type column) u/qualified-name)- coercion-strategy (some-> (:coercion_strategy column) u/qualified-name)]- (-> {:field_id (str field-id-prefix index)- ;; Prefer desired-column-alias over source-column-alias to ensure we use deduplicated names if needed- :name (or (:lib/desired_column_alias column)- (:lib/source_column_alias column)- (:name column))- :display_name (:display_name column)}- (m/assoc-some :description (:description column)- :base_type base-type- :effective_type (when (not= effective-type base-type) effective-type)- :semantic_type semantic-type- :database_type (:database_type column)- :coercion_strategy coercion-strategy- :field_values (:field_values column)- :table_reference (:table_reference column)))))- ([query column index field-id-prefix]- (->result-column (assoc column :display-name (lib/display-name query column))- index- field-id-prefix)))+ "Return tool result columns for `column` of `query`. The position of `column` is determined by `index`.+ The ID is then generated by prefixing the position with `field-id-prefix`."+ [query column index field-id-prefix]+ (let [base-type (some-> (:base-type column) u/qualified-name)+ effective-type (some-> (:effective-type column) u/qualified-name)+ semantic-type (some-> (:semantic-type column) u/qualified-name)+ coercion-strategy (some-> (:coercion-strategy column) u/qualified-name)]+ (-> {:field_id (str field-id-prefix index)+ ;; Prefer desired-column-alias over source-column-alias to ensure we use deduplicated names if needed+ :name (or (:lib/desired-column-alias column)+ (:lib/source-column-alias column))+ :display_name (lib/display-name query column)}+ (m/assoc-some :description (:description column)+ :base_type base-type+ :effective_type (when (not= effective-type base-type) effective-type)+ :semantic_type semantic-type+ :database_type (:database-type column)+ :coercion_strategy coercion-strategy+ :field_values (:field-values column)+ :table_reference (:table-reference column)))))(defn parse-field-id"Parse a field-id string into its components.@@ -195,58 +160,12 @@second)):schedule_frame (some->> day-of-month name (re-find #"^(?:first|mid|last)"))})-(defn- database-filter- "HoneySQL WHERE clause matching tables (alias `:t`) belonging to the database with `database-id`."- [database-id]- [:= :t.db_id database-id])--(defn- table-filter- "HoneySQL WHERE clause keeping only user-visible tables (alias `:t`): active and not hidden,- technical, or cruft."- []- [:and [:= :t.active true]- [:or- [:= :t.visibility_type nil]- [:not-in :t.visibility_type [:inline ["hidden" "technical" "cruft"]]]]])--(defn- field-filter- "HoneySQL WHERE clause keeping only user-visible fields (alias `:f`): active and not retired- or sensitive."- []- [:and [:= :f.active true]- [:or- [:= :f.visibility_type nil]- [:not-in :f.visibility_type [:inline ["retired" "sensitive"]]]]])--(defn list-databases- "Get all databases the current user can read."- []- (->> (t2/select [:model/Database :id :name :engine])- (filterv mi/can-read?)))-(defn get-database- "Get the database with ID `database-id`."- [database-id]- (-> (t2/select-one [:model/Database :id :name :engine] :id database-id)+ "Get the `fields` of the database with ID `id`."+ [id & fields]+ (-> (t2/select-one (into [:model/Database :id] fields) id)api/read-check))-(defn list-database-tables- "Get all active tables in the database with ID `database-id`."- [database-id]- (->> (t2/select [:model/Table :t.id :t.db_id :t.name :t.schema :t.display_name :t.description]- {:from [[(t2/table-name :model/Table) :t]]- :where [:and (database-filter database-id) (table-filter)]})- (filterv mi/can-read?)))--(defn list-database-table-fields- "Get all active fields for active tables in the database with ID `database-id`."- [database-id]- (t2/select [:model/Field :f.id :f.table_id :f.position :f.name :f.display_name :f.description- :f.base_type :f.effective_type :f.semantic_type :f.coercion_strategy :f.database_type]- {:from [[(t2/table-name :model/Field) :f]]- :join [[(t2/table-name :model/Table) :t] [:= :t.id :f.table_id]]- :where [:and (database-filter database-id) (table-filter) (field-filter)]}))-(defn get-table"Get the `fields` of the table with ID `id`."[id & fields]diff --git a/test/metabase/agent_api/api_test.clj b/test/metabase/agent_api/api_test.cljindex 81ef3e1895d..d44300594c1 100644--- a/test/metabase/agent_api/api_test.clj+++ b/test/metabase/agent_api/api_test.clj@@ -122,95 +122,33 @@(deftest get-table-details-field-types-test(testing "Field metadata (base_type, effective_type, semantic_type, coercion_strategy) is returned correctly"- (testing "base_type is always set, semantic_type is returned when set"- (mt/with-temp [:model/Database {db-id :id} {}- :model/Table {table-id :id} {:db_id db-id, :name "t", :active true}- :model/Field _ {:table_id table-id, :name "id"- :base_type :type/BigInteger- :semantic_type :type/PK}]- (is (=? {:fields [{:name "id"- :base_type "type/BigInteger"- :semantic_type "type/PK"}]}- (mt/user-http-request :rasta :get 200 (str "agent/v1/table/" table-id))))))-- (testing "semantic_type is omitted when not set, effective_type is omitted when it equals base_type"- (mt/with-temp [:model/Database {db-id :id} {}- :model/Table {table-id :id} {:db_id db-id, :name "t", :active true}- :model/Field _ {:table_id table-id, :name "name"- :base_type :type/Text}]- (let [[field :as fields] (:fields (mt/user-http-request :rasta :get 200 (str "agent/v1/table/" table-id)))]- (is (= 1 (count fields)))- (is (=? {:name "name"- :base_type "type/Text"}- field))- (is (not (contains? field :semantic_type)))- (is (not (contains? field :effective_type))))))-- (testing "effective_type and coercion_strategy are returned when coerced"- (mt/with-temp [:model/Database {db-id :id} {}- :model/Table {table-id :id} {:db_id db-id, :name "t", :active true}- :model/Field _ {:table_id table-id, :name "created_at"- :base_type :type/Text- :effective_type :type/DateTime- :coercion_strategy :Coercion/ISO8601->DateTime}]- (is (=? {:fields [{:name "created_at"- :base_type "type/Text"- :effective_type "type/DateTime"- :coercion_strategy "Coercion/ISO8601->DateTime"}]}- (mt/user-http-request :rasta :get 200 (str "agent/v1/table/" table-id))))))))--(deftest list-databases-test- (testing "Returns a list of databases"- ;; Reference a table to ensure the test database exists- (let [_ (mt/id :orders)]- (is (=? [{:id (mt/id)- :name "test-data (h2)"- :engine "h2"}]- (mt/user-http-request :crowberto :get 200 "agent/v1/database")))))-- (testing "User without data permissions gets empty list"- (mt/with-no-data-perms-for-all-users!- (is (= [] (mt/user-http-request :rasta :get 200 "agent/v1/database"))))))--(deftest get-database-details-test- (testing "Returns database details"- (is (=? {:id (mt/id)- :name "test-data (h2)"- :engine "h2"}- (mt/user-http-request :rasta :get 200 (str "agent/v1/database/" (mt/id))))))-- (testing "Returns 404 for non-existent database"- (is (= "Not found."- (mt/user-http-request :rasta :get 404 "agent/v1/database/999999"))))-- (testing "Tables are included when requested"- (let [response (mt/user-http-request :rasta :get 200- (str "agent/v1/database/" (mt/id) "?with-tables=true"))- orders (m/find-first #(= (mt/id :orders) (:id %)) (:tables response))]- (is (=? {:id (mt/id :orders)- :type "table"- :name "ORDERS"- :display_name "Orders"- :database_id (mt/id)}- orders))))-- (testing "Tables include fields when requested"- (let [response (mt/user-http-request :rasta :get 200- (str "agent/v1/database/" (mt/id) "?with-tables=true&with-fields=true"))- orders (m/find-first #(= (mt/id :orders) (:id %)) (:tables response))- total (m/find-first #(= (str (metabot.tools.u/table-field-id-prefix (mt/id :orders)) 5)- (:field_id %))- (:fields orders))]- (is (=? {:field_id (str (metabot.tools.u/table-field-id-prefix (mt/id :orders)) 5)- :name "TOTAL"- :display_name "Total"- :base_type "type/Float"- :database_type "DOUBLE PRECISION"}- total))))-- (testing "User without data permissions gets 403"- (mt/with-no-data-perms-for-all-users!- (mt/user-http-request :rasta :get 403 (str "agent/v1/database/" (mt/id))))))+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {table-id :id} {:db_id db-id, :name "t", :active true}+ :model/Field _ {:table_id table-id, :name "id"+ :base_type :type/BigInteger+ :semantic_type :type/PK}+ :model/Field _ {:table_id table-id, :name "name"+ :base_type :type/Text}+ :model/Field _ {:table_id table-id, :name "created_at"+ :base_type :type/Text+ :effective_type :type/DateTime+ :coercion_strategy :Coercion/ISO8601->DateTime}]+ (let [fields (-> (mt/user-http-request :rasta :get 200 (str "agent/v1/table/" table-id))+ :fields)+ by-name (m/index-by :name fields)]+ (testing "base_type is always set"+ (is (= "type/BigInteger" (get-in by-name ["id" :base_type])))+ (is (= "type/Text" (get-in by-name ["name" :base_type])))+ (is (= "type/Text" (get-in by-name ["created_at" :base_type]))))+ (testing "semantic_type is returned when set"+ (is (= "type/PK" (get-in by-name ["id" :semantic_type])))+ (is (nil? (get-in by-name ["name" :semantic_type]))))+ (testing "effective_type and coercion_strategy are returned when coerced"+ (is (= "type/DateTime" (get-in by-name ["created_at" :effective_type])))+ (is (= "Coercion/ISO8601->DateTime" (get-in by-name ["created_at" :coercion_strategy]))))+ (testing "effective_type is omitted when it equals base_type"+ (is (not (contains? (get by-name "id") :effective_type)))+ (is (not (contains? (get by-name "name") :effective_type))))))))(deftest get-metric-details-test(mt/with-temp [:model/Card metric {:name "Test Metric"diff --git a/test/metabase/mcp/api_test.clj b/test/metabase/mcp/api_test.cljindex d214d124f8a..fb5cbd9b775 100644--- a/test/metabase/mcp/api_test.clj+++ b/test/metabase/mcp/api_test.clj@@ -168,16 +168,15 @@(is (= 200 (:status post-response))))))(deftest tools-list-test- (testing "tools/list returns the 10 agent tools"+ (testing "tools/list returns the 8 agent tools"(let [[session-id _] (initialize!)response (mcp-request (jsonrpc-request "tools/list"){"mcp-session-id" session-id})tools (get-in response [:body :result :tools])](is (= 200 (:status response)))(is (pos? (count tools)))- (is (= #{"search" "list_databases" "get_database" "get_table" "get_metric"- "get_table_field_values" "get_metric_field_values"- "construct_query" "execute_query" "query"}+ (is (= #{"search" "get_table" "get_metric" "get_table_field_values"+ "get_metric_field_values" "construct_query" "execute_query" "query"}(set (map :name tools))))(testing "each tool has a description and inputSchema"(doseq [tool tools]@@ -444,7 +443,7 @@(deftest tools-list-scope-filtering-test(testing "tools/list with unrestricted scopes returns all tools"(let [tools (mcp.tools/list-tools #{::scope/unrestricted})]- (is (= 10 (count tools)))))+ (is (= 8 (count tools)))))(testing "tools/list with specific scope only returns matching tools"(let [tools (mcp.tools/list-tools #{"agent:search"})@@ -457,11 +456,11 @@(testing "tools/list with wildcard scope matches all agent tools"(let [tools (mcp.tools/list-tools #{"agent:*"})]- (is (= 10 (count tools)))))+ (is (= 8 (count tools)))))(testing "tools/list with nil scopes returns all tools"(let [tools (mcp.tools/list-tools nil)]- (is (= 10 (count tools)))))+ (is (= 8 (count tools)))))(testing "tools/list with empty scopes does not return all tools"(let [tools (mcp.tools/list-tools #{})]