The personal finance app for everyone (by everyone)
Ruby
fix(mcp): assign OAuth clients read_write scope (#2884)
Atlas53a87a7645e6
diff --git a/app/controllers/oauth/authorizations_controller.rb b/app/controllers/oauth/authorizations_controller.rbnew file mode 100644index 0000000000..05c4f5af4f--- /dev/null+++ b/app/controllers/oauth/authorizations_controller.rb@@ -0,0 +1,12 @@+class Oauth::AuthorizationsController < Doorkeeper::AuthorizationsController+ before_action :default_mcp_scope, only: [ :new, :create ]++ private++ def default_mcp_scope+ return if params[:scope].present?++ application = Doorkeeper::Application.find_by(uid: params[:client_id])+ params[:scope] = "read_write" if application&.scopes.to_s.split == [ "read_write" ]+ end+enddiff --git a/app/controllers/oauth_registration_controller.rb b/app/controllers/oauth_registration_controller.rbindex 1b3170be44..f04121f2a1 100644--- a/app/controllers/oauth_registration_controller.rb+++ b/app/controllers/oauth_registration_controller.rb@@ -48,7 +48,11 @@ class OauthRegistrationController < ApplicationControllerapp = Doorkeeper::Application.new(name: client_name,redirect_uri: redirect_uris.join("\n"),- confidential: false+ confidential: false,+ # MCP requires the read_write scope. Without assigning it to the+ # dynamically registered client, Doorkeeper falls back to the provider's+ # default read scope and the token is rejected by McpController.+ scopes: "read_write")if app.savediff --git a/config/routes.rb b/config/routes.rbindex 305b33d07e..76d99fdd3c 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -207,7 +207,9 @@ Rails.application.routes.draw doget ".well-known/oauth-protected-resource", to: "oauth_metadata#protected_resource"get ".well-known/oauth-authorization-server", to: "oauth_metadata#authorization_server"post "register", to: "oauth_registration#create"- use_doorkeeper+ use_doorkeeper do |mapping|+ mapping.controllers authorizations: "oauth/authorizations"+ end# MFA routesresource :mfa, controller: "mfa", only: [ :new, :create ] doget :verifydiff --git a/docs/hosting/mcp.md b/docs/hosting/mcp.mdindex 39b60bd8e6..d491f07ae0 100644--- a/docs/hosting/mcp.md+++ b/docs/hosting/mcp.md@@ -13,14 +13,15 @@ This is useful when:## Prerequisites-To enable the MCP endpoint, you need to set two environment variables:+For legacy bearer-token authentication, set these two environment variables:| Variable | Description | Example ||----------|-------------|---------|| `MCP_API_TOKEN` | Bearer token for authentication | `your-secret-token-here` || `MCP_USER_EMAIL` | Email of the Sure user whose data the assistant can access | `[email protected]` |-Both variables are **required**. The endpoint will not activate if either is missing.+Both variables are required for the legacy token flow. OAuth clients using the+MCP discovery and dynamic registration endpoints do not need these variables.### Generating a secure token@@ -82,7 +83,14 @@ POST /mcp### Authentication-All requests must include the `MCP_API_TOKEN` as a Bearer token:+MCP supports OAuth authorization-code flow for clients such as Claude Code.+Clients should discover the protected-resource metadata, register dynamically,+request the advertised `read_write` scope, and send the resulting access token+as a Bearer token. Dynamically registered clients are assigned this scope so+their tokens can authenticate to MCP.++For self-hosted deployments or clients without OAuth support, requests may use+the legacy `MCP_API_TOKEN` as a Bearer token:```Authorization: Bearer <MCP_API_TOKEN>diff --git a/test/controllers/mcp_controller_test.rb b/test/controllers/mcp_controller_test.rbindex 58988918ef..318dae5ea1 100644--- a/test/controllers/mcp_controller_test.rb+++ b/test/controllers/mcp_controller_test.rb@@ -47,6 +47,56 @@ class McpControllerTest < ActionDispatch::IntegrationTestassert_equal "2025-03-26", result["protocolVersion"]end+ test "authenticates a token issued to a dynamically registered MCP client" do+ post "/register",+ params: {+ client_name: "Claude",+ redirect_uris: [ "https://claude.ai/callback" ],+ grant_types: [ "authorization_code" ],+ response_types: [ "code" ],+ token_endpoint_auth_method: "none"+ }.to_json,+ headers: { "Content-Type" => "application/json" }++ assert_response :created+ app = Doorkeeper::Application.find_by!(uid: JSON.parse(response.body)["client_id"])+ assert_equal "read_write", app.scopes.to_s++ sign_in(@user)+ verifier = SecureRandom.urlsafe_base64(64)+ challenge = Base64.urlsafe_encode64(Digest::SHA256.digest(verifier), padding: false)++ post "/oauth/authorize", params: {+ client_id: app.uid,+ redirect_uri: app.redirect_uri,+ response_type: "code",+ code_challenge: challenge,+ code_challenge_method: "S256"+ }++ assert_response :redirect+ code = Rack::Utils.parse_query(URI.parse(response.location).query)["code"]+ assert code.present?, "Authorization response should contain a code"++ post "/oauth/token", params: {+ grant_type: "authorization_code",+ client_id: app.uid,+ redirect_uri: app.redirect_uri,+ code: code,+ code_verifier: verifier+ }++ assert_response :success+ token_response = JSON.parse(response.body)+ assert_equal "read_write", token_response["scope"]++ post "/mcp", params: jsonrpc_request("initialize").to_json,+ headers: mcp_headers(token_response["access_token"])++ assert_response :ok+ assert_equal "2025-03-26", JSON.parse(response.body).dig("result", "protocolVersion")+ end+test "rejects token with read-only scope" doapp = Doorkeeper::Application.create!(name: "Test MCP Client #{SecureRandom.hex(4)}",diff --git a/test/controllers/oauth_registration_controller_test.rb b/test/controllers/oauth_registration_controller_test.rbindex 929e83e272..a509fa350b 100644--- a/test/controllers/oauth_registration_controller_test.rb+++ b/test/controllers/oauth_registration_controller_test.rb@@ -24,6 +24,7 @@ class OauthRegistrationControllerTest < ActionDispatch::IntegrationTestapp = Doorkeeper::Application.find_by(uid: json["client_id"])assert app.present?, "Application should be persisted"assert_not app.confidential?, "Application must be non-confidential (public client)"+ assert_equal "read_write", app.scopes.to_sendtest "returns error for invalid JSON body" do