Class: Jazari::Mcp::StdioServer

Inherits:
Object
  • Object
show all
Defined in:
lib/jazari/mcp/stdio_server.rb

Constant Summary collapse

PROTOCOL_VERSION =
"2025-11-25"
SUPPORTED_VERSIONS =
%w[2024-11-05 2025-03-26 2025-06-18 2025-11-25].freeze

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher:, version: Jazari::VERSION) ⇒ StdioServer

Returns a new instance of StdioServer.



13
14
15
16
17
18
# File 'lib/jazari/mcp/stdio_server.rb', line 13

def initialize(dispatcher:, version: Jazari::VERSION)
  @dispatcher = dispatcher
  @version = version
  @initialization_response_sent = false
  @initialized = false
end

Instance Method Details

#operational_response(message) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/jazari/mcp/stdio_server.rb', line 54

def operational_response(message)
  case message["method"]
  when "tools/list" then list_tools(message)
  when "tools/call" then call_tool(message)
  else jsonrpc_error(message["id"], -32_601, "Method not found")
  end
end

#receive(message) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jazari/mcp/stdio_server.rb', line 39

def receive(message)
  id = message.is_a?(Hash) ? message["id"] : nil
  return jsonrpc_error(id, -32_600, "Invalid Request") unless valid_message?(message)
  return receive_notification(message) unless message.key?("id")

  case message["method"]
  when "initialize" then initialize_response(message)
  when "ping" then empty_response(message)
  else
    return jsonrpc_error(message["id"], -32_002, "Server not initialized") unless @initialized

    operational_response(message)
  end
end

#serve(input: $stdin, output: $stdout) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jazari/mcp/stdio_server.rb', line 20

def serve(input: $stdin, output: $stdout)
  input.each_line do |line|
    next if line.strip.empty?

    message = JSON.parse(line)
    response = receive(message)
    output.puts(JSON.generate(response)) if response
    output.flush
  rescue JSON::ParserError
    output.puts(JSON.generate(jsonrpc_error(nil, -32_700, "Parse error")))
    output.flush
  rescue StandardError
    id = message.is_a?(Hash) ? message["id"] : nil
    output.puts(JSON.generate(jsonrpc_error(id, -32_603, "Internal error")))
    output.flush
  end
  0
end