Class: VectorMCP::Transport::BaseSessionManager Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_mcp/transport/base_session_manager.rb

Overview

This class is abstract.

Subclass and implement transport-specific methods

Base session manager providing unified session lifecycle management across all transports. This abstract base class defines the standard interface that all transport session managers should implement, ensuring consistent session handling regardless of transport type.

Defined Under Namespace

Classes: Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, session_timeout = 300) ⇒ BaseSessionManager

Initializes a new session manager.

Parameters:

  • transport (Object)

    The parent transport instance

  • session_timeout (Integer) (defaults to: 300)

    Session timeout in seconds (default: 300)



35
36
37
38
39
40
41
42
43
44
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 35

def initialize(transport, session_timeout = 300)
  @transport = transport
  @session_timeout = session_timeout
  @logger = transport.logger
  @sessions = Concurrent::Hash.new
  @cleanup_timer = nil

  start_cleanup_timer if auto_cleanup_enabled?
  logger.debug { "#{self.class.name} initialized with session_timeout: #{session_timeout}" }
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



29
30
31
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 29

def logger
  @logger
end

#session_timeoutObject (readonly)

Returns the value of attribute session_timeout.



29
30
31
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 29

def session_timeout
  @session_timeout
end

#transportObject (readonly)

Returns the value of attribute transport.



29
30
31
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 29

def transport
  @transport
end

Instance Method Details

#active_session_idsArray<String>

Gets all active session IDs.

Returns:

  • (Array<String>)

    Array of session IDs



126
127
128
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 126

def active_session_ids
  @sessions.keys
end

#broadcast_message(message) ⇒ Integer

Broadcasts a message to all sessions that support messaging.

Parameters:

  • message (Hash)

    The message to broadcast

Returns:

  • (Integer)

    Number of sessions the message was sent to



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 190

def broadcast_message(message)
  count = 0
  @sessions.each_value do |session|
    next unless can_send_message_to_session?(session)

    count += 1 if message_sent_to_session?(session, message)
  end

  # Message broadcasted to recipients
  count
end

#cleanup_all_sessionsvoid

This method returns an undefined value.

Cleans up all sessions and stops the cleanup timer.



140
141
142
143
144
145
146
147
148
149
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 140

def cleanup_all_sessions
  logger.info { "Cleaning up all sessions: #{@sessions.size}" }

  @sessions.each_value do |session|
    on_session_terminated(session)
  end

  @sessions.clear
  stop_cleanup_timer
end

#create_session(session_id = nil) ⇒ Session

Creates a new session.

Parameters:

  • session_id (String, nil) (defaults to: nil)

    Optional specific session ID to use

Returns:

  • (Session)

    The newly created session



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 80

def create_session(session_id = nil)
  session_id ||= generate_session_id
  now = Time.now

  # Create VectorMCP session context
  session_context = VectorMCP::Session.new(@transport.server, @transport, id: session_id)

  # Create internal session record with transport-specific metadata
  session = Session.new(
    session_id,
    session_context,
    now,
    now,
    
  )

  @sessions[session_id] = session

  logger.info { "Session created: #{session_id}" }
  on_session_created(session)
  session
end

#find_sessions(criteria = {}) ⇒ Array<Session>

Finds sessions matching criteria.

Parameters:

  • criteria (Hash) (defaults to: {})

    Search criteria

Options Hash (criteria):

  • :created_after (Symbol)

    Time to search after

  • :metadata (Symbol)

    Hash of metadata to match

Returns:

  • (Array<Session>)

    Matching sessions



180
181
182
183
184
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 180

def find_sessions(criteria = {})
  @sessions.values.select do |session|
    matches_criteria?(session, criteria)
  end
end

#get_or_create_session(session_id = nil) ⇒ Session

Gets an existing session or creates a new one.

Parameters:

  • session_id (String, nil) (defaults to: nil)

    The session ID (optional)

Returns:

  • (Session)

    The existing or newly created session



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 64

def get_or_create_session(session_id = nil)
  if session_id
    session = get_session(session_id)
    return session if session

    # If session_id was provided but not found, create with that ID
    return create_session(session_id)
  end

  create_session
end

#get_session(session_id) ⇒ Session?

Gets an existing session by ID.

Parameters:

  • session_id (String)

    The session ID

Returns:

  • (Session, nil)

    The session if found and valid



50
51
52
53
54
55
56
57
58
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 50

def get_session(session_id)
  return nil unless session_id

  session = @sessions[session_id]
  return nil unless session && !session.expired?(@session_timeout)

  session.touch!
  session
end

#get_session_metadata(session_id) ⇒ Hash?

Gets session metadata.

Parameters:

  • session_id (String)

    The session ID

Returns:

  • (Hash, nil)

    Session metadata or nil if session not found



169
170
171
172
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 169

def (session_id)
  session = @sessions[session_id]
  session&.
end

#session_countInteger

Gets the current number of active sessions.

Returns:

  • (Integer)

    Number of active sessions



119
120
121
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 119

def session_count
  @sessions.size
end

#session_metadata_updated?(session_id, metadata) ⇒ Boolean

Updates session metadata.

Parameters:

  • session_id (String)

    The session ID

  • metadata (Hash)

    Metadata to merge

Returns:

  • (Boolean)

    True if session was found and updated



156
157
158
159
160
161
162
163
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 156

def (session_id, )
  session = @sessions[session_id]
  return false unless session

  session..merge!()
  session.touch!
  true
end

#session_terminated?(session_id) ⇒ Boolean

Terminates a session by ID.

Parameters:

  • session_id (String)

    The session ID to terminate

Returns:

  • (Boolean)

    True if session was found and terminated



107
108
109
110
111
112
113
114
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 107

def session_terminated?(session_id)
  session = @sessions.delete(session_id)
  return false unless session

  on_session_terminated(session)
  logger.info { "Session terminated: #{session_id}" }
  true
end

#sessions?Boolean

Checks if any sessions exist.

Returns:

  • (Boolean)

    True if at least one session exists



133
134
135
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 133

def sessions?
  !@sessions.empty?
end