Class: Raix::MCP::StdioClient
- Inherits:
-
Object
- Object
- Raix::MCP::StdioClient
- Defined in:
- lib/raix/mcp/stdio_client.rb
Overview
Client for communicating with MCP servers via stdio using JSON-RPC.
Instance Method Summary collapse
-
#call_tool(name, **arguments) ⇒ Object
Executes a tool with given arguments.
-
#close ⇒ Object
Closes the connection to the server.
-
#initialize(*args, env) ⇒ StdioClient
constructor
Creates a new client with a bidirectional pipe to the MCP server.
-
#tools ⇒ Object
Returns available tools from the server.
- #unique_key ⇒ Object
Constructor Details
#initialize(*args, env) ⇒ StdioClient
Creates a new client with a bidirectional pipe to the MCP server.
10 11 12 13 |
# File 'lib/raix/mcp/stdio_client.rb', line 10 def initialize(*args, env) @args = args @io = IO.popen(env, args, "w+") end |
Instance Method Details
#call_tool(name, **arguments) ⇒ Object
Executes a tool with given arguments. Returns text content directly, or JSON-encoded data for other content types.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/raix/mcp/stdio_client.rb', line 26 def call_tool(name, **arguments) result = call("tools/call", name:, arguments:) content = result["content"] return "" if content.nil? || content.empty? # Handle different content formats first_item = content.first case first_item when Hash case first_item["type"] when "text" first_item["text"] when "image" # Return a structured response for images { type: "image", data: first_item["data"], mime_type: first_item["mimeType"] || "image/png" }.to_json else # For any other type, return the item as JSON first_item.to_json end else first_item.to_s end end |
#close ⇒ Object
Closes the connection to the server.
55 56 57 |
# File 'lib/raix/mcp/stdio_client.rb', line 55 def close @io.close end |
#tools ⇒ Object
Returns available tools from the server.
16 17 18 19 20 21 22 |
# File 'lib/raix/mcp/stdio_client.rb', line 16 def tools result = call("tools/list") result["tools"].map do |tool_json| Tool.from_json(tool_json) end end |
#unique_key ⇒ Object
59 60 61 62 |
# File 'lib/raix/mcp/stdio_client.rb', line 59 def unique_key parametrized_args = @args.join(" ").parameterize.underscore Digest::SHA256.hexdigest(parametrized_args)[0..2] end |