Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(mcp): support RFC 9728 path-scoped protected resource discovery (#4601)

Kenrick Tandrianf252ac5de49a
diff --git a/app/controllers/mcp_controller.rb b/app/controllers/mcp_controller.rb
index 1da564f80e..2e191925b2 100644
--- a/app/controllers/mcp_controller.rb
+++ b/app/controllers/mcp_controller.rb
@@ -223,7 +223,7 @@ class McpController < ApplicationController
def render_mcp_unauthorized
response.set_header(
"WWW-Authenticate",
- "Bearer resource_metadata=\"#{configured_base_url}/.well-known/oauth-protected-resource\", scope=\"#{REQUIRED_SCOPE}\""
+ "Bearer resource_metadata=\"#{configured_base_url}/.well-known/oauth-protected-resource/mcp\", 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 c06f292654..f905cd2986 100644
--- a/app/controllers/oauth_metadata_controller.rb
+++ b/app/controllers/oauth_metadata_controller.rb
@@ -8,9 +8,12 @@ class OauthMetadataController < ApplicationController
skip_before_action :detect_os, raise: false
def protected_resource
+ base = configured_base_url
+ resource_url = params[:resource_path].present? ? "#{base}/#{params[:resource_path]}" : base
+
render json: {
- resource: configured_base_url,
- authorization_servers: [ configured_base_url ],
+ resource: resource_url,
+ authorization_servers: [ base ],
scopes_supported: [ McpController::REQUIRED_SCOPE ],
bearer_methods_supported: [ "header" ]
}
diff --git a/config/routes.rb b/config/routes.rb
index b8a251e821..c94812ac58 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -265,7 +265,10 @@ Rails.application.routes.draw do
post :new_connection
end
end
- get ".well-known/oauth-protected-resource", to: "oauth_metadata#protected_resource"
+ get ".well-known/oauth-protected-resource(/*resource_path)",
+ to: "oauth_metadata#protected_resource",
+ as: :oauth_protected_resource_metadata,
+ format: false
get ".well-known/oauth-authorization-server", to: "oauth_metadata#authorization_server"
post "register", to: "oauth_registration#create"
use_doorkeeper do |mapping|
diff --git a/test/controllers/mcp_controller_test.rb b/test/controllers/mcp_controller_test.rb
index 41a718c6df..e66b24d3c6 100644
--- a/test/controllers/mcp_controller_test.rb
+++ b/test/controllers/mcp_controller_test.rb
@@ -17,7 +17,7 @@ class McpControllerTest < ActionDispatch::IntegrationTest
assert_response :unauthorized
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"], "oauth-protected-resource/mcp"
assert_includes response.headers["WWW-Authenticate"], "scope=\"read_write\""
end
diff --git a/test/controllers/oauth_metadata_controller_test.rb b/test/controllers/oauth_metadata_controller_test.rb
index 4703ff4f99..14b5c19340 100644
--- a/test/controllers/oauth_metadata_controller_test.rb
+++ b/test/controllers/oauth_metadata_controller_test.rb
@@ -17,6 +17,18 @@ class OauthMetadataControllerTest < ActionDispatch::IntegrationTest
assert_equal [ "header" ], json["bearer_methods_supported"]
end
+ test "protected_resource returns path-scoped resource metadata" do
+ get "/.well-known/oauth-protected-resource/mcp"
+
+ assert_response :ok
+ assert_equal "application/json", response.content_type.split(";").first
+ json = JSON.parse(response.body)
+ assert_equal "#{@base}/mcp", 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
get "/.well-known/oauth-authorization-server"