Class: ZeroMcp::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Server

Returns a new instance of Server.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/zeromcp/server.rb', line 14

def initialize(config = nil)
  @config = config || Config.load
  @scanner = Scanner.new(@config)
  @resource_scanner = ResourceScanner.new(@config)
  @prompt_scanner = PromptScanner.new(@config)
  @tools = {}
  @resources = {}
  @templates = {}
  @prompts = {}
  @subscriptions = {}
  @log_level = 'info'
  @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"})


95
96
97
98
99
100
101
102
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
144
145
146
147
# File 'lib/zeromcp/server.rb', line 95

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_toolsObject

Load tools (and resources/prompts) from the configured directories. Call this before using handle_request directly (serve calls this automatically).



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zeromcp/server.rb', line 32

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

#serveObject



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 45

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