Class: McpOnRuby::Transport::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp_on_ruby/transport.rb

Overview

Rack middleware for handling MCP requests

Instance Method Summary collapse

Constructor Details

#initialize(app, server:, **options) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



7
8
9
10
11
12
13
# File 'lib/mcp_on_ruby/transport.rb', line 7

def initialize(app, server:, **options)
  @app = app
  @server = server
  @path = options[:path] || @server.configuration.path
  @logger = McpOnRuby.logger
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mcp_on_ruby/transport.rb', line 15

def call(env)
  request = Rack::Request.new(env)
  
  # Only handle requests to our MCP path
  return @app.call(env) unless request.path == @path
  
  # Only handle POST requests for JSON-RPC
  return method_not_allowed_response unless request.post?
  
  # Security checks
  security_result = check_security(request)
  return security_result if security_result
  
  # Handle MCP request
  handle_mcp_request(request)
  
rescue => error
  @logger.error("Transport error: #{error.message}")
  @logger.error(error.backtrace.join("\n"))
  internal_error_response
end