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
Hide MCP UI tools from clients with inline charts turned off (#83644)
Sébastien8561804a1ad8
diff --git a/src/metabase/mcp/core.clj b/src/metabase/mcp/core.cljindex 66f4dc10375..d8d2c9bb577 100644--- a/src/metabase/mcp/core.clj+++ b/src/metabase/mcp/core.clj@@ -7,7 +7,8 @@[metabase.mcp.paths :as mcp.paths][metabase.mcp.session :as mcp.session][metabase.mcp.settings :as mcp.settings]- [metabase.mcp.ui-surface :as mcp.ui-surface]))+ [metabase.mcp.ui-surface :as mcp.ui-surface]+ [metabase.mcp.usage :as mcp.usage]))(set! *warn-on-reflection* true)@@ -47,6 +48,12 @@[method uri claims](mcp.ui-surface/scope-satisfied? method uri claims))+(defn inline-ui-enabled-for-client?+ "Whether the admin allows the client named `client-info-name` (from the `initialize` handshake) to render+ MCP Apps UI. See [[metabase.mcp.settings/inline-ui-enabled-for-client?]]."+ [client-info-name]+ (mcp.settings/inline-ui-enabled-for-client? (mcp.usage/detect-client client-info-name)))+(defn vscode-webview-enabled?"Returns true if vscode/cursor is enabled in common MCP apps."[]diff --git a/src/metabase/mcp/settings.clj b/src/metabase/mcp/settings.cljindex 91faf986b75..14c5515aaca 100644--- a/src/metabase/mcp/settings.clj+++ b/src/metabase/mcp/settings.clj@@ -148,3 +148,19 @@(map str/trim)(keep not-empty)(str/join " "))))++(def ^:private client-key->setting-key+ "Maps a client key from [[metabase.mcp.usage/detect-client]] to the key its toggle uses in+ [[mcp-apps-cors-enabled-clients]]. VS Code shares the Cursor toggle: both render in a vscode-webview."+ {"claude" "claude"+ "chatgpt" "chatgpt"+ "cursor-vscode" "cursor-vscode"+ "vscode" "cursor-vscode"})++(defn inline-ui-enabled-for-client?+ "Whether `client-key` may render MCP Apps UI. An iframe from a client the admin switched off fails its CORS+ preflight, so it would show a broken card. Clients without a toggle are not gated."+ [client-key]+ (if-let [setting-key (client-key->setting-key client-key)]+ (boolean (some #{setting-key} (mcp-apps-cors-enabled-clients)))+ true))diff --git a/src/metabase/mcp/transport.clj b/src/metabase/mcp/transport.cljindex d1ce3316229..8f73c24ea2f 100644--- a/src/metabase/mcp/transport.clj+++ b/src/metabase/mcp/transport.clj@@ -371,7 +371,9 @@;; Initialize: create session and return response with session header(and (not batch?) (valid-message? body) (= "initialize" (:method body)))(let [params (:params body)- supports-mcp-ui? (mcp-app-ui-capability? params)+ ;; A client the admin switched off would only get a broken card: its iframe is blocked by CORS.+ supports-mcp-ui? (and (mcp-app-ui-capability? params)+ (mcp/inline-ui-enabled-for-client? (get-in params [:clientInfo :name])))session-id (mcp.session/create! user-id {:supports-mcp-ui?supports-mcp-ui?})init-response (handle-initialize (:id body) params capabilities instructions)]diff --git a/test/metabase/mcp/settings_test.clj b/test/metabase/mcp/settings_test.cljindex 39b361b6e4d..67626b606d5 100644--- a/test/metabase/mcp/settings_test.clj+++ b/test/metabase/mcp/settings_test.clj@@ -60,3 +60,17 @@GC task on every run"(setting/set-value-of-type! :string :mcp-query-handle-ttl-hours "forever")(is (= 24 (mcp.settings/mcp-query-handle-ttl-hours)))))))++(deftest inline-ui-enabled-for-client-test+ (mt/with-temporary-setting-values [mcp.settings/mcp-apps-cors-enabled-clients ["claude"]]+ (testing "a client with its toggle on may render UI"+ (is (true? (mcp.settings/inline-ui-enabled-for-client? "claude"))))+ (testing "a client with its toggle off may not"+ (is (false? (mcp.settings/inline-ui-enabled-for-client? "chatgpt")))+ (is (false? (mcp.settings/inline-ui-enabled-for-client? "cursor-vscode"))))+ (testing "VS Code follows the Cursor toggle"+ (is (false? (mcp.settings/inline-ui-enabled-for-client? "vscode")))+ (mt/with-temporary-setting-values [mcp.settings/mcp-apps-cors-enabled-clients ["cursor-vscode"]]+ (is (true? (mcp.settings/inline-ui-enabled-for-client? "vscode")))))+ (testing "a client with no toggle is not gated"+ (is (true? (mcp.settings/inline-ui-enabled-for-client? "other"))))))diff --git a/test/metabase/mcp/v2/api_test.clj b/test/metabase/mcp/v2/api_test.cljindex fd0e36f1fee..d394aebbad0 100644--- a/test/metabase/mcp/v2/api_test.clj+++ b/test/metabase/mcp/v2/api_test.clj@@ -561,6 +561,21 @@(-> (mcp-request (jsonrpc-request "initialize" mcp-app-ui-capabilities))(get-in [:headers "Mcp-Session-Id"])))+(deftest ui-tools-hidden-from-client-switched-off-test+ (testing "EMB-2406: a client the admin switched off under \"Show inline charts\" is not offered the UI tools"+ (let [handshake (fn []+ (-> (mcp-request (jsonrpc-request "initialize"+ (assoc mcp-app-ui-capabilities+ :clientInfo {:name "ChatGPT"})))+ (get-in [:headers "Mcp-Session-Id"])))+ tool-names (fn [session-id]+ (->> (mcp-request (jsonrpc-request "tools/list") {"mcp-session-id" session-id})+ :body :result :tools (map :name) set))]+ (mt/with-temporary-setting-values [mcp.settings/mcp-apps-cors-enabled-clients []]+ (is (not (contains? (tool-names (handshake)) "visualize_query"))))+ (mt/with-temporary-setting-values [mcp.settings/mcp-apps-cors-enabled-clients ["chatgpt"]]+ (is (contains? (tool-names (handshake)) "visualize_query"))))))+(deftest tools-list-descriptions-fit-client-truncation-test(testing "GHY-4543: Claude Code (2.1.271) truncates each tool description at 2048 characters, silently droppingwhatever guidance comes after. Every description `tools/list` sends, MCP Apps tools included and the