Class: Phronomy::Tool::McpTool::HttpTransport
- Inherits:
-
Object
- Object
- Phronomy::Tool::McpTool::HttpTransport
- 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.
Instance Method Summary collapse
-
#call_tool(tool_name, args) ⇒ Object
Call a tool on the MCP server using MCP
tools/call. -
#fetch_tool(tool_name) ⇒ Hash
Retrieve the tool definition from the server using MCP
tools/list. -
#initialize(base_url, open_timeout: 5, read_timeout: 30) ⇒ HttpTransport
constructor
A new instance of HttpTransport.
Constructor Details
#initialize(base_url, open_timeout: 5, read_timeout: 30) ⇒ HttpTransport
Returns a new instance of HttpTransport.
188 189 190 191 192 |
# File 'lib/phronomy/tool/mcp_tool.rb', line 188 def initialize(base_url, open_timeout: 5, read_timeout: 30) @uri = URI.parse(base_url) @open_timeout = open_timeout @read_timeout = read_timeout end |
Instance Method Details
#call_tool(tool_name, args) ⇒ Object
Call a tool on the MCP server using MCP tools/call.
213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/phronomy/tool/mcp_tool.rb', line 213 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.
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/phronomy/tool/mcp_tool.rb', line 197 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 |