Class: ActionMCP::Client::StreamableHttpTransport

Inherits:
TransportBase
  • Object
show all
Defined in:
lib/action_mcp/client/streamable_http_transport.rb

Overview

StreamableHTTP transport implementation following MCP specification

Defined Under Namespace

Classes: AuthenticationError, ConnectionError

Constant Summary collapse

SSE_TIMEOUT =
10
ENDPOINT_TIMEOUT =
5

Instance Attribute Summary collapse

Attributes inherited from TransportBase

#options, #session_store, #url

Instance Method Summary collapse

Methods inherited from TransportBase

#connected?, #ready?

Methods included from Transport

#connected?, #on_connect, #on_disconnect, #on_error, #on_message, #ready?

Constructor Details

#initialize(url, session_store:, session_id: nil, protocol_version: nil, **options) ⇒ StreamableHttpTransport

Returns a new instance of StreamableHttpTransport.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/action_mcp/client/streamable_http_transport.rb', line 18

def initialize(url, session_store:, session_id: nil, protocol_version: nil, **options)
  super(url, session_store: session_store, **options)
  @session_id = session_id
  @protocol_version = protocol_version || ActionMCP::DEFAULT_PROTOCOL_VERSION
  @negotiated_protocol_version = nil
  @last_event_id = nil
  @buffer = +""
  @current_event = nil
  @reconnect_attempts = 0
  @max_reconnect_attempts = options[:max_reconnect_attempts] || 3
  @reconnect_delay = options[:reconnect_delay] || 1.0

  setup_http_client
end

Instance Attribute Details

#last_event_idObject (readonly)

Returns the value of attribute last_event_id.



16
17
18
# File 'lib/action_mcp/client/streamable_http_transport.rb', line 16

def last_event_id
  @last_event_id
end

#protocol_versionObject (readonly)

Returns the value of attribute protocol_version.



16
17
18
# File 'lib/action_mcp/client/streamable_http_transport.rb', line 16

def protocol_version
  @protocol_version
end

#session_idObject (readonly)

Returns the value of attribute session_id.



16
17
18
# File 'lib/action_mcp/client/streamable_http_transport.rb', line 16

def session_id
  @session_id
end

Instance Method Details

#connectObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/action_mcp/client/streamable_http_transport.rb', line 33

def connect
  log_debug("Connecting via StreamableHTTP to #{@url}")

  # Load session if session_id provided
  load_session_state if @session_id

  # Start SSE stream if server supports it
  start_sse_stream

  # Set ready first, then connected (so transport is ready when on_connect fires)
  set_ready(true)
  set_connected(true)
  log_debug("StreamableHTTP connection established")
  true
rescue StandardError => e
  handle_error(e)
  false
end

#disconnectObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/action_mcp/client/streamable_http_transport.rb', line 52

def disconnect
  return true unless connected?

  log_debug("Disconnecting StreamableHTTP")
  stop_sse_stream
  save_session_state if @session_id
  set_connected(false)
  set_ready(false)
  true
rescue StandardError => e
  handle_error(e)
  false
end

#send_message(message) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/action_mcp/client/streamable_http_transport.rb', line 66

def send_message(message)
  raise ConnectionError, "Transport not ready" unless ready?

  headers = build_post_headers
  json_data = message.is_a?(String) ? message : message.to_json

  log_debug("Sending message via POST")
  response = @http_client.post(@url, json_data, headers)

  handle_post_response(response, message)
  true
rescue StandardError => e
  handle_error(e)
  false
end