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.

Direct Known Subclasses

HttpStream::SessionManager

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)



20
21
22
23
24
25
26
27
28
29
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 20

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.



14
15
16
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 14

def logger
  @logger
end

#session_timeoutObject (readonly)

Returns the value of attribute session_timeout.



14
15
16
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 14

def session_timeout
  @session_timeout
end

#transportObject (readonly)

Returns the value of attribute transport.



14
15
16
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 14

def transport
  @transport
end

Instance Method Details

#active_session_idsArray<String>

Gets all active session IDs.

Returns:

  • (Array<String>)

    Array of session IDs



100
101
102
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 100

def active_session_ids
  @sessions.keys
end

#cleanup_all_sessionsvoid

This method returns an undefined value.

Cleans up all sessions and stops the cleanup timer.



114
115
116
117
118
119
120
121
122
123
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 114

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

Creates a new session.

Parameters:

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

    Optional specific session ID to use

Returns:



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

def create_session(session_id = nil)
  session_id ||= generate_session_id

  session = VectorMCP::Session.new(@transport.server, @transport, id: session_id)
  session..merge!()

  @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



154
155
156
157
158
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 154

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

#get_or_create_session(session_id = nil) ⇒ VectorMCP::Session?

Gets an existing session or creates a new one. When a session_id is provided but not found (expired or unknown), returns nil instead of creating a session with the client-supplied ID — session IDs are always server-generated (prevents session fixation).

Parameters:

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

    The session ID (optional)

Returns:



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

def get_or_create_session(session_id = nil)
  if session_id
    get_session(session_id)
  else
    create_session
  end
end

#get_session(session_id) ⇒ VectorMCP::Session?

Gets an existing session by ID.

Parameters:

  • session_id (String)

    The session ID

Returns:



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

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



143
144
145
146
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 143

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

#session_countInteger

Gets the current number of active sessions.

Returns:

  • (Integer)

    Number of active sessions



93
94
95
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 93

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



130
131
132
133
134
135
136
137
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 130

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



81
82
83
84
85
86
87
88
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 81

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



107
108
109
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 107

def sessions?
  !@sessions.empty?
end