Class: MCP::Server::OAuth::ProtectedResourceMetadataMiddleware
- Inherits:
-
Object
- Object
- MCP::Server::OAuth::ProtectedResourceMetadataMiddleware
- Defined in:
- lib/mcp/server/oauth/protected_resource_metadata_middleware.rb
Overview
Rack middleware that serves a Protected Resource Metadata document (RFC 9728) at its well-known path,
with the CORS headers browser-based MCP clients need, and passes every other request down the stack.
The Rack shape of the Python SDK's create_protected_resource_routes and the TypeScript SDK's mcpAuthMetadataRouter.
Use it at the top of the stack: the well-known path lives at the origin root, not under the MCP endpoint
(/.well-known/oauth-protected-resource/mcp for a resource at /mcp), so inside a map block it would never see that path.
The metadata is how unauthenticated clients bootstrap, so it belongs above any bearer enforcement.
metadata = MCP::Server::OAuth::ProtectedResourceMetadata.new(...)
use MCP::Server::OAuth::ProtectedResourceMetadataMiddleware, metadata
map("/mcp") { run(transport) }
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, metadata) ⇒ ProtectedResourceMetadataMiddleware
constructor
A new instance of ProtectedResourceMetadataMiddleware.
Constructor Details
#initialize(app, metadata) ⇒ ProtectedResourceMetadataMiddleware
Returns a new instance of ProtectedResourceMetadataMiddleware.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mcp/server/oauth/protected_resource_metadata_middleware.rb', line 25 def initialize(app, ) # The document is published as `to_json`, so only the class whose serialization is a validated RFC 9728 document will do. unless .is_a?(ProtectedResourceMetadata) raise ArgumentError, "metadata must be a ProtectedResourceMetadata (got #{.class})" end @app = app @well_known_path = .well_known_path @metadata_json = .to_json end |
Instance Method Details
#call(env) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mcp/server/oauth/protected_resource_metadata_middleware.rb', line 36 def call(env) # An exact match only: a deeper well-known path describes another resource on this host (RFC 9728), # and every other path is the application's. return @app.call(env) unless env["PATH_INFO"] == @well_known_path case env["REQUEST_METHOD"] when "GET" when "HEAD" (head: true) when "OPTIONS" preflight_response else method_not_allowed_response end end |