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) ⇒ HttpTransport
constructor
A new instance of HttpTransport.
Constructor Details
#initialize(base_url) ⇒ HttpTransport
Returns a new instance of HttpTransport.
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.
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.
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 |