Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(mcp): advertise the required read_write scope during discovery (#4490)

Kenrick Tandrian4eced99f2af9
diff --git a/app/controllers/mcp_controller.rb b/app/controllers/mcp_controller.rb
index 32243d0e51..78139426da 100644
--- a/app/controllers/mcp_controller.rb
+++ b/app/controllers/mcp_controller.rb
@@ -4,6 +4,7 @@ class McpController < ApplicationController
PROTOCOL_VERSION = "2025-06-18"
SUPPORTED_PROTOCOL_VERSIONS = [ "2025-03-26", PROTOCOL_VERSION ].freeze
MCP_SESSION_TTL = 1.day
+ REQUIRED_SCOPE = "read_write"
# Skip session-based auth and CSRF — this is a token-authenticated API
skip_authentication
@@ -133,7 +134,7 @@ class McpController < ApplicationController
def authenticate_via_doorkeeper(token)
access_token = Doorkeeper::AccessToken.by_token(token)
return false unless access_token&.accessible?
- return false unless access_token.scopes.include?("read_write")
+ return false unless access_token.scopes.include?(REQUIRED_SCOPE)
user = User.find_by(id: access_token.resource_owner_id)
return false unless user&.active?
@@ -221,7 +222,7 @@ class McpController < ApplicationController
def render_mcp_unauthorized
response.set_header(
"WWW-Authenticate",
- "Bearer resource_metadata=\"#{configured_base_url}/.well-known/oauth-protected-resource\""
+ "Bearer resource_metadata=\"#{configured_base_url}/.well-known/oauth-protected-resource\", scope=\"#{REQUIRED_SCOPE}\""
)
render json: { error: "unauthorized" }, status: :unauthorized
end
diff --git a/app/controllers/oauth_metadata_controller.rb b/app/controllers/oauth_metadata_controller.rb
index d5260ff552..c06f292654 100644
--- a/app/controllers/oauth_metadata_controller.rb
+++ b/app/controllers/oauth_metadata_controller.rb
@@ -10,7 +10,9 @@ class OauthMetadataController < ApplicationController
def protected_resource
render json: {
resource: configured_base_url,
- authorization_servers: [ configured_base_url ]
+ authorization_servers: [ configured_base_url ],
+ scopes_supported: [ McpController::REQUIRED_SCOPE ],
+ bearer_methods_supported: [ "header" ]
}
end
@@ -23,7 +25,7 @@ class OauthMetadataController < ApplicationController
response_types_supported: [ "code" ],
grant_types_supported: [ "authorization_code" ],
code_challenge_methods_supported: [ "S256" ],
- scopes_supported: [ "read_write" ]
+ scopes_supported: [ McpController::REQUIRED_SCOPE ]
}
end
end
diff --git a/test/controllers/mcp_controller_test.rb b/test/controllers/mcp_controller_test.rb
index ef4877dec8..0d92ef75dd 100644
--- a/test/controllers/mcp_controller_test.rb
+++ b/test/controllers/mcp_controller_test.rb
@@ -18,6 +18,7 @@ class McpControllerTest < ActionDispatch::IntegrationTest
assert_equal "unauthorized", JSON.parse(response.body)["error"]
assert response.headers["WWW-Authenticate"].present?, "Must include WWW-Authenticate header"
assert_includes response.headers["WWW-Authenticate"], "oauth-protected-resource"
+ assert_includes response.headers["WWW-Authenticate"], "scope=\"read_write\""
end
test "returns 401 with wrong token" do
diff --git a/test/controllers/oauth_metadata_controller_test.rb b/test/controllers/oauth_metadata_controller_test.rb
index 538456095a..4703ff4f99 100644
--- a/test/controllers/oauth_metadata_controller_test.rb
+++ b/test/controllers/oauth_metadata_controller_test.rb
@@ -13,6 +13,8 @@ class OauthMetadataControllerTest < ActionDispatch::IntegrationTest
json = JSON.parse(response.body)
assert_equal @base, json["resource"]
assert_equal [ @base ], json["authorization_servers"]
+ assert_equal [ "read_write" ], json["scopes_supported"]
+ assert_equal [ "header" ], json["bearer_methods_supported"]
end
test "authorization_server returns RFC 8414 metadata" do