Module: BetterAuth::Plugins::MCP::ResourceHandler

Defined in:
lib/better_auth/plugins/mcp/resource_handler.rb

Class Method Summary collapse

Class Method Details

.unauthorized(resource_metadata_url) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/better_auth/plugins/mcp/resource_handler.rb', line 24

def unauthorized()
  [
    401,
    {
      "www-authenticate" => %(Bearer resource_metadata="#{}"),
      "access-control-expose-headers" => "WWW-Authenticate"
    },
    ["unauthorized"]
  ]
end

.with_mcp_auth(app, resource_metadata_url:, auth: nil, resource_metadata_mappings: {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/better_auth/plugins/mcp/resource_handler.rb', line 9

def with_mcp_auth(app, resource_metadata_url:, auth: nil, resource_metadata_mappings: {})
  lambda do |env|
    authorization = env["HTTP_AUTHORIZATION"].to_s
    return unauthorized() unless authorization.start_with?("Bearer ")

    session = auth&.api&.get_mcp_session(headers: {"authorization" => authorization})
    return unauthorized() unless session

    env["better_auth.mcp_session"] = session
    app.call(env)
  rescue APIError
    unauthorized()
  end
end