session

package
v0.15.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package session provides session management for the MCP data platform. It defines the Store interface for session persistence and the Session type that represents an active client connection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AwareHandler

type AwareHandler struct {
	// contains filtered or unexported fields
}

AwareHandler wraps an HTTP handler to manage MCP sessions against an external Store. It is used when the SDK runs in stateless mode to provide session persistence (e.g. for zero-downtime restarts).

func NewAwareHandler

func NewAwareHandler(inner http.Handler, cfg HandlerConfig) *AwareHandler

NewAwareHandler creates a handler that manages sessions externally.

func (*AwareHandler) ServeHTTP

func (h *AwareHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches the request based on session state.

type HandlerConfig

type HandlerConfig struct {
	Store Store
	TTL   time.Duration
}

HandlerConfig configures an AwareHandler.

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore implements Store using an in-memory map with TTL-based expiration.

func NewMemoryStore

func NewMemoryStore(ttl time.Duration) *MemoryStore

NewMemoryStore creates a new in-memory session store.

func (*MemoryStore) Cleanup

func (s *MemoryStore) Cleanup(_ context.Context) error

Cleanup removes expired sessions.

func (*MemoryStore) Close

func (s *MemoryStore) Close() error

Close stops the cleanup goroutine and waits for it to exit. It is safe to call Close even if StartCleanupRoutine was never called.

func (*MemoryStore) Create

func (s *MemoryStore) Create(_ context.Context, sess *Session) error

Create persists a new session.

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(_ context.Context, id string) error

Delete removes a session.

func (*MemoryStore) Get

func (s *MemoryStore) Get(_ context.Context, id string) (*Session, error)

Get retrieves a session by ID. Returns nil, nil if not found or expired.

func (*MemoryStore) List

func (s *MemoryStore) List(_ context.Context) ([]*Session, error)

List returns all non-expired sessions.

func (*MemoryStore) StartCleanupRoutine

func (s *MemoryStore) StartCleanupRoutine(interval time.Duration)

StartCleanupRoutine starts a background goroutine that periodically removes expired sessions. The goroutine is stopped when Close is called.

func (*MemoryStore) Touch

func (s *MemoryStore) Touch(_ context.Context, id string) error

Touch updates LastActiveAt and extends ExpiresAt by the store's TTL.

func (*MemoryStore) UpdateState

func (s *MemoryStore) UpdateState(_ context.Context, id string, state map[string]any) error

UpdateState merges state into the session's State map.

type Session

type Session struct {
	// ID is the unique session identifier.
	ID string

	// UserID identifies the session owner. For authenticated sessions this is
	// a hash of the bearer token; for anonymous sessions it is empty.
	UserID string

	// CreatedAt is when the session was established.
	CreatedAt time.Time

	// LastActiveAt is the most recent activity timestamp.
	LastActiveAt time.Time

	// ExpiresAt is when the session expires if not touched.
	ExpiresAt time.Time

	// State holds extensible session data (e.g. enrichment dedup state).
	State map[string]any
}

Session represents an active client session.

type Store

type Store interface {
	// Create persists a new session.
	Create(ctx context.Context, s *Session) error

	// Get retrieves a session by ID. Returns nil, nil if not found or expired.
	Get(ctx context.Context, id string) (*Session, error)

	// Touch updates LastActiveAt and extends ExpiresAt by the store's TTL.
	Touch(ctx context.Context, id string) error

	// Delete removes a session.
	Delete(ctx context.Context, id string) error

	// List returns all non-expired sessions.
	List(ctx context.Context) ([]*Session, error)

	// UpdateState merges state into the session's State map.
	UpdateState(ctx context.Context, id string, state map[string]any) error

	// Cleanup removes expired sessions.
	Cleanup(ctx context.Context) error

	// Close stops background routines and releases resources.
	Close() error
}

Store defines the interface for session persistence.

Directories

Path Synopsis
Package postgres provides PostgreSQL storage for sessions.
Package postgres provides PostgreSQL storage for sessions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL