Class: ZeroMcp::Server
- Inherits:
-
Object
- Object
- ZeroMcp::Server
- Defined in:
- lib/zeromcp/server.rb
Instance Method Summary collapse
-
#handle_request(request) ⇒ Object
Process a single JSON-RPC request hash and return a response hash.
-
#initialize(config = nil, tools: nil) ⇒ Server
constructor
A new instance of Server.
-
#load_tools ⇒ Object
Load tools (and resources/prompts) from the configured directories.
- #serve ⇒ Object
-
#serve_http(port: 3000) ⇒ Object
Start an HTTP server that exposes: POST /mcp — JSON-RPC over HTTP
— per-tool route handlers (tools with a route:field).
Constructor Details
#initialize(config = nil, tools: nil) ⇒ Server
Returns a new instance of Server.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/zeromcp/server.rb', line 17 def initialize(config = nil, tools: nil) @config = config || Config.load @scanner = Scanner.new(@config) @resource_scanner = ResourceScanner.new(@config) @prompt_scanner = PromptScanner.new(@config) @tools = tools || {} @resources = {} @templates = {} @prompts = {} @subscriptions = {} @log_level = 'info' @icon = tools ? Config.resolve_icon(@config.icon) : nil @credential_cache = {} end |
Instance Method Details
#handle_request(request) ⇒ Object
Process a single JSON-RPC request hash and return a response hash. Returns nil for notifications that require no response.
Note: tools must be loaded first via #serve or by calling load_tools manually if using this method directly for HTTP integration.
Usage:
response = server.handle_request({"jsonrpc" => "2.0", "id" => 1, "method" => "tools/list"})
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/zeromcp/server.rb', line 153 def handle_request(request) id = request['id'] method = request['method'] params = request['params'] || {} # Notifications (no id) if id.nil? handle_notification(method, params) return nil end case method when 'initialize' handle_initialize(id, params) when 'ping' { 'jsonrpc' => '2.0', 'id' => id, 'result' => {} } # Tools when 'tools/list' handle_tools_list(id, params) when 'tools/call' { 'jsonrpc' => '2.0', 'id' => id, 'result' => call_tool(params) } # Resources when 'resources/list' handle_resources_list(id, params) when 'resources/read' handle_resources_read(id, params) when 'resources/subscribe' handle_resources_subscribe(id, params) when 'resources/templates/list' handle_resources_templates_list(id, params) # Prompts when 'prompts/list' handle_prompts_list(id, params) when 'prompts/get' handle_prompts_get(id, params) # Passthrough when 'logging/setLevel' handle_logging_set_level(id, params) when 'completion/complete' handle_completion_complete(id, params) else { 'jsonrpc' => '2.0', 'id' => id, 'error' => { 'code' => -32601, 'message' => "Method not found: #{method}" } } end end |
#load_tools ⇒ Object
Load tools (and resources/prompts) from the configured directories. Call this before using handle_request directly (serve calls this automatically).
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/zeromcp/server.rb', line 90 def load_tools @tools = @scanner.scan @resource_scanner.scan @resources = @resource_scanner.resources @templates = @resource_scanner.templates @prompt_scanner.scan @prompts = @prompt_scanner.prompts @icon = Config.resolve_icon(@config.icon) resource_count = @resources.size + @templates.size $stderr.puts "[zeromcp] #{@tools.size} tool(s), #{resource_count} resource(s), #{@prompts.size} prompt(s)" end |
#serve ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/zeromcp/server.rb', line 103 def serve $stdout.sync = true $stderr.sync = true $stdin.set_encoding('UTF-8') $stdout.set_encoding('UTF-8') @tools = @scanner.scan @resource_scanner.scan @resources = @resource_scanner.resources @templates = @resource_scanner.templates @prompt_scanner.scan @prompts = @prompt_scanner.prompts @icon = Config.resolve_icon(@config.icon) resource_count = @resources.size + @templates.size $stderr.puts "[zeromcp] #{@tools.size} tool(s), #{resource_count} resource(s), #{@prompts.size} prompt(s)" $stderr.puts "[zeromcp] stdio transport ready" $stdin.each_line do |line| begin line = line.encode('UTF-8', invalid: :replace, undef: :replace, replace: '').strip rescue StandardError next end next if line.empty? begin request = JSON.parse(line) rescue JSON::ParserError, EncodingError, StandardError next end next unless request.is_a?(Hash) response = handle_request(request) if response $stdout.puts JSON.generate(response) $stdout.flush end end end |
#serve_http(port: 3000) ⇒ Object
Start an HTTP server that exposes:
POST /mcp — JSON-RPC over HTTP
<method> <path> — per-tool route handlers (tools with a `route:` field)
Usage: ZeroMcp::Server.new.serve_http(port: 3000)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/zeromcp/server.rb', line 37 def serve_http(port: 3000) load_tools http = WEBrick::HTTPServer.new(Port: port, Logger: WEBrick::Log.new($stderr), AccessLog: []) $stderr.puts "[zeromcp] HTTP server listening on port #{port}" # /mcp — JSON-RPC endpoint http.mount_proc('/mcp') do |req, res| begin request = JSON.parse(req.body || '{}') rescue JSON::ParserError res.status = 400 res.content_type = 'application/json' res.body = JSON.generate({ 'error' => 'Invalid JSON' }) next end response = handle_request(request) res.content_type = 'application/json' res.body = JSON.generate(response || {}) end # /openapi.json — auto-generated OpenAPI 3.0 spec from routed tools http.mount_proc('/openapi.json') do |_req, res| res.content_type = 'application/json' res.body = JSON.generate(build_openapi_spec) end # /docs — Swagger UI http.mount_proc('/docs') do |_req, res| res.content_type = 'text/html' res.body = SWAGGER_UI_HTML end # /health — liveness check http.mount_proc('/health') do |_req, res| res.content_type = 'application/json' res.body = JSON.generate({ 'ok' => true }) end # Register per-tool HTTP routes @tools.each do |_name, tool| next unless tool.route.is_a?(Hash) register_tool_route(http, tool, tool.route_method, tool.route_path) end trap('INT') { http.shutdown } trap('TERM') { http.shutdown } http.start end |