duck.contrib.mcp.session

MCP session store.

Sessions are stored entirely in the configured InMemoryCache, which provides LRU eviction and TTL expiration automatically.

Module Contents

Classes

SessionStore

Represents a single MCP session.

Data

DEFAULT_SESSION_REGISTRY

API

duck.contrib.mcp.session.DEFAULT_SESSION_REGISTRY

‘InMemoryCache(…)’

class duck.contrib.mcp.session.SessionStore(cache: Optional[duck.utils.caching.InMemoryCache] = None, session_id: Optional[str] = None, ttl: int = 3600)[source]

Bases: dict

Represents a single MCP session.

Session data is cached in-memory using the provided cache instance.

Initialization

Initialize a session.

Parameters:
  • cache – Shared in-memory cache.

  • session_id – Existing session ID. A new one is generated if omitted.

  • ttl – Session lifetime in seconds.

__contains__(key)[source]
__delitem__(key)[source]

Delete a key, marking the session as modified.

__getitem__(key)[source]
__repr__() str[source]
__setitem__(key, value)[source]

Set a key’s value, marking the session as modified.

__slots__

(‘_session_id’, ‘_cache’, ‘_ttl’, ‘_loaded’, ‘_modified’)

__str__() str[source]
assign_new_session_id() str[source]

Creates a new session with a new session id.

clear()[source]

Remove all data from the session, marking the session as modified.

delete() None[source]

Delete the session.

static ensure_session_loaded(method)[source]

Decorator which ensures that the session is loaded.

exists() bool[source]

Return whether the session exists.

static generate_session_id() str[source]

Generate a new session identifier.

load() dict[source]

Load session data from cache.

property loaded: bool
property modified: bool

Whether the session data has changed since it was last loaded or saved.

needs_update() bool[source]

Return whether the session has pending changes that haven’t been saved yet.

pop(*a, **kw)[source]

Remove and return a key’s value, marking the session as modified.

popitem()[source]

Remove and return a (key, value) pair, marking the session as modified.

save() None[source]

Persist the session to cache.

property session_id: Optional[str]
setdefault(*a, **kw)[source]

Set a default value for a key if it’s not already present, marking the session as modified.

touch() None[source]

Refresh the session TTL.

update(*a, **kw)[source]

Update multiple keys at once, marking the session as modified.