Skip to content

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

Cleanup scopes BOT-2127 (#84458)

Eric Normandfa5b1f3a6099
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 5fda7652b00..c60f247ebe5 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -1441,6 +1441,7 @@
events
lib.schema
mcp
+ metabot.agent-api
models
request
server
diff --git a/src/metabase/agent_api/api.clj b/src/metabase/agent_api/api.clj
index 85eb14499b9..64c4ca50a83 100644
--- a/src/metabase/agent_api/api.clj
+++ b/src/metabase/agent_api/api.clj
@@ -1832,3 +1832,11 @@
(respond response))
raise))))
(fn [prefix] (open-api/open-api-spec base-routes prefix))))
+
+(defn defendpoint-scopes
+ "All defendpoint scopes from agent-api as a sorted set."
+ []
+ (into (sorted-set)
+ (comp (keep #(get-in % [:form :metadata :scope]))
+ (filter string?))
+ (vals (api.macros/ns-routes 'metabase.agent-api.api))))
diff --git a/src/metabase/mcp/core.clj b/src/metabase/mcp/core.clj
index 9ccf20aa629..64266dba683 100644
--- a/src/metabase/mcp/core.clj
+++ b/src/metabase/mcp/core.clj
@@ -3,7 +3,6 @@
rather than reaching into internal namespaces like [[metabase.mcp.settings]]."
(:require
[clojure.string :as str]
- [metabase.api.macros :as api.macros]
[metabase.mcp.paths :as mcp.paths]
[metabase.mcp.session :as mcp.session]
[metabase.mcp.settings :as mcp.settings]
@@ -78,32 +77,13 @@
"vscode-webview://" (vscode-webview-enabled?)
false)))
-(defn all-scopes
- "All supported OAuth scopes, as a sorted set: those declared on agent-api endpoints via defendpoint metadata, plus
- every scope the v2 MCP surface accepts ([[v2-scopes]])."
- []
- (-> (sorted-set)
- ;; agent-api scopes from defendpoint metadata
- (into (comp (keep #(get-in % [:form :metadata :scope]))
- (filter string?))
- (vals (api.macros/ns-routes 'metabase.agent-api.api)))
- ;; The v2 surface's scopes, read from the require-free leaf rather than the registry (see [[v2-scopes]]).
- (into mcp.paths/v2-surface-scopes)))
-
(defn v2-scopes
- "The scopes the v2 MCP surface itself gates on. Excludes the agent-API endpoint scopes
- [[all-scopes]] also gathers — those belong to a different resource, and advertising them for v2 is
- what puts per-entity scopes the v2 tools don't use on a v2 client's consent screen.
-
- Read from the require-free literal in [[metabase.mcp.paths/v2-surface-scopes]], not the registries.
- The registries report only the tools whose namespaces happen to be loaded.
- Reaching them from here would also pull `metabot.scope` in wherever this namespace loads (see [[metabase.mcp.paths]]).
- `v2-surface-scopes-match-metabot-scope-test` keeps the literal in step with what the tools gate on."
+ "The scopes the v2 MCP surface itself gates on."
[]
mcp.paths/v2-surface-scopes)
(defn v2-baseline-scopes
- "The subset of [[v2-scopes]] that the consent page locks: when a client requests one of these, the user cannot
- untick it."
+ "The subset of [[v2-scopes]] that the consent page locks: when a client requests one of these, the user cannot untick
+ it."
[]
mcp.paths/v2-baseline-scopes)
diff --git a/src/metabase/oauth_server/core.clj b/src/metabase/oauth_server/core.clj
index 116689029eb..2d64138774e 100644
--- a/src/metabase/oauth_server/core.clj
+++ b/src/metabase/oauth_server/core.clj
@@ -2,6 +2,7 @@
(:require
[clojure.string :as str]
[java-time.api :as t]
+ [metabase.agent-api.api :as agent-api]
[metabase.api-scope.core :as api-scope]
[metabase.mcp.core :as mcp]
[metabase.oauth-server.db :as oauth-server.db]
@@ -29,14 +30,13 @@
(defonce ^:private provider (atom nil))
(defn supported-scopes
- "All OAuth scopes intended to be advertised in the server's discovery metadata (`scopes-supported`).
-
- `mb:full` is deliberately absent. Advertising it here puts a full-access grant in front of every client that reads
- discovery metadata, so keeping it out means no client is led toward it. Note this is not a gate: dynamic
- registration is unauthenticated and accepts any registered scope, so a client that names `mb:full` itself still
- registers with it. Keeping it off this list narrows who finds it, not who may ask."
+ "All OAuth scopes intended to be advertised in the server's discovery metadata (`scopes-supported`)."
[]
- (vec (into (sorted-set) (mcp/all-scopes))))
+ (-> (sorted-set)
+ (into (mcp/v2-scopes))
+ (into (agent-api/defendpoint-scopes))
+ (disj "mb:full") ;; mb:full should not appear so clients don't find it; they can still register with it (e.g., CLI)
+ vec))
(defn all-scopes-registered?
"True when every scope in the space-delimited `scope` string is registered via
diff --git a/test/metabase/mcp/v2/tools/visualize_test.clj b/test/metabase/mcp/v2/tools/visualize_test.clj
index d7059d45445..bc2da2defea 100644
--- a/test/metabase/mcp/v2/tools/visualize_test.clj
+++ b/test/metabase/mcp/v2/tools/visualize_test.clj
@@ -10,9 +10,6 @@
(:require
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
- ;; `mcp.core/all-scopes` reads the agent-api's defendpoint metadata, so the namespace has to be
- ;; loaded for `resource-scopes-are-advertised-test` to see it (nothing else here pulls it in).
- [metabase.agent-api.api]
[metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
@@ -336,13 +333,11 @@
;;; ------------------------------------------------- Resources ----------------------------------------------------
(deftest resource-scopes-are-advertised-test
- (testing "GHY-4157: every v2 resource scope is in the OAuth grant — one a client can't request is a data resource it
- could never read, or a shell that never gets a credential"
- (let [advertised (set (mcp.core/all-scopes))]
- (testing "GHY-4543: data resources count, not just UI shells"
- (is (contains? (v2.resources/resource-scopes) "agent:content:read")))
+ (testing "GHY-4157: every v2 resource scope is in the MCP scope list"
+ (is (seq (v2.resources/resource-scopes)) "No scopes in registered resources in MCP")
+ (let [accepted (set (mcp.core/v2-scopes))]
(doseq [scope (v2.resources/resource-scopes)]
- (is (contains? advertised scope) scope)))))
+ (is (contains? accepted scope) scope)))))
(deftest resources-scope-gating-test
(testing "GHY-4543: resources/list lists every resource regardless of scope"
diff --git a/test/metabase/oauth_server/core_test.clj b/test/metabase/oauth_server/core_test.clj
index 6246607bc58..2b698984e01 100644
--- a/test/metabase/oauth_server/core_test.clj
+++ b/test/metabase/oauth_server/core_test.clj
@@ -2,9 +2,6 @@
(:require
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
- ;; load-bearing: the advertised scope sets are derived from the agent-api routes and the v2 tool
- ;; registry, so both must be loaded for these assertions to see the real surface
- [metabase.agent-api.api]
[metabase.mcp.core :as mcp]
[metabase.mcp.v2.api :as v2.api]
[metabase.oauth-server.core :as oauth-server]
@@ -13,8 +10,6 @@
[oidc-provider.store :as oidc.store]
[toucan2.core :as t2]))
-(comment metabase.agent-api.api/keep-me)
-
(use-fixtures :each (fn [thunk]
(oauth-server/reset-provider!)
(thunk)
@@ -38,9 +33,7 @@
what Claude and ChatGPT both do.
Iterated over every endpoint path rather than over the sets `default-grant-scopes` is
- built from: asserting a set covers the sets it is defined as the union of cannot fail.
- The per-path arities are the ones a client actually meets, and the v1 aliases are inside
- the ceiling only incidentally today, because `all-scopes` also feeds `supported-scopes`."
+ built from: asserting a set covers the sets it is defined as the union of cannot fail."
(let [ceiling (set (oauth-server/default-grant-scopes))]
(doseq [path (mcp/mcp-endpoint-paths)]
(testing path
diff --git a/test/metabase/server/middleware/session_oauth_test.clj b/test/metabase/server/middleware/session_oauth_test.clj
index 6d54572ab1a..703c624e989 100644
--- a/test/metabase/server/middleware/session_oauth_test.clj
+++ b/test/metabase/server/middleware/session_oauth_test.clj
@@ -4,11 +4,6 @@
OAuth scopes are mapped onto `:token-scopes` for the scope-enforcement middleware."
(:require
[clojure.test :refer [deftest is testing]]
- ;; Loaded for its load-time side effects: it registers the agent API endpoints, from which the
- ;; OAuth provider derives its scopes-supported (see [[metabase.mcp.core/all-scopes]]). In a full
- ;; server boot [[metabase.api-routes.routes]] loads it; the isolated test classpath does not
- ;; mount the routes, so require it here as that route ns does.
- [metabase.agent-api.api]
[metabase.api.macros.scope :as scope]
[metabase.initialization-status.core :as init-status]
[metabase.mcp.http-handler :as mcp.http-handler]