Class: LLM::MCP::Transport::HTTP
- Inherits:
-
Object
- Object
- LLM::MCP::Transport::HTTP
- 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.
Defined Under Namespace
Classes: EventHandler
Instance Method Summary collapse
- #initialize(url:, headers: {}, timeout: nil, 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.
Constructor Details
#initialize(url:, headers: {}, timeout: nil, transport: nil) ⇒ LLM::MCP::Transport::HTTP
22 23 24 25 26 27 28 29 |
# File 'lib/llm/mcp/transport/http.rb', line 22 def initialize(url:, headers: {}, timeout: nil, transport: nil) @uri = URI.parse(url) @headers = headers @transport = resolve_transport(transport, timeout:) @queue = [] @monitor = Monitor.new @running = false end |
Instance Method Details
#read_nonblock ⇒ Hash
Reads the next queued message without blocking.
79 80 81 82 83 84 85 |
# File 'lib/llm/mcp/transport/http.rb', line 79 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
90 91 92 |
# File 'lib/llm/mcp/transport/http.rb', line 90 def running? @running end |
#start ⇒ void
This method returns an undefined value.
Starts the HTTP transport.
36 37 38 39 40 41 42 |
# File 'lib/llm/mcp/transport/http.rb', line 36 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.
48 49 50 51 52 53 54 |
# File 'lib/llm/mcp/transport/http.rb', line 48 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.
63 64 65 66 67 68 69 70 |
# File 'lib/llm/mcp/transport/http.rb', line 63 def write() raise LLM::MCP::Error, "MCP transport is not running" unless running? req = Net::HTTP::Post.new(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 |