The personal finance app for everyone (by everyone)
Ruby
Add an "mcp_only" assistant type: no in-app chat, MCP stays available (#4147)
Ellis Nieuwpoortdc8ee02c6f0e
diff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rbindex 8ade7145dc..0964d7d842 100644--- a/app/controllers/settings/hostings_controller.rb+++ b/app/controllers/settings/hostings_controller.rb@@ -332,8 +332,7 @@ class Settings::HostingsController < ApplicationControllerdef load_external_assistant_models@external_assistant_models = []@external_assistant_catalog_error = nil- effective_type = ENV["ASSISTANT_TYPE"].presence || Current.family.assistant_type- return unless effective_type == "external"+ return unless Current.family.effective_assistant_type == "external"config = Assistant::External.configreturn unless config.url.present? && config.token.present?diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex 246c91ce69..3eea1ed429 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -207,7 +207,7 @@ module ApplicationHelperenddef assistant_icon- type = ENV["ASSISTANT_TYPE"].presence || Current.family&.assistant_type.presence || "builtin"+ type = Current.family&.effective_assistant_type || "builtin"type == "external" ? "claw" : "ai"enddiff --git a/app/models/assistant.rb b/app/models/assistant.rbindex 3b00a04ad5..2f74cfe3fc 100644--- a/app/models/assistant.rb+++ b/app/models/assistant.rb@@ -93,7 +93,7 @@ module Assistantdef implementation_for(chat)raise Error, "chat is required" if chat.blank?- type = ENV["ASSISTANT_TYPE"].presence || chat.user&.family&.assistant_type.presence || "builtin"+ type = chat.user&.family&.effective_assistant_type || "builtin"REGISTRY.fetch(type) { REGISTRY["builtin"] }endenddiff --git a/app/models/chat.rb b/app/models/chat.rbindex 4a4a1379fa..7e83b65b4e 100644--- a/app/models/chat.rb+++ b/app/models/chat.rb@@ -129,6 +129,13 @@ class Chat < ApplicationRecordenddef ask_assistant(message, assistant_message: nil)+ # The assistant can be turned off while a response is still queued. Drop+ # the pending "Thinking…" bubble rather than calling a provider.+ unless user.family.assistant_enabled?+ assistant_message&.destroy+ return+ end+assistant.respond_to(message, assistant_message: assistant_message)enddiff --git a/app/models/family.rb b/app/models/family.rbindex 6d37e1bcde..7c5f9a4474 100644--- a/app/models/family.rb+++ b/app/models/family.rb@@ -34,7 +34,11 @@ class Family < ApplicationRecordMONIKERS = [ "Family", "Group" ].freeze- ASSISTANT_TYPES = %w[builtin external].freeze+ # `mcp_only` removes the in-app assistant (chat, sidebar, chat-seeding+ # buttons) without touching MCP: /mcp serves the same tool registry to+ # external agents whatever this says. "Off" stays free for a future+ # no-LLM-at-all switch; categorization still runs under `mcp_only`.+ ASSISTANT_TYPES = %w[builtin external mcp_only].freeze# Which provider categorizes this family's transactions. Family-level rather# than a global Setting because it decides whose transaction descriptions get@@ -171,6 +175,16 @@ class Family < ApplicationRecordENV["CATEGORIZATION_PROVIDER"].presence || categorization_providerend+ # ENV-over-column precedence for the assistant, same shape as the provider+ # above. Unknown ENV values pass through; callers treat them as builtin.+ def effective_assistant_type+ ENV["ASSISTANT_TYPE"].presence || assistant_type.presence || "builtin"+ end++ def assistant_enabled?+ effective_assistant_type != "mcp_only"+ end+# The provider that will actually categorize this family's transactions,# falling back to the LLM path when Jev is unselected or unconfigured.# Credentials alone never select Jev; the family has to choose it, becausediff --git a/app/models/user.rb b/app/models/user.rbindex ec71ad07be..e94793e00a 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -196,10 +196,10 @@ class User < ApplicationRecordenddef ai_available?+ effective_type = family&.effective_assistant_type || "builtin"+ return false if effective_type == "mcp_only"return true unless Rails.application.config.app_mode.self_hosted?- effective_type = ENV["ASSISTANT_TYPE"].presence || family&.assistant_type.presence || "builtin"-case effective_typewhen "external"Assistant::External.available_for?(self)diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erbindex 4adbd29bae..1379d7ada3 100644--- a/app/views/layouts/application.html.erb+++ b/app/views/layouts/application.html.erb@@ -1,5 +1,7 @@<% intro_mode = Current.user&.ui_layout_intro? %><% home_path = intro_mode ? chats_path : root_path %>+<%# Assistant type "mcp_only" removes every chat surface. MCP is unaffected. %>+<% assistant_enabled = Current.family.assistant_enabled? %><% mobile_nav_items = if intro_mode[{ name: t(".nav.home"), path: chats_path, icon: "home", icon_custom: false, active: page_active?(chats_path) },@@ -12,7 +14,7 @@ elsebills_nav_item,{ name: t(".nav.reports"), path: reports_path, icon: "chart-bar", icon_custom: false, active: page_active?(reports_path) },plan_nav_item,- { name: t(".nav.assistant"), path: chats_path, icon: "icon-assistant", icon_custom: true, active: page_active?(chats_path), mobile_only: true }+ ({ name: t(".nav.assistant"), path: chats_path, icon: "icon-assistant", icon_custom: true, active: page_active?(chats_path), mobile_only: true } if assistant_enabled)].compactend %>@@ -175,7 +177,9 @@ end %><%= icon("eye", size: "sm", class: "hidden", data: { privacy_mode_target: "iconOn" }) %></button>- <%= icon("panel-right", as_button: true, data: { action: "app-layout#toggleRightSidebar" }) %>+ <% if assistant_enabled %>+ <%= icon("panel-right", as_button: true, data: { action: "app-layout#toggleRightSidebar" }) %>+ <% end %></div></div><% end %>@@ -189,28 +193,30 @@ end %><%= yield %><% end %>- <%# DESKTOP - Right sidebar %>- <%= tag.div class: class_names(- "hidden lg:block relative h-full shrink-0 transition-all duration-300 border-divider",- Current.user.show_ai_sidebar? ? [expanded_sidebar_class, "border-l"] : [collapsed_sidebar_class, "border-l-0"],- ),- style: "max-width: var(--right-sidebar-width, 400px)",- inert: !Current.user.show_ai_sidebar?,- data: { app_layout_target: "rightSidebar", sidebar_resize_target: "rightSidebar" } do %>- <%= render "layouts/shared/sidebar_resize_handle", side: "right" %>- <%= tag.div id: "chat-container", class: "relative h-full px-4 overflow-y-auto", data: { controller: "chat hotkey", chat_response_timeout_value: Chat.response_timeout_ms, turbo_permanent: true } do %>- <div class="flex flex-col h-full justify-between shrink-0">- <%= turbo_frame_tag chat_frame, src: chat_view_path(@chat), loading: "lazy", class: "h-full" do %>- <div class="flex justify-center items-center h-full">- <%= icon("loader-circle", class: "animate-spin") %>+ <% if assistant_enabled %>+ <%# DESKTOP - Right sidebar %>+ <%= tag.div class: class_names(+ "hidden lg:block relative h-full shrink-0 transition-all duration-300 border-divider",+ Current.user.show_ai_sidebar? ? [expanded_sidebar_class, "border-l"] : [collapsed_sidebar_class, "border-l-0"],+ ),+ style: "max-width: var(--right-sidebar-width, 400px)",+ inert: !Current.user.show_ai_sidebar?,+ data: { app_layout_target: "rightSidebar", sidebar_resize_target: "rightSidebar" } do %>+ <%= render "layouts/shared/sidebar_resize_handle", side: "right" %>+ <%= tag.div id: "chat-container", class: "relative h-full px-4 overflow-y-auto", data: { controller: "chat hotkey", chat_response_timeout_value: Chat.response_timeout_ms, turbo_permanent: true } do %>+ <div class="flex flex-col h-full justify-between shrink-0">+ <%= turbo_frame_tag chat_frame, src: chat_view_path(@chat), loading: "lazy", class: "h-full" do %>+ <div class="flex justify-center items-center h-full">+ <%= icon("loader-circle", class: "animate-spin") %>+ </div>+ <% end %>+ </div>++ <% unless Current.user.ai_enabled? %>+ <div class="absolute backdrop-blur-lg inset-0 h-full w-full flex flex-col justify-center items-center px-4">+ <%= render "chats/ai_consent" %></div><% end %>- </div>-- <% unless Current.user.ai_enabled? %>- <div class="absolute backdrop-blur-lg inset-0 h-full w-full flex flex-col justify-center items-center px-4">- <%= render "chats/ai_consent" %>- </div><% end %><% end %><% end %>diff --git a/app/views/settings/hostings/_assistant_settings.html.erb b/app/views/settings/hostings/_assistant_settings.html.erbindex edf4c953d8..175500700a 100644--- a/app/views/settings/hostings/_assistant_settings.html.erb+++ b/app/views/settings/hostings/_assistant_settings.html.erb@@ -8,7 +8,7 @@<% end %></div>- <% effective_type = ENV["ASSISTANT_TYPE"].presence || Current.family.assistant_type %>+ <% effective_type = Current.family.effective_assistant_type %><%= styled_form_with model: Current.family,url: settings_hosting_path,@@ -22,7 +22,8 @@options_for_select([[t(".type_builtin"), "builtin"],- [t(".type_external"), "external"]+ [t(".type_external"), "external"],+ [t(".type_mcp_only"), "mcp_only"]],effective_type),@@ -30,6 +31,9 @@{ disabled: ENV["ASSISTANT_TYPE"].present?,data: { "auto-submit-form-target": "auto" } } %><% end %>+ <% if effective_type == "mcp_only" %>+ <p class="text-sm text-secondary"><%= t(".mcp_only_help") %></p>+ <% end %><% if effective_type == "external" %><div data-testid="external-assistant-info">diff --git a/config/locales/views/settings/hostings/en.yml b/config/locales/views/settings/hostings/en.ymlindex b81b1745b2..780f2a9534 100644--- a/config/locales/views/settings/hostings/en.yml+++ b/config/locales/views/settings/hostings/en.yml@@ -69,10 +69,12 @@ en:mansa: Mansaassistant_settings:title: AI Assistant- description: Choose how the chat assistant responds. Builtin uses your configured LLM provider directly. External delegates to a remote AI agent that can call back to Sure's financial tools via MCP.+ description: Choose how the chat assistant responds. Builtin uses your configured LLM provider directly. External delegates to a remote AI agent that can call back to Sure's financial tools via MCP. None in app removes the in-app chat and keeps MCP available for your own agents.type_label: Assistant typetype_builtin: Builtin (direct LLM)type_external: External (remote agent)+ type_mcp_only: None in app (connect your own agent via MCP)+ mcp_only_help: The in-app assistant is off for everyone in this household. Bills AI shortcuts and AI-written insight text also stop. MCP and background categorization stay available; connect external agents in Settings > MCP.external_status: External assistant endpointexternal_configured: Configuredexternal_not_configured: Not configured. Enter the URL and token below, or set EXTERNAL_ASSISTANT_URL and EXTERNAL_ASSISTANT_TOKEN environment variables.diff --git a/docs/hosting/ai.md b/docs/hosting/ai.mdindex f769b84834..8ff404a8f5 100644--- a/docs/hosting/ai.md+++ b/docs/hosting/ai.md@@ -17,10 +17,11 @@ Sure has **two separate AI systems** that operate independently. Understanding t### 1. Chat Assistant (conversational)-The interactive chat where users ask questions about their finances. Routes through one of two backends:+The interactive chat where users ask questions about their finances. Routes through one of two backends, or is turned off:- **Builtin** (default): Uses the OpenAI-compatible provider configured via `OPENAI_ACCESS_TOKEN` / `OPENAI_URI_BASE` / `OPENAI_MODEL`. Calls Sure's function tools directly (get_accounts, get_transactions, etc.).- **External**: Delegates the entire conversation to a remote AI agent. The agent calls back to Sure via MCP to access financial data. Set `ASSISTANT_TYPE=external` as a global override, or configure each family's assistant type in Settings.+- **None in app** (`mcp_only`): Removes the in-app chat for the household: the right-hand chat sidebar and its toggle, the mobile Assistant tab, and the buttons that start a chat. Bring your own agent and connect it through [MCP](mcp.md), which keeps working. Set `ASSISTANT_TYPE=mcp_only` to remove the chat for every family.### 2. Auto-Categorization and Merchant Detection (background)@@ -32,9 +33,12 @@ Background jobs that classify transactions and detect merchants. These **always*|---------|---------------|---------------------|| `ASSISTANT_TYPE=builtin` (default) | Uses OpenAI provider | Uses OpenAI provider || `ASSISTANT_TYPE=external` | Uses external agent | Still uses OpenAI provider |+| `ASSISTANT_TYPE=mcp_only` | None in app; connect your own agent via MCP | Unchanged; uses the configured categorization provider |If you use an external agent for chat, you still need `OPENAI_ACCESS_TOKEN` set for auto-categorization and merchant detection to work. The two systems are fully independent.+With `mcp_only`, Sure also skips the features that need a user's AI consent: the LLM-written insight text and the bills AI shortcuts (review, smart fill, smart configuration).+## Quickstart: OpenAI TokenThe easiest way to get started with AI features in Sure is to use OpenAI:diff --git a/docs/hosting/mcp.md b/docs/hosting/mcp.mdindex 3c9c23ed82..b1fca8716f 100644--- a/docs/hosting/mcp.md+++ b/docs/hosting/mcp.md@@ -420,6 +420,10 @@ Any AI agent that supports JSON-RPC 2.0 can connect to the MCP endpoint. The age3. Use the JSON-RPC 2.0 format for requests4. Handle the protocol methods: `initialize`, `tools/list`, `tools/call`+### Using MCP without the in-app chat++MCP does not depend on the in-app assistant. To use only your own agent, set the assistant type to **None in app (connect your own agent via MCP)** in **Settings -> Self-Hosting**, or set `ASSISTANT_TYPE=mcp_only` for the whole instance. The chat sidebar and its entry points disappear; `/mcp`, OAuth, and **Settings -> MCP** keep working.+## Troubleshooting### "unauthorized" errordiff --git a/test/controllers/api/v1/chats_controller_test.rb b/test/controllers/api/v1/chats_controller_test.rbindex c7dbcdf768..0c3683b9a5 100644--- a/test/controllers/api/v1/chats_controller_test.rb+++ b/test/controllers/api/v1/chats_controller_test.rb@@ -43,6 +43,30 @@ class Api::V1::ChatsControllerTest < ActionDispatch::IntegrationTestassert_equal "feature_disabled", response_body["error"]end+ test "rejects chat creation for a consenting user when the assistant type is mcp_only" do+ api_key = ApiKey.create!(+ user: @user,+ name: "MCP Only Key",+ scopes: [ "read_write" ],+ source: "web",+ display_key: "test_rw_#{SecureRandom.hex(8)}"+ )++ get "/api/v1/chats", headers: api_headers(api_key)+ assert_response :success++ @user.family.update!(assistant_type: "mcp_only")++ assert_no_difference "Chat.count" do+ post "/api/v1/chats",+ params: { title: "New chat", message: "Hello AI" },+ headers: api_headers(api_key)+ end++ assert_response :forbidden+ assert_equal "feature_disabled", JSON.parse(response.body)["error"]+ end+test "should list chats with read scope" doget "/api/v1/chats", headers: bearer_auth_header(@read_token)assert_response :success@@ -192,4 +216,8 @@ class Api::V1::ChatsControllerTest < ActionDispatch::IntegrationTestdef bearer_auth_header(token){ "Authorization" => "Bearer #{token.token}" }end++ def api_headers(api_key)+ { "X-Api-Key" => api_key.plain_key }+ endenddiff --git a/test/controllers/chats_controller_test.rb b/test/controllers/chats_controller_test.rbindex 65dfd57c9d..426a8b8c58 100644--- a/test/controllers/chats_controller_test.rb+++ b/test/controllers/chats_controller_test.rb@@ -56,6 +56,16 @@ class ChatsControllerTest < ActionDispatch::IntegrationTestassert_redirected_to chats_pathend+ test "does not create a chat for a consenting user when the assistant type is mcp_only" do+ @family.update!(assistant_type: "mcp_only")++ assert_no_difference("Chat.count") do+ post chats_url, params: { chat: { content: "Hello", ai_model: "gpt-4.1" } }+ end++ assert_redirected_to chats_path+ end+test "index shows the AI consent screen instead of the compose form when AI is disabled" do@user.update!(ai_enabled: false)@user.chats.destroy_alldiff --git a/test/controllers/mcp_controller_test.rb b/test/controllers/mcp_controller_test.rbindex 4f9fcf7ffb..ddffd2c688 100644--- a/test/controllers/mcp_controller_test.rb+++ b/test/controllers/mcp_controller_test.rb@@ -330,6 +330,27 @@ class McpControllerTest < ActionDispatch::IntegrationTestendend+ test "tools stay available to external agents when the in-app assistant is off" do+ @user.family.update!(assistant_type: "mcp_only")++ with_mcp_env do+ post "/mcp", params: jsonrpc_request("tools/list").to_json,+ headers: mcp_headers(@token)++ assert_response :ok+ assert_equal Assistant.function_classes(@user).size,+ JSON.parse(response.body)["result"]["tools"].size++ post "/mcp", params: jsonrpc_request("tools/call", { name: "get_accounts", arguments: {} }).to_json,+ headers: mcp_headers(@token)++ assert_response :ok+ result = JSON.parse(response.body)["result"]+ assert_not result["isError"]+ assert JSON.parse(result["content"][0]["text"]).key?("accounts")+ end+ end+test "tools/list accepts and echoes a valid MCP session id" dowith_mcp_cache dowith_mcp_env dodiff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rbindex 1a77f73011..227cba968d 100644--- a/test/controllers/pages_controller_test.rb+++ b/test/controllers/pages_controller_test.rb@@ -21,6 +21,20 @@ class PagesControllerTest < ActionDispatch::IntegrationTestassert_select "#netWorthChart[data-time-series-chart-selectable-value='true'][draggable='false']"end+ test "dashboard has no chat sidebar or toggle when the assistant type is mcp_only" do+ get root_path+ assert_select "#chat-container", count: 1+ assert_select "[data-action='app-layout#toggleRightSidebar']", count: 1++ @family.update!(assistant_type: "mcp_only")+ get root_path++ assert_response :ok+ assert_select "#chat-container", count: 0+ assert_select "[data-action='app-layout#toggleRightSidebar']", count: 0+ assert_select "a[href='#{chats_path}']", count: 0+ end+test "inactive user's existing session is revoked" dosession_record = @user.sessions.order(:created_at).last@user.update_column(:active, false)diff --git a/test/controllers/settings/hostings_controller_test.rb b/test/controllers/settings/hostings_controller_test.rbindex 811632c5c9..29901a65b0 100644--- a/test/controllers/settings/hostings_controller_test.rb+++ b/test/controllers/settings/hostings_controller_test.rb@@ -711,6 +711,19 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestendend+ test "can turn the in-app assistant off" do+ with_self_hosting do+ patch settings_hosting_url, params: { family: { assistant_type: "mcp_only" } }++ assert_redirected_to settings_hosting_url+ assert_equal "mcp_only", users(:family_admin).family.reload.assistant_type++ get settings_hosting_url+ assert_response :success+ assert_select "select[name='family[assistant_type]'] option[value='mcp_only'][selected]"+ end+ end+test "can update external assistant settings" dowith_self_hosting doAssistant::External::ModelCatalog.any_instance.stubs(:models).returns([diff --git a/test/models/chat_test.rb b/test/models/chat_test.rbindex b7433b8fc1..36273eb690 100644--- a/test/models/chat_test.rb+++ b/test/models/chat_test.rb@@ -40,6 +40,19 @@ class ChatTest < ActiveSupport::TestCaseendend+ test "a response queued before the assistant was turned off never reaches a provider" do+ chat = @user.chats.start!("Test prompt", model: "gpt-4.1")+ user_message = chat.messages.find_by!(type: "UserMessage")+ pending = chat.messages.find_by!(type: "AssistantMessage", status: "pending")++ @user.family.update!(assistant_type: "mcp_only")+ Assistant.expects(:for_chat).never++ AssistantResponseJob.perform_now(user_message, pending)++ assert_not Message.exists?(pending.id)+ end+test "creates with default model when model is nil" doprompt = "Test prompt"diff --git a/test/models/user_test.rb b/test/models/user_test.rbindex 4d951e7a1c..d40d2229f3 100644--- a/test/models/user_test.rb+++ b/test/models/user_test.rb@@ -485,6 +485,32 @@ class UserTest < ActiveSupport::TestCase@user.family.update!(assistant_type: "builtin")end+ test "ai_available? is false when the assistant type is mcp_only, even with a provider configured" do+ previous = Setting.openai_access_token+ @user.family.update!(assistant_type: "mcp_only")+ with_env_overrides OPENAI_ACCESS_TOKEN: nil do+ Setting.openai_access_token = "token"++ Rails.application.config.app_mode.stubs(:self_hosted?).returns(true)+ assert_not @user.ai_available?+ assert_not @user.ai_enabled?++ Rails.application.config.app_mode.stubs(:self_hosted?).returns(false)+ assert_not @user.ai_available?+ end+ ensure+ Setting.openai_access_token = previous+ end++ test "ASSISTANT_TYPE=mcp_only overrides the family assistant type" do+ Rails.application.config.app_mode.stubs(:self_hosted?).returns(true)+ @user.family.update!(assistant_type: "external")+ with_env_overrides ASSISTANT_TYPE: "mcp_only", EXTERNAL_ASSISTANT_URL: "http://localhost:18789/v1/chat", EXTERNAL_ASSISTANT_TOKEN: "test-token", EXTERNAL_ASSISTANT_MODEL: "openclaw/main" do+ assert_not @user.ai_available?+ assert_not @user.family.assistant_enabled?+ end+ end+test "intro layout collapses sidebars and enables ai" douser = User.new(family: families(:empty),