Class: Phronomy::Tool::McpTool::StdioTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/tool/mcp_tool.rb

Overview

Minimal stdio transport implementing a subset of the MCP JSON-RPC protocol. Keeps the child process alive for the lifetime of this transport instance so that session state (registered resources, tool context, etc.) is preserved across multiple calls.

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ StdioTransport

Returns a new instance of StdioTransport.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/phronomy/tool/mcp_tool.rb', line 86

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)
  @mutex = Mutex.new
  @stdin = nil
  @stdout = nil
  @stderr = nil
  @wait_thr = nil
  @stderr_thread = nil
end

Instance Method Details

#call_tool(tool_name, args) ⇒ Object

Call a tool on the MCP server using the tools/call method.

Parameters:

  • tool_name (String)
  • args (Hash)

Returns:

  • (Object)

    the tool result



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/phronomy/tool/mcp_tool.rb', line 135

def call_tool(tool_name, args)
  response = rpc_call("tools/call", {name: tool_name, arguments: args})
  if response["error"]
    err_msg = response.dig("error", "message") || response["error"].to_s
    raise Phronomy::ToolError, "MCP server returned error: #{err_msg}"
  end
  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

#closeObject

Shut down the child process and close its IO streams.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/phronomy/tool/mcp_tool.rb', line 99

def close
  @mutex.synchronize do
    @stdin&.close
    @stdout&.close
    @stderr&.close
    @stdin = nil
    @stdout = nil
    @stderr = nil
  end
  # Join the stderr drain thread and the child process outside the mutex
  # to avoid holding the lock during potentially slow joins.
  @stderr_thread&.join(1)
  @wait_thr&.join(5)
  @stderr_thread = nil
  @wait_thr = nil
end

#fetch_tool(tool_name) ⇒ Hash

Retrieve the tool definition from the server using the MCP tools/list method.

Parameters:

  • tool_name (String)

Returns:

  • (Hash)

    { description:, parameters: }

Raises:

  • (ArgumentError)


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/phronomy/tool/mcp_tool.rb', line 119

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