Resource

session

A bucket-backed session keyed by the Mcp-Session-Id HTTP header

resource session;

F id

id: func() -> string;

Get the session ID

This ID should be returned to clients in the Mcp-Session-Id header so they can resume the session in future requests.

F get

get: func(key: string) -> result​<option​<list​<u8>>, session-error>;

Get a persistent session value by key

Returns None if the key doesn't exist. Session data persists across HTTP requests as long as the session exists.

F set

set: func(key: string, value: list​<u8>) -> result​<_, session-error>;

Set a persistent session value by key

The value is immediately persisted to the underlying key-value store. Overwriting an existing key replaces the previous value.

F elicit

elicit: func(client: borrow​<output-stream>, elicitation: elicit-request) -> result​<future-elicit-result, session-error>;

Elicit information from the client

This sends a request to the client asking for specific information (roots, sampling, etc.). The result is a future that can be polled to check when the client responds.

The request is sent immediately on the provided output stream. The future should be stored and polled later to retrieve the response.

https://spec.modelcontextprotocol.io/specification/2025-06-18/client/elicitation

F terminate

terminate: func(reason: option​<string>) -> result​<_, session-error>;

Idempotently terminate the session, optionally providing a reason

Termination marks the session as ended but does NOT delete session data. Clients can detect termination and stop sending requests. Use delete() to remove all session data.

This operation is idempotent - calling multiple times has no effect.

F is-terminated

is-terminated: func() -> result​<bool, session-error>;

Check if the session is terminated

Returns true if terminate() was previously called on this session. Terminated sessions can still read data but should not accept new operations.

F initialize

initialize: func(bucket: bucket) -> result​<session, session-error>;

Initialize a new session in the given bucket

A cryptographically unique session ID is generated automatically. The caller should extract the ID and return it to the client via the Mcp-Session-Id response header.

The bucket is consumed and owned by the session resource.

F open

open: func(bucket: bucket, id: string) -> result​<session, session-error>;

Open an existing session by ID in the given bucket

This is called when a client provides an Mcp-Session-Id header to resume a previous session. Returns an error if the session ID doesn't exist in the bucket.

The bucket is consumed and owned by the session resource.

F close

close: func(session: session) -> bucket;

Close the session and return the underlying bucket

This releases the session resource but keeps all data in the bucket. The returned bucket can be used to create new sessions or passed to other components.

F delete

delete: func(session: session) -> result​<bucket, tuple​<bucket, session-error>>;

Delete the session and all associated data in the bucket

This permanently removes the session ID and all key-value pairs. The bucket is returned for reuse. If deletion fails, returns both the bucket and the error so the caller can recover.