Class: RosettAi::Mcp::Middleware::Cors

Inherits:
Object
  • Object
show all
Defined in:
lib/rosett_ai/mcp/middleware/cors.rb

Overview

Rack middleware for CORS header handling and preflight.

Disabled by default. When enabled, handles OPTIONS preflight and adds CORS headers to responses.

Author:

  • hugo

  • claude

Constant Summary collapse

DEFAULT_METHODS =
['POST', 'GET', 'DELETE'].freeze
DEFAULT_HEADERS =
['Content-Type', 'Authorization', 'Accept', 'Mcp-Session-Id'].freeze
DEFAULT_MAX_AGE =
86_400
CONFIG_FIELDS =
[:origins, :methods, :headers, :max_age].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, config: nil) ⇒ Cors

Returns a new instance of Cors.



24
25
26
27
28
29
30
# File 'lib/rosett_ai/mcp/middleware/cors.rb', line 24

def initialize(app, config: nil)
  @app = app
  @origins = resolve_config(config, :origins, [])
  @methods = resolve_config(config, :methods, DEFAULT_METHODS)
  @headers = resolve_config(config, :headers, DEFAULT_HEADERS)
  @max_age = resolve_config(config, :max_age, DEFAULT_MAX_AGE)
end

Instance Method Details

#call(env) ⇒ Array

Returns Rack response triplet.

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Array)

    Rack response triplet



34
35
36
37
38
39
40
# File 'lib/rosett_ai/mcp/middleware/cors.rb', line 34

def call(env)
  return handle_preflight(env) if preflight?(env)

  status, headers, body = @app.call(env)
  add_cors_headers(headers, env)
  [status, headers, body]
end