Class: Jazari::Mcp::StdioServer
- Inherits:
-
Object
- Object
- Jazari::Mcp::StdioServer
- 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
-
#initialize(dispatcher:, version: Jazari::VERSION) ⇒ StdioServer
constructor
A new instance of StdioServer.
- #operational_response(message) ⇒ Object
- #receive(message) ⇒ Object
- #serve(input: $stdin, output: $stdout) ⇒ Object
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() case ["method"] when "tools/list" then list_tools() when "tools/call" then call_tool() else jsonrpc_error(["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() id = .is_a?(Hash) ? ["id"] : nil return jsonrpc_error(id, -32_600, "Invalid Request") unless () return receive_notification() unless .key?("id") case ["method"] when "initialize" then initialize_response() when "ping" then empty_response() else return jsonrpc_error(["id"], -32_002, "Server not initialized") unless @initialized operational_response() 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? = JSON.parse(line) response = receive() 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 = .is_a?(Hash) ? ["id"] : nil output.puts(JSON.generate(jsonrpc_error(id, -32_603, "Internal error"))) output.flush end 0 end |