Class: LLM::MCP::Transport::HTTP

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(url:, headers: {}, timeout: nil) ⇒ LLM::MCP::Transport::HTTP

Parameters:

  • url (String)

    The URL for the MCP HTTP endpoint

  • headers (Hash) (defaults to: {})

    Extra headers to send with requests

  • timeout (Integer, nil) (defaults to: nil)

    The timeout in seconds. Defaults to nil



20
21
22
23
24
25
26
27
28
# File 'lib/llm/mcp/transport/http.rb', line 20

def initialize(url:, headers: {}, timeout: nil)
  @uri = URI.parse(url)
  @use_ssl = @uri.scheme == "https"
  @headers = headers
  @timeout = timeout
  @queue = []
  @monitor = Monitor.new
  @running = false
end

Instance Method Details

#read_nonblockHash

Reads the next queued message without blocking.

Returns:

  • (Hash)

Raises:

  • (LLM::MCP::Error)

    When the transport is not running

  • (IO::WaitReadable)

    When no complete message is available to read



82
83
84
85
86
87
88
# File 'lib/llm/mcp/transport/http.rb', line 82

def read_nonblock
  lock do
    raise LLM::MCP::Error, "MCP transport is not running" unless running?
    raise IO::WaitReadable if @queue.empty?
    @queue.shift
  end
end

#running?Boolean

Returns true when the MCP server connection is alive

Returns:

  • (Boolean)

    Returns true when the MCP server connection is alive



93
94
95
# File 'lib/llm/mcp/transport/http.rb', line 93

def running?
  @running
end

#startvoid

This method returns an undefined value.

Starts the HTTP transport.

Raises:



35
36
37
38
39
40
41
# File 'lib/llm/mcp/transport/http.rb', line 35

def start
  lock do
    raise LLM::MCP::Error, "MCP transport is already running" if running?
    @queue.clear
    @running = true
  end
end

#stopvoid

This method returns an undefined value.

Stops the HTTP transport and closes the connection. This method is idempotent.



47
48
49
50
51
52
53
# File 'lib/llm/mcp/transport/http.rb', line 47

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.

Parameters:

  • message (Hash)

    The JSON-RPC message

Raises:

  • (LLM::MCP::Error)

    When the transport is not running or the HTTP request fails



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/llm/mcp/transport/http.rb', line 62

def write(message)
  raise LLM::MCP::Error, "MCP transport is not running" unless running?
  http = Net::HTTP.start(uri.host, uri.port, use_ssl:, open_timeout: timeout, read_timeout: timeout)
  req = Net::HTTP::Post.new(uri.path, headers.merge("content-type" => "application/json"))
  req.body = LLM.json.dump(message)
  http.request(req) do |res|
    unless Net::HTTPSuccess === res
      raise LLM::MCP::Error, "MCP transport write failed with HTTP #{res.code}"
    end
    read(res)
  end
end