Session & SessionManager

View as MarkdownOpen in Claude

The session management layer tracks active MCP sessions tied to SignalWire call IDs. Each session wraps an MCPClient process and is automatically cleaned up when it expires or when the underlying process dies.

1from signalwire.mcp_gateway import Session, SessionManager

Session

A dataclass representing a single active MCP session. Sessions are created by SessionManager.create_session() and should not be instantiated directly.

Properties

session_id
str

Unique identifier for the session, typically a SignalWire call ID.

service_name
str

Name of the MCP service this session is connected to.

process
MCPClient

The MCPClient instance managing the MCP server process for this session.

created_at
datetime

Timestamp when the session was created.

last_accessed
datetime

Timestamp of the most recent activity on this session. Updated automatically when the session is retrieved via get_session().

timeout
intDefaults to 300

Session timeout in seconds. The session is considered expired when last_accessed + timeout is in the past.

metadata
dict[str, Any]Defaults to {}

Arbitrary metadata attached to the session.

is_expired
bool

Read-only property. Returns True if the session has exceeded its timeout since the last access.

is_alive
bool

Read-only property. Returns True if the underlying MCP client process is still running.

touch

touch() -> None

Update last_accessed to the current time, resetting the expiration clock. Called automatically by get_session().


SessionManager

Manages the full lifecycle of MCP sessions including creation, retrieval, cleanup, and resource limits. Runs a background thread that periodically removes expired or dead sessions.

Methods