Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320---title: sessionssidebarTitle: sessions---
# `fastmcp.server.sessions`
Stateless session state: server-side per-user and per-session storage.
Modern (2026-07-28) MCP connections are stateless by construction — everyrequest builds a fresh connection whose in-memory state is discarded when therequest returns. This module gives tools two explicit ways to keep state acrosscalls, both backed by the server's existing state store and both isolated by theauthenticated principal rather than by any client-declared identifier.
- `Session`: async `get`/`set`/`delete`/`clear` over a single dict stored under one key, scoped to a `(principal, session_id)` pair. This is the state-accessor object a handler works with — the value the standalone `get_session(id)` returns and the value injected for a `UserSession` parameter.- `session: UserSession` (injected): a per-user bucket, dependency-injected like `ctx: Context` and keyed by the request's authenticated principal. Requires auth. `UserSession` is the injection annotation; the injected value is a `Session`. It is always available under auth — no `create_session`, no provider, no validation.- `session_id: SessionId` (argument): a required string the agent supplies, resolved with the standalone `await get_session(session_id)`. The id is minted by `create_session`; an id that was never created (or was created under a different principal) is rejected. This validation is the whole guarantee — an unminted id never resolves, so nothing enforces provider registration.- `SessionProvider`: a `Provider` contributing `create_session` / `end_session` tools. Register it with `mcp.add_provider(SessionProvider())` so a tool that takes `session_id` has a way to mint ids; without it, no id can be created, so those tools simply cannot resolve a session.
Isolation is the authenticated principal, not the session id. State keyed by`(principal, session_id)` means a request under principal B can never addressprincipal A's keys, no matter what `session_id` it passes; the id only organizessessions within a principal. Without auth there is no principal wall — a sessionid is a bearer capability and sessions are not a boundary between clients.
## Functions
### `current_principal` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L139" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncurrent_principal() -> str | None```
The authenticated principal for the current request as a compact JSON string.
Returns the `(client_id, issuer, subject)` triple encoded as compact JSON, or`None` on an unauthenticated request. Two users of one OAuth client aredistinct principals whenever the token verifier supplies a subject.
### `session_storage_key` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L164" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonsession_storage_key(principal: str | None, session_id: str) -> str```
The single storage key holding a session's state dict.
Keyed by `(principal, session_id)`: the principal is the isolation wall, theid organizes sessions within it. A session's whole state lives under this onekey as a dict, so one key means one store TTL per session and `end` is asingle delete.
### `session_id_parameter_names` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L343" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonsession_id_parameter_names(fn: Callable[..., object]) -> tuple[str, ...]```
Names of a function's parameters annotated with `SessionId`.
Scans resolved type hints for `Annotated[str, _SessionIdMarker()]` metadata.Returns an empty tuple when the hints cannot be resolved (the function thensimply carries no auto-populated session-id description).
`functools.partial` is unwrapped first, since `get_type_hints` rejects apartial object — FastMCP supports registering a partial as a tool, and itsschema is still built from the underlying function, so its `SessionId`parameters must be detected here too. Parameters the partial has alreadybound — positionally or by keyword — are dropped, matching the tool's actualargument surface (the partial's own signature already reflects this).
### `CurrentSession` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L449" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonCurrentSession() -> Session```
Inject the per-user `Session` for the current authenticated principal.
Rarely written explicitly — a `session: UserSession` parameter is rewrittento this. Provided for parity with `CurrentContext()` when an explicit defaultis preferred.
### `OptionalCurrentSession` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L459" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonOptionalCurrentSession() -> Session | None```
Inject the per-user `Session`, or `None` when the request is unauthenticated.
Rarely written explicitly — a `session: UserSession | None = None` parameteris rewritten to this. Provided for parity with `OptionalCurrentContext()`.
### `create_session` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L468" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncreate_session() -> str```
Create a new session and return its identifier.
Mints an unguessable `uuid4`, records an initial session owned by the currentprincipal, and returns the id as a string. Store it and pass it back as a`session_id` argument on later calls to persist state across a session — onlyan id created this way resolves. State is keyed by the authenticatedprincipal, so the id organizes sessions within a user; on an unauthenticatedconnection the id is the only thing standing between callers, which is why itis unguessable.
### `end_session` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L490" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonend_session(session_id: SessionId) -> str```
End a session and delete all of its state.
Validates the id like any other resolution (an unknown or foreign id isrejected), then deletes the session's key so the id no longer resolves.
## Classes
### `SessionAuthError` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L106" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
An injected `session: UserSession` was requested with no authenticated principal.
Per-user session injection keys off the request's authenticated principal, soit is only meaningful under auth. A tool that needs cross-call state withoutauth should take a `session_id: SessionId` argument instead.
### `InvalidSession` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L125" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
A session id did not resolve to a session created under the current principal.
Raised by `get_session(session_id)` when the id was never created, or wascreated under a different principal. The public message is deliberatelygeneric — the specific reason (which id, which principal) is logged at debuglevel, not returned to the caller, so an attacker cannot distinguish "unknownid" from "belongs to someone else".
### `Session` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L175" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Async accessors over one `(principal, session_id)` bucket of state.
A session's state is a single dict stored under one key. That dict holds userstate in a `state` sub-dict and a small creation marker alongside it, so acreated-but-empty session is still distinguishable from a missing one.`get`/`set`/`delete` read-modify-write the sub-dict; `clear` empties thesub-dict but keeps the session valid; `end` deletes the whole key. Writesnever impose a TTL — retention is entirely the server store's (configure it onthe store you pass to `FastMCP(session_state_store=...)`).
Concurrent writes to one session race on the read-modify-write; session stateis small and typically driven serially by one agent, so this is acceptable.
**Methods:**
#### `id` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L205" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonid(self) -> str | None```
The session's identifier, or `None` for an injected per-user session.
For a session resolved from a `session_id` argument (or minted by`create_session`) this is that id. An injected `UserSession` has nodistinct id — its bucket is the authenticated user — so it is `None`; theinternal principal-derived key is deliberately not exposed here.
#### `get` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L254" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonget(self, key: str, default: Any = None) -> Any```
Return the value for `key`, or `default` when it is not set.
#### `set` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L259" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonset(self, key: str, value: Any) -> None```
Store `value` under `key` in this session (read-modify-write).
Preserves the creation marker: only the user-state sub-dict is touched.
#### `delete` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L270" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythondelete(self, key: str) -> None```
Remove `key` from this session, if present (preserves the marker).
#### `clear` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L281" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonclear(self) -> None```
Empty the session's user state but keep the session valid.
The user-state sub-dict is reset to empty while the creation marker staysin place, so a cleared session still resolves through `get_session`.To invalidate a session entirely, use `end` (what `end_session` calls).
#### `end` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L294" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonend(self) -> None```
Invalidate the session — delete its one key and all of its state.
After this the id no longer resolves through `get_session`. This iswhat `end_session` calls; `clear` only empties state and keeps the session.
### `UserSession` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L303" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Annotation marker for the injected per-user session.
A `session: UserSession` parameter is **dependency-injected** like`ctx: Context`: keyed by the request's authenticated principal, excluded fromthe input schema, and requiring auth (it raises `SessionAuthError` with noprincipal). It doubles as the injection *annotation* and the injectedtype — the value a handler receives is a `UserSession`, which subclasses`Session`, so `await session.get(...)`, `.set`, `.delete`, and `.clear` allwork exactly as on any other `Session`.
Unlike `session_id: SessionId`, the per-user bucket needs no `create_session`,no `SessionProvider`, and no validation — it is always available under auth,keyed directly by the caller's identity.
```pythonfrom fastmcp.server.sessions import UserSession
@mcp.toolasync def remember(fact: str, session: UserSession) -> str: await session.set("fact", fact) return "noted"```
Subclasses `Session` only so the framework's type-based injection detector cankey off it; it adds no behavior of its own.
### `SessionProvider` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/sessions.py#L501" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Provider contributing the session lifecycle tools.
Register it whenever a tool declares a `session_id: SessionId` argument:
```pythonfrom fastmcp.server.sessions import SessionProvider
mcp.add_provider(SessionProvider())```
It registers two tools:
- `create_session()` mints an unguessable `uuid4`, records the session, and returns the id.- `end_session(session_id)` invalidates that session and deletes its state.
It owns no storage (session state lives in the server's configured`session_state_store`) and imposes no TTL (retention is the store's). Itexists to mint and end owned session ids. Registration is not enforced: withno provider, no id can be created, so every `get_session(...)` rejects —a `session_id` tool without a provider simply cannot resolve a session.