Class: RobotLab::MCP::Transports::SSE

Inherits:
Base
  • Object
show all
Defined in:
lib/robot_lab/mcp/transports/sse.rb

Overview

Server-Sent Events transport for MCP servers

Uses async-http for SSE streaming.

Examples:

transport = SSE.new(url: "http://localhost:8080/sse")

Constant Summary

Constants inherited from Base

Base::DEFAULT_TIMEOUT

Instance Attribute Summary

Attributes inherited from Base

#config, #timeout

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SSE

Creates a new SSE transport.

Parameters:

  • config (Hash)

    transport configuration

Options Hash (config):

  • :url (String)

    SSE server URL



18
19
20
21
22
23
# File 'lib/robot_lab/mcp/transports/sse.rb', line 18

def initialize(config)
  super
  @client = nil
  @connected = false
  @event_queue = []
end

Instance Method Details

#closeself

Close the SSE connection.

Returns:

  • (self)


80
81
82
83
84
85
86
87
88
# File 'lib/robot_lab/mcp/transports/sse.rb', line 80

def close
  return self unless @connected

  @client&.close
  @connected = false
  @client = nil

  self
end

#connectself

Connect to the MCP server via SSE.

Returns:

  • (self)

Raises:

  • (MCPError)

    if async-http gem is not available



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/robot_lab/mcp/transports/sse.rb', line 29

def connect
  return self if @connected

  require "async"
  require "async/http/client"
  require "async/http/endpoint"

  url = @config[:url]

  Async do
    endpoint = Async::HTTP::Endpoint.parse(url)
    @client = Async::HTTP::Client.new(endpoint)
    @connected = true

    # Initialize MCP protocol
    send_initialize
  end

  self
rescue LoadError => e
  raise MCPError, "async-http gem required for SSE transport: #{e.message}"
end

#connected?Boolean

Check if the transport is connected.

Returns:

  • (Boolean)

    true if connected



93
94
95
# File 'lib/robot_lab/mcp/transports/sse.rb', line 93

def connected?
  @connected
end

#send_request(message) ⇒ Hash

Send a JSON-RPC request to the MCP server.

Parameters:

  • message (Hash)

    JSON-RPC message

Returns:

  • (Hash)

    the response

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/robot_lab/mcp/transports/sse.rb', line 57

def send_request(message)
  raise MCPError, "Not connected" unless @connected

  require "async"
  require "async/http/body/writable"

  Async do
    # POST the request
    response = @client.post(
      @config[:url],
      { "Content-Type" => "application/json" },
      [message.to_json]
    )

    # Read response
    body = response.read
    JSON.parse(body, symbolize_names: true)
  end.wait
end