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

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/mcp/transport/http/event_handler.rb

Overview

The LLM::MCP::Transport::HTTP::EventHandler class adapts generic server-sent event callbacks into decoded JSON-RPC messages for LLM::MCP::Transport::HTTP. It accumulates event data until a blank line terminates the current event, then parses the payload as JSON and yields it to the callback given at initialization.

Instance Method Summary collapse

Constructor Details

#initialize {|message| ... } ⇒ LLM::MCP::Transport::HTTP::EventHandler

Yield Parameters:

  • message (Hash)

    A decoded JSON-RPC message



17
18
19
20
# File 'lib/llm/mcp/transport/http/event_handler.rb', line 17

def initialize(&on_message)
  @on_message = on_message
  reset
end

Instance Method Details

#on_chunk(event, chunk = nil) ⇒ void

This method returns an undefined value.

The generic event stream parser dispatches one line at a time. A blank line terminates the current SSE event.

Parameters:



47
48
49
# File 'lib/llm/mcp/transport/http/event_handler.rb', line 47

def on_chunk(event, chunk = nil)
  flush if (chunk || event&.chunk || event) == "\n"
end

#on_data(event, chunk = nil) ⇒ void

This method returns an undefined value.

Receives one line of SSE data.

Parameters:



38
39
40
# File 'lib/llm/mcp/transport/http/event_handler.rb', line 38

def on_data(event, chunk = nil)
  @data << (chunk ? event : event.value).to_s
end

#on_event(event, chunk = nil) ⇒ void

This method returns an undefined value.

Receives the SSE event name.

Parameters:



28
29
30
# File 'lib/llm/mcp/transport/http/event_handler.rb', line 28

def on_event(event, chunk = nil)
  @event = chunk ? event : event.value
end