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.rbindex 1da564f80e..2e191925b2 100644--- a/app/controllers/mcp_controller.rb+++ b/app/controllers/mcp_controller.rb@@ -223,7 +223,7 @@ class McpController < ApplicationControllerdef render_mcp_unauthorizedresponse.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: :unauthorizedenddiff --git a/app/controllers/oauth_metadata_controller.rb b/app/controllers/oauth_metadata_controller.rbindex c06f292654..f905cd2986 100644--- a/app/controllers/oauth_metadata_controller.rb+++ b/app/controllers/oauth_metadata_controller.rb@@ -8,9 +8,12 @@ class OauthMetadataController < ApplicationControllerskip_before_action :detect_os, raise: falsedef 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.rbindex b8a251e821..c94812ac58 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -265,7 +265,10 @@ Rails.application.routes.draw dopost :new_connectionendend- 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: falseget ".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.rbindex 41a718c6df..e66b24d3c6 100644--- a/test/controllers/mcp_controller_test.rb+++ b/test/controllers/mcp_controller_test.rb@@ -17,7 +17,7 @@ class McpControllerTest < ActionDispatch::IntegrationTestassert_response :unauthorizedassert_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\""enddiff --git a/test/controllers/oauth_metadata_controller_test.rb b/test/controllers/oauth_metadata_controller_test.rbindex 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::IntegrationTestassert_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" doget "/.well-known/oauth-authorization-server"