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) ⇒ 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:



45
46
47
# File 'lib/llm/mcp/transport/http/event_handler.rb', line 45

def on_chunk(event)
  flush if event.chunk == "\n"
end

#on_data(event) ⇒ void

This method returns an undefined value.

Receives one line of SSE data.

Parameters:



36
37
38
# File 'lib/llm/mcp/transport/http/event_handler.rb', line 36

def on_data(event)
  @data << event.value.to_s
end

#on_event(event) ⇒ void

This method returns an undefined value.

Receives the SSE event name.

Parameters:



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

def on_event(event)
  @event = event.value
end