Class: McpOnRuby::Server

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

Overview

Main MCP server class that handles JSON-RPC protocol and manages tools/resources

Defined Under Namespace

Classes: RateLimiter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|Server| ... } ⇒ Server

Initialize a new MCP server

Yields:

  • (Server)

    Server instance for configuration



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mcp_on_ruby/server.rb', line 11

def initialize(options = {}, &block)
  @configuration = McpOnRuby.configuration || Configuration.new
  @configuration.tap do |config|
    options.each { |key, value| config.send("#{key}=", value) if config.respond_to?("#{key}=") }
  end
  
  @logger = McpOnRuby.logger
  @tools = {}
  @resources = {}
  @rate_limiter = RateLimiter.new(@configuration.rate_limit_per_minute)
  
  # Configure the server using the block
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



6
7
8
# File 'lib/mcp_on_ruby/server.rb', line 6

def configuration
  @configuration
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/mcp_on_ruby/server.rb', line 6

def logger
  @logger
end

#resourcesObject (readonly)

Returns the value of attribute resources.



6
7
8
# File 'lib/mcp_on_ruby/server.rb', line 6

def resources
  @resources
end

#toolsObject (readonly)

Returns the value of attribute tools.



6
7
8
# File 'lib/mcp_on_ruby/server.rb', line 6

def tools
  @tools
end

Instance Method Details

#capabilitiesHash

Get server capabilities for MCP initialization



100
101
102
103
104
105
# File 'lib/mcp_on_ruby/server.rb', line 100

def capabilities
  {
    tools: tools.any? ? {} : nil,
    resources: resources.any? ? { subscribe: @configuration.enable_sse } : nil
  }.compact
end

#handle_request(request_body, context = {}) ⇒ String?

Handle a JSON-RPC request



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mcp_on_ruby/server.rb', line 74

def handle_request(request_body, context = {})
  # Parse JSON request
  request = JSON.parse(request_body)
  
  # Rate limiting check
  unless @rate_limiter.allowed?(context[:remote_ip])
    return error_response(nil, -32603, "Rate limit exceeded")
  end
  
  # Handle the request
  response = handle_json_rpc(request, context)
  
  # Return JSON response for requests with ID, nil for notifications
  response ? JSON.generate(response) : nil
  
rescue JSON::ParserError => e
  @logger.warn("Invalid JSON request: #{e.message}")
  JSON.generate(error_response(nil, -32700, "Parse error"))
rescue => e
  @logger.error("Request handling failed: #{e.message}")
  @logger.error(e.backtrace.join("\n"))
  JSON.generate(error_response(nil, -32603, "Internal error"))
end

#register_resource(resource, uri = nil) ⇒ Resource

Register a resource



41
42
43
44
45
46
# File 'lib/mcp_on_ruby/server.rb', line 41

def register_resource(resource, uri = nil)
  resource_uri = uri || resource.uri
  @resources[resource_uri] = resource
  @logger.debug("Registered resource: #{resource_uri}")
  resource
end

#register_tool(tool, name = nil) ⇒ Tool

Register a tool



30
31
32
33
34
35
# File 'lib/mcp_on_ruby/server.rb', line 30

def register_tool(tool, name = nil)
  tool_name = name || tool.name
  @tools[tool_name] = tool
  @logger.debug("Registered tool: #{tool_name}")
  tool
end

#resource(uri, **options, &block) ⇒ Resource

DSL method to define a resource



65
66
67
68
# File 'lib/mcp_on_ruby/server.rb', line 65

def resource(uri, **options, &block)
  resource_instance = McpOnRuby.resource(uri, **options, &block)
  register_resource(resource_instance)
end

#server_infoHash

Get server information



109
110
111
112
113
114
# File 'lib/mcp_on_ruby/server.rb', line 109

def server_info
  {
    name: "mcp_on_ruby",
    version: McpOnRuby::VERSION
  }
end

#tool(name, description = '', input_schema = {}, **options, &block) ⇒ Tool

DSL method to define a tool



55
56
57
58
# File 'lib/mcp_on_ruby/server.rb', line 55

def tool(name, description = '', input_schema = {}, **options, &block)
  tool_instance = McpOnRuby.tool(name, description, input_schema, **options, &block)
  register_tool(tool_instance)
end