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 =

Returns Default CORS headers.

Returns:

  • (Array)

    Default CORS headers.

['Content-Type', 'Authorization', 'Accept', 'Mcp-Session-Id'].freeze
DEFAULT_MAX_AGE =

Returns Default CORS preflight cache duration in seconds.

Returns:

  • (Object)

    Default CORS preflight cache duration in seconds.

86_400
CONFIG_FIELDS =

Returns Configuration field names for CORS middleware.

Returns:

  • (Array)

    Configuration field names for CORS middleware.

[:origins, :methods, :headers, :max_age].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, config: nil) ⇒ Cors

Returns a new instance of Cors.



28
29
30
31
32
33
34
# File 'lib/rosett_ai/mcp/middleware/cors.rb', line 28

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



38
39
40
41
42
43
44
# File 'lib/rosett_ai/mcp/middleware/cors.rb', line 38

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