Class: MCP::Server::OAuth::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/server/oauth/middleware.rb

Overview

Rack middleware that enforces bearer authentication for everything it wraps, per RFC 6750 and the MCP authorization specification:

use MCP::Server::OAuth::Middleware,
token_verifier: verifier,
required_scopes: ["mcp:tools"],
resource_metadata: 
run transport

See Authenticator for the option semantics; this class only adapts it to the Rack middleware calling convention. The streamable HTTP transport embeds the same authenticator via its token_verifier: option, so use this middleware when composition at the Rack layer is preferable (for example to share one authenticator across several apps).

Tokens are accepted from the Authorization header only, never from a query string. On success the verified AccessToken is stored in env[MCP::Server::OAuth::ENV_KEY], where the streamable HTTP transport picks it up and exposes it to handlers as server_context.auth_info.

Because the middleware wraps the whole app, POST, GET (SSE), and DELETE requests are protected uniformly. Mount the metadata document outside the protected scope: it is how unauthenticated clients bootstrap. When a browser-based client is involved, run the CORS middleware before this one; otherwise its preflight OPTIONS request dies here with a 401 that the browser will not let the client see.

Instance Method Summary collapse

Constructor Details

#initialize(app, token_verifier:, required_scopes: [], resource_metadata: nil, resource_metadata_url: nil, scope_matcher: nil) ⇒ Middleware

Returns a new instance of Middleware.



29
30
31
32
33
34
35
36
37
38
# File 'lib/mcp/server/oauth/middleware.rb', line 29

def initialize(app, token_verifier:, required_scopes: [], resource_metadata: nil, resource_metadata_url: nil, scope_matcher: nil)
  @app = app
  @authenticator = Authenticator.new(
    token_verifier: token_verifier,
    required_scopes: required_scopes,
    resource_metadata: ,
    resource_metadata_url: ,
    scope_matcher: scope_matcher,
  )
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mcp/server/oauth/middleware.rb', line 40

def call(env)
  begin
    env[ENV_KEY] = @authenticator.authenticate(env)
  rescue Error => e
    return @authenticator.challenge_response(e)
  rescue => e
    # A misbehaving verifier (e.g. an unreachable JWKS or introspection endpoint) is an internal failure: report it
    # and answer 500 without a challenge, so the client does not discard a perfectly good token.
    MCP.configuration.exception_reporter.call(e, { middleware: self.class.name })

    return [500, { "content-type" => "application/json" }, [{ error: "server_error" }.to_json]]
  end

  # Outside the rescue on purpose: only verification is this middleware's business, and a failure in the wrapped app
  # must reach whatever handles that app's errors, as it does with the Python SDK's bearer middleware.
  @app.call(env)
end