Class: Ask::MCP::Transport::SSE
- Inherits:
-
Object
- Object
- Ask::MCP::Transport::SSE
- Defined in:
- lib/ask/mcp/transport/sse.rb
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url, options = {}) ⇒ SSE
constructor
A new instance of SSE.
- #on_message(&block) ⇒ Object
- #running? ⇒ Boolean
- #send(message) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
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, = {}) @url = url @options = @running = false @buffer = +"" @message_handlers = [] @http = nil @response = nil @post_url = nil @reconnect_delay = [:reconnect_delay] || 1.0 @max_reconnect_delay = [:max_reconnect_delay] || 30.0 @reconnect_jitter = [:reconnect_jitter] || 0.1 @max_retries = [:max_retries] || 5 @retries = 0 end |
Instance Attribute Details
#url ⇒ Object (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 (&block) @message_handlers << block end |
#running? ⇒ 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() require "httpx" data = .is_a?(String) ? : .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.}" end |
#shutdown ⇒ Object
70 71 72 |
# File 'lib/ask/mcp/transport/sse.rb', line 70 def shutdown stop end |
#start ⇒ Object
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 |
#stop ⇒ Object
39 40 41 42 43 |
# File 'lib/ask/mcp/transport/sse.rb', line 39 def stop @running = false @response&.close @http&.close end |