Class: LLM::MCP::Transport::HTTP
- Inherits:
-
Object
- Object
- LLM::MCP::Transport::HTTP
- Includes:
- Transport::Utils
- Defined in:
- lib/llm/mcp/transport/http.rb
Overview
The LLM::MCP::Transport::HTTP class provides an HTTP transport for LLM::MCP. It sends JSON-RPC messages with HTTP POST requests and buffers response messages for non-blocking reads.
Instance Method Summary collapse
- #initialize(url:, headers: {}, timeout: nil, persistent: false, transport: nil) ⇒ LLM::MCP::Transport::HTTP constructor
-
#read_nonblock ⇒ Hash
Reads the next queued message without blocking.
-
#running? ⇒ Boolean
Returns true when the MCP server connection is alive.
-
#start ⇒ void
Starts the HTTP transport.
-
#stop ⇒ void
Stops the HTTP transport and closes the connection.
-
#write(message) ⇒ void
Writes a JSON-RPC message via HTTP POST.
Methods included from Transport::Utils
Constructor Details
#initialize(url:, headers: {}, timeout: nil, persistent: false, transport: nil) ⇒ LLM::MCP::Transport::HTTP
24 25 26 27 28 29 30 31 |
# File 'lib/llm/mcp/transport/http.rb', line 24 def initialize(url:, headers: {}, timeout: nil, persistent: false, transport: nil) @uri = URI.parse(url) @headers = headers @queue = [] @monitor = Monitor.new @running = false @transport = resolve_transport(host: uri.host, port: uri.port, ssl: uri.scheme == "https", timeout:, persistent:, transport:) end |
Instance Method Details
#read_nonblock ⇒ Hash
Reads the next queued message without blocking.
81 82 83 84 85 86 87 |
# File 'lib/llm/mcp/transport/http.rb', line 81 def read_nonblock lock do raise LLM::MCP::Error, "MCP transport is not running" unless running? raise IO::EAGAINWaitReadable, "no complete message available" if @queue.empty? @queue.shift end end |
#running? ⇒ Boolean
Returns true when the MCP server connection is alive
92 93 94 |
# File 'lib/llm/mcp/transport/http.rb', line 92 def running? @running end |
#start ⇒ void
This method returns an undefined value.
Starts the HTTP transport.
38 39 40 41 42 43 44 |
# File 'lib/llm/mcp/transport/http.rb', line 38 def start lock do raise LLM::MCP::Error, "MCP transport is already running" if running? @queue.clear @running = true end end |
#stop ⇒ void
This method returns an undefined value.
Stops the HTTP transport and closes the connection. This method is idempotent.
50 51 52 53 54 55 56 |
# File 'lib/llm/mcp/transport/http.rb', line 50 def stop lock do return nil unless running? @running = false nil end end |
#write(message) ⇒ void
This method returns an undefined value.
Writes a JSON-RPC message via HTTP POST.
65 66 67 68 69 70 71 72 |
# File 'lib/llm/mcp/transport/http.rb', line 65 def write() raise LLM::MCP::Error, "MCP transport is not running" unless running? req = LLM::Transport::Request.post(uri.request_uri, headers.merge("content-type" => "application/json")) req.body = LLM.json.dump() res = transport.request(req, owner: self) { consume(_1) } res = LLM::Transport::Response.from(res) raise LLM::MCP::Error, "MCP transport write failed with HTTP #{res.code}" unless res.success? end |