Class: Phronomy::Tool::McpTool::StdioTransport
- Inherits:
-
Object
- Object
- Phronomy::Tool::McpTool::StdioTransport
- Defined in:
- lib/phronomy/tool/mcp_tool.rb
Overview
Minimal stdio transport implementing a subset of the MCP JSON-RPC protocol. Spawns the server command as a child process and communicates line-by-line.
Instance Method Summary collapse
-
#call_tool(tool_name, args) ⇒ Object
Call a tool on the MCP server using the
tools/callmethod. -
#fetch_tool(tool_name) ⇒ Hash
Retrieve the tool definition from the server using the MCP
tools/listmethod. -
#initialize(command) ⇒ StdioTransport
constructor
A new instance of StdioTransport.
Constructor Details
#initialize(command) ⇒ StdioTransport
Returns a new instance of StdioTransport.
84 85 86 87 88 |
# File 'lib/phronomy/tool/mcp_tool.rb', line 84 def initialize(command) # Split the command string into an argv array so that Open3 executes # it directly without going through the shell, preventing injection. @command = Shellwords.split(command) end |
Instance Method Details
#call_tool(tool_name, args) ⇒ Object
Call a tool on the MCP server using the tools/call method.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/phronomy/tool/mcp_tool.rb', line 109 def call_tool(tool_name, args) response = rpc_call("tools/call", {name: tool_name, arguments: args}) content = response.dig("result", "content") # MCP content is an array of content blocks; extract text blocks. if content.is_a?(Array) texts = content.select { |c| c["type"] == "text" }.map { |c| c["text"] } (texts.length == 1) ? texts.first : texts else content end end |
#fetch_tool(tool_name) ⇒ Hash
Retrieve the tool definition from the server using the MCP tools/list method.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/phronomy/tool/mcp_tool.rb', line 93 def fetch_tool(tool_name) response = rpc_call("tools/list", {}) tools = response.dig("result", "tools") || [] defn = tools.find { |t| t["name"] == tool_name } raise ArgumentError, "Tool #{tool_name.inspect} not found on MCP server #{@command.inspect}" unless defn { description: defn["description"], parameters: parse_schema_params(defn.dig("inputSchema", "properties") || {}) } end |