Class: McpToolkit::Session
- Inherits:
-
Object
- Object
- McpToolkit::Session
- Defined in:
- lib/mcp_toolkit/session.rb
Overview
Server-side session for the MCP Streamable HTTP transport: created on
initialize, identified by an opaque Mcp-Session-Id header the client echoes
on every later request, stored in the configured cache with a sliding TTL.
Cache-backed (rather than the gem's in-process StreamableHTTPTransport) so sessions survive across Puma workers and interoperate with a gateway's client. The cache store + TTL come from McpToolkit.config.
Constant Summary collapse
- CACHE_KEY_PREFIX =
"mcp_toolkit:session:"
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .create!(config: McpToolkit.config) ⇒ Object
- .delete(id, config: McpToolkit.config) ⇒ Object
- .find(id, config: McpToolkit.config) ⇒ Object
Instance Method Summary collapse
-
#initialize(id:) ⇒ Session
constructor
A new instance of Session.
Constructor Details
#initialize(id:) ⇒ Session
Returns a new instance of Session.
43 44 45 |
# File 'lib/mcp_toolkit/session.rb', line 43 def initialize(id:) @id = id end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
41 42 43 |
# File 'lib/mcp_toolkit/session.rb', line 41 def id @id end |
Class Method Details
.create!(config: McpToolkit.config) ⇒ Object
13 14 15 16 17 |
# File 'lib/mcp_toolkit/session.rb', line 13 def self.create!(config: McpToolkit.config) id = SecureRandom.uuid config.cache_store.write(cache_key(id), { created_at: Time.now.to_i }, expires_in: config.session_ttl) new(id:) end |
.delete(id, config: McpToolkit.config) ⇒ Object
30 31 32 33 34 |
# File 'lib/mcp_toolkit/session.rb', line 30 def self.delete(id, config: McpToolkit.config) return false if id.to_s.empty? config.cache_store.delete(cache_key(id)) end |
.find(id, config: McpToolkit.config) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mcp_toolkit/session.rb', line 19 def self.find(id, config: McpToolkit.config) return nil if id.to_s.empty? data = config.cache_store.read(cache_key(id)) return nil unless data # Sliding expiry: bump TTL on every successful lookup. config.cache_store.write(cache_key(id), data, expires_in: config.session_ttl) new(id:) end |