Class: RailsAiContext::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_context/middleware.rb

Overview

Rack middleware that intercepts requests at the configured HTTP path and delegates to the MCP StreamableHTTPTransport. All other requests pass through to the Rails app.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



11
12
13
14
15
# File 'lib/rails_ai_context/middleware.rb', line 11

def initialize(app)
  @app = app
  @mcp_transport = nil
  @mutex = Mutex.new
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_ai_context/middleware.rb', line 17

def call(env)
  config = RailsAiContext.configuration
  path = config.http_path

  if env["PATH_INFO"] == path || env["PATH_INFO"] == "#{path}/"
    request = Rack::Request.new(env)
    transport.handle_request(request)
  else
    @app.call(env)
  end
rescue => e
  Rails.logger.error "[rails-ai-context] MCP request failed: #{e.class}: #{e.message}"
  json_rpc_error_response(e)
end