Class: Otto::MCP::Auth::TokenMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/otto/mcp/auth/token.rb

Overview

Middleware for token authentication in MCP protocol

Instance Method Summary collapse

Constructor Details

#initialize(app, security_config = nil) ⇒ TokenMiddleware

Returns a new instance of TokenMiddleware.



50
51
52
53
# File 'lib/otto/mcp/auth/token.rb', line 50

def initialize(app, security_config = nil)
  @app             = app
  @security_config = security_config
end

Instance Method Details

#call(env) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/otto/mcp/auth/token.rb', line 55

def call(env)
  # Only apply to MCP endpoints
  return @app.call(env) unless mcp_endpoint?(env)

  # Fail closed: this middleware is only mounted when MCP auth was
  # requested, so a missing authenticator is a misconfiguration, not a
  # licence to serve the endpoint unauthenticated (issue #258).
  auth = @security_config&.mcp_auth
  return unauthorized_response if auth.nil? || !auth.authenticate(env)

  @app.call(env)
end