Skip to content

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

GHY-4940: Ensure no REST scopes are accepted by MCP tools (#84339)

Eric Normandb7955d0167f6
diff --git a/test/metabase/mcp/v2/api_test.clj b/test/metabase/mcp/v2/api_test.clj
index 2b94833cc19..e4b07965c0e 100644
--- a/test/metabase/mcp/v2/api_test.clj
+++ b/test/metabase/mcp/v2/api_test.clj
@@ -3,6 +3,7 @@
[clojure.string :as str]
[clojure.test :refer :all]
[metabase.ai-tracing.core :as ait]
+ [metabase.api-scope.core :as api-scope]
[metabase.api.macros :as api.macros]
[metabase.auth-identity.core :as auth-identity]
[metabase.config.core :as config]
@@ -1258,6 +1259,51 @@
(is (every? #(= #{:jsonrpc :id} (disj (set (keys %)) :error :result)) (:body response))
"no transport-internal marker leaks into a batch element")))))))))))
+(deftest rest-scopes-reach-no-mcp-tool-or-resource-test
+ (testing "GHY-4940: REST scopes gate REST endpoints only. A token holding every scope that is not an MCP scope (every
+ registered scope and every scope a REST endpoint declares) is refused by every MCP tool and every MCP data
+ resource. UI shells carry no data and are read by any token, so they are not checked."
+ (require 'metabase.api-routes.routes)
+ (when config/ee-available?
+ (require 'metabase-enterprise.api-routes.routes))
+ (let [endpoint-scopes (for [nmspace (all-ns)
+ [_ {:keys [form]}] (api.macros/ns-routes nmspace)
+ :let [scope (get-in form [:metadata :scope])]
+ :when (string? scope)]
+ scope)
+ ;; mb:full is left out: the session middleware maps it to the unrestricted sentinel, which MCP accepts.
+ non-mcp-scopes (into (sorted-set)
+ (remove (conj (set mcp.paths/v2-surface-scopes) "mb:full"))
+ (concat endpoint-scopes (map :scope (api-scope/all-scopes))))]
+ (is (seq non-mcp-scopes))
+ (do-with-bearer-token!
+ non-mcp-scopes
+ (fn [headers]
+ (let [session-id (-> (client/client-full-response :post 200 endpoint
+ {:request-options {:headers headers}}
+ (jsonrpc-request "initialize" mcp-app-ui-capabilities))
+ (get-in [:headers "Mcp-Session-Id"]))
+ post! (fn [body]
+ (client/client-full-response :post endpoint
+ {:request-options {:headers (assoc headers "mcp-session-id"
+ session-id)}}
+ body))
+ tools (get-in (post! (jsonrpc-request "tools/list")) [:body :result :tools])
+ resources (get-in (post! (jsonrpc-request "resources/list")) [:body :result :resources])]
+ (is (seq tools))
+ (doseq [{tool-name :name} tools]
+ (testing (str "tools/call " tool-name)
+ (let [response (post! (jsonrpc-request "tools/call" {:name tool-name :arguments {}}))]
+ (is (= 403 (:status response)))
+ (is (str/includes? (get-in response [:headers "WWW-Authenticate"] "") "error=\"insufficient_scope\"")))))
+ (doseq [{:keys [uri mimeType]} resources
+ :when (not= "text/html;profile=mcp-app" mimeType)]
+ (testing (str "resources/read " uri)
+ (let [response (post! (jsonrpc-request "resources/read" {:uri uri}))]
+ (is (= 403 (:status response)))
+ (is (str/includes? (get-in response [:headers "WWW-Authenticate"] "")
+ "error=\"insufficient_scope\"")))))))))))
+
(deftest baseline-token-reads-the-fields-catalog-test
(testing "GHY-4543, GHY-4939: the locked baseline carries agent:content:read, so a client that unticked everything
else still reads the fields catalog without stepping up"