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

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

Overview

HTTP+SSE transport (2024-11-05). DEPRECATED since 2025-03-26 and reclassified as Deprecated under the MCP feature lifecycle in 2026-07-28 (SEP-2596). Keep working for legacy servers; new integrations should use StreamableHTTP instead.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SSE.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ask/mcp/transport/sse.rb', line 13

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.



11
12
13
# File 'lib/ask/mcp/transport/sse.rb', line 11

def url
  @url
end

Instance Method Details

#on_message(&block) ⇒ Object



29
30
31
# File 'lib/ask/mcp/transport/sse.rb', line 29

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

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  @running
end

#send(message, _headers = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ask/mcp/transport/sse.rb', line 49

def send(message, _headers = {})
  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



74
75
76
# File 'lib/ask/mcp/transport/sse.rb', line 74

def shutdown
  stop
end

#startObject



33
34
35
36
37
38
39
40
41
# File 'lib/ask/mcp/transport/sse.rb', line 33

def start
  require "httpx"

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

#stopObject



43
44
45
46
47
# File 'lib/ask/mcp/transport/sse.rb', line 43

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