Class: Phronomy::Tool::McpTool::HttpTransport

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

Overview

HTTP/HTTPS transport implementing JSON-RPC over HTTP with SSE support.

Sends JSON-RPC POST requests to the MCP server endpoint. Accepts both plain JSON responses (Content-Type: application/json) and Server-Sent Events streams (Content-Type: text/event-stream), covering both the 2024-11-05 and 2025-03-26 MCP HTTP transport specifications.

Examples:

tool = Phronomy::Tool::McpTool.from_server(
  "http://localhost:8080/mcp",
  tool_name: "weather_lookup"
)

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ HttpTransport

Returns a new instance of HttpTransport.

Parameters:



157
158
159
# File 'lib/phronomy/tool/mcp_tool.rb', line 157

def initialize(base_url)
  @uri = URI.parse(base_url)
end

Instance Method Details

#call_tool(tool_name, args) ⇒ Object

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

Parameters:

  • tool_name (String)
  • args (Hash)

Returns:

  • (Object)

    the tool result



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/phronomy/tool/mcp_tool.rb', line 180

def call_tool(tool_name, args)
  response = rpc_call("tools/call", {name: tool_name, arguments: args})
  content = response.dig("result", "content")

  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 MCP tools/list.

Parameters:

  • tool_name (String)

Returns:

  • (Hash)

    { description:, parameters: }

Raises:

  • (ArgumentError)


164
165
166
167
168
169
170
171
172
173
174
# File 'lib/phronomy/tool/mcp_tool.rb', line 164

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 #{@uri}" unless defn

  {
    description: defn["description"],
    parameters: parse_schema_params(defn.dig("inputSchema", "properties") || {})
  }
end