SuperPi.MCP.Sessions (super_pi v0.1.0)

Copy Markdown View Source

Sessions of the Streamable HTTP server (SuperPi.MCP.HTTPPlug): the id a client carries in Mcp-Session-Id, and the connection state that SuperPi.MCP.Server.handle_mcp_line/2 threads.

The table lives on the node that answered initialize. A request that reaches another node finds no session and gets 404, which a conforming client answers by starting a new session, so a cluster needs no shared session store (sticky routing only saves the round trip).

A session expires after mcp_session_ttl_ms without a request (default one hour). At most mcp_max_sessions exist at once (default 1000), so a client that only calls initialize cannot fill memory.

Summary

Functions

Drops every session. For tests.

Number of live sessions.

Starts a session with state. Returns {:ok, id}, or {:error, :full} at the limit.

Ends a session. Returns :ok, or :error when there was none.

The state of a live session. A request counts as use, so an active session does not expire.

Stores the next state of a live session. A session that ended meanwhile stays ended.

Functions

clear()

Drops every session. For tests.

count()

Number of live sessions.

create(state)

@spec create(map()) :: {:ok, String.t()} | {:error, :full}

Starts a session with state. Returns {:ok, id}, or {:error, :full} at the limit.

delete(id)

@spec delete(term()) :: :ok | :error

Ends a session. Returns :ok, or :error when there was none.

fetch(id)

@spec fetch(term()) :: {:ok, map()} | :error

The state of a live session. A request counts as use, so an active session does not expire.

put(id, state)

@spec put(String.t(), map()) :: :ok

Stores the next state of a live session. A session that ended meanwhile stays ended.