Class: McpOnRuby::Server
- Inherits:
-
Object
- Object
- McpOnRuby::Server
- 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
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Instance Method Summary collapse
-
#capabilities ⇒ Hash
Get server capabilities for MCP initialization.
-
#handle_request(request_body, context = {}) ⇒ String?
Handle a JSON-RPC request.
-
#initialize(options = {}) {|Server| ... } ⇒ Server
constructor
Initialize a new MCP server.
-
#register_resource(resource, uri = nil) ⇒ Resource
Register a resource.
-
#register_tool(tool, name = nil) ⇒ Tool
Register a tool.
-
#resource(uri, **options, &block) ⇒ Resource
DSL method to define a resource.
-
#server_info ⇒ Hash
Get server information.
-
#tool(name, description = '', input_schema = {}, **options, &block) ⇒ Tool
DSL method to define a tool.
Constructor Details
#initialize(options = {}) {|Server| ... } ⇒ Server
Initialize a new MCP server
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( = {}, &block) @configuration = McpOnRuby.configuration || Configuration.new @configuration.tap do |config| .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
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
6 7 8 |
# File 'lib/mcp_on_ruby/server.rb', line 6 def configuration @configuration end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'lib/mcp_on_ruby/server.rb', line 6 def logger @logger end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
6 7 8 |
# File 'lib/mcp_on_ruby/server.rb', line 6 def resources @resources end |
#tools ⇒ Object (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
#capabilities ⇒ Hash
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, **, &block) resource_instance = McpOnRuby.resource(uri, **, &block) register_resource(resource_instance) end |
#server_info ⇒ Hash
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 = {}, **, &block) tool_instance = McpOnRuby.tool(name, description, input_schema, **, &block) register_tool(tool_instance) end |