Class: Ask::MCP::Transport::SSE

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/mcp/transport/sse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ SSE

Returns a new instance of SSE.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ask/mcp/transport/sse.rb', line 9

def initialize(url, options = {})
  @url = url
  @options = options
  @running = false
  @buffer = +""
  @message_handlers = []
  @http = nil
  @response = nil
  @post_url = nil
  @reconnect_delay = options[:reconnect_delay] || 1.0
  @max_reconnect_delay = options[:max_reconnect_delay] || 30.0
  @reconnect_jitter = options[:reconnect_jitter] || 0.1
  @max_retries = options[:max_retries] || 5
  @retries = 0
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/ask/mcp/transport/sse.rb', line 7

def url
  @url
end

Instance Method Details

#on_message(&block) ⇒ Object



25
26
27
# File 'lib/ask/mcp/transport/sse.rb', line 25

def on_message(&block)
  @message_handlers << block
end

#running?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/ask/mcp/transport/sse.rb', line 66

def running?
  @running
end

#send(message) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ask/mcp/transport/sse.rb', line 45

def send(message)
  require "httpx"

  data = message.is_a?(String) ? message : message.to_json
  target = @post_url || @url

  headers = { "Content-Type" => "application/json" }
  headers.merge!(@options[:headers]) if @options[:headers]

  http = HTTPX.with(headers:)
  response = http.post(target, body: data)

  unless response.status == 200 || response.status == 202 || response.status == 204
    raise ConnectionError, "Failed to send message: #{response.status} #{response.body.to_s}"
  end

  response
rescue HTTPX::Error => e
  raise ConnectionError, "Failed to send message: #{e.message}"
end

#shutdownObject



70
71
72
# File 'lib/ask/mcp/transport/sse.rb', line 70

def shutdown
  stop
end

#startObject



29
30
31
32
33
34
35
36
37
# File 'lib/ask/mcp/transport/sse.rb', line 29

def start
  require "httpx"

  @running = true
  @retries = 0
  @post_url = nil
  connect_stream
  self
end

#stopObject



39
40
41
42
43
# File 'lib/ask/mcp/transport/sse.rb', line 39

def stop
  @running = false
  @response&.close
  @http&.close
end