Class: FastMcp::Transports::AuthenticatedRackTransport

Inherits:
RackTransport show all
Defined in:
lib/mcp/transports/authenticated_rack_transport.rb

Constant Summary

Constants inherited from RackTransport

RackTransport::DEFAULT_ALLOWED_IPS, RackTransport::DEFAULT_ALLOWED_ORIGINS, RackTransport::DEFAULT_PATH_PREFIX, RackTransport::SERVER_ENV_KEY, RackTransport::SSE_HEADERS

Instance Attribute Summary

Attributes inherited from RackTransport

#allowed_ips, #allowed_origins, #app, #localhost_only, #messages_route, #path_prefix, #sse_clients, #sse_route

Attributes inherited from BaseTransport

#logger, #server

Instance Method Summary collapse

Methods inherited from RackTransport

#call, #register_sse_client, #send_message, #start, #stop, #unregister_sse_client

Methods inherited from BaseTransport

#process_message, #send_message, #start, #stop

Constructor Details

#initialize(app, server, options = {}) ⇒ AuthenticatedRackTransport

Returns a new instance of AuthenticatedRackTransport.



8
9
10
11
12
13
14
15
# File 'lib/mcp/transports/authenticated_rack_transport.rb', line 8

def initialize(app, server, options = {})
  super

  @auth_token = options[:auth_token]
  @auth_header_name = options[:auth_header_name] || 'Authorization'
  @auth_exempt_paths = options[:auth_exempt_paths] || []
  @auth_enabled = !@auth_token.nil?
end

Instance Method Details

#handle_mcp_request(request, env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/mcp/transports/authenticated_rack_transport.rb', line 17

def handle_mcp_request(request, env)
  if auth_enabled? && !exempt_from_auth?(request.path)
    auth_header = request.env["HTTP_#{@auth_header_name.upcase.gsub('-', '_')}"]
    token = auth_header&.gsub('Bearer ', '')

    return unauthorized_response(request) unless valid_token?(token)
  end

  super
end