# `Snodo.Session`
[🔗](https://github.com/joshrotenberg/snodo/blob/v0.5.0/lib/snodo/session.ex#L1)

One initialize-era session: the state a 2025-11-25 or 2025-06-18 client
shares with the server between `initialize` and the end of the session.

Sessions are opt-in. A transport given the `sessions:` option starts one
process per session: `Snodo.Transport.StreamableHTTP.Server` and
`Snodo.Transport.Plug` for each `initialize` (through
`Snodo.Session.Manager`, which issues the `Mcp-Session-Id`), and
`Snodo.Transport.Stdio` for its connection, which needs no ID. Without the
option the dialects run sessionless, as before. 2026-07-28 requests never
use a session.

The process holds the negotiated version, the client's capabilities and
information, the principal that created the session, the log level set with
`logging/setLevel`, the resources the client subscribed to, the standalone
stream, the server's requests waiting for the client's answers, and the
client's requests in flight, so `notifications/cancelled` can reach them.

A handler written for 2026-07-28 input requests serves these clients
unchanged: it returns `Snodo.Result.input_required/1` with
`Snodo.Elicitation`, `Snodo.Sampling`, or `Snodo.Roots` requests, and the
transport sends each one to the client as its own JSON-RPC request on the
original request's response stream, waits for the answers, and calls the
handler again with them in `Snodo.Context.input_responses`. The
request's deadline covers every round.

With a `:subscription_source` on the runtime, the session opens it for the
list changes the server advertises and the resources the client subscribes
to with `resources/subscribe`, and sends each event on the standalone
stream: the `GET` stream over HTTP, or the connection over stdio. An event
that arrives while no standalone stream is open is dropped.

A transport passes this snapshot to the dialect. On a process-backed
session it becomes `Snodo.Context.session`. The in-process client uses a
snapshot with a nil `pid` to carry initialize capabilities and client
identity.

# `t`

```elixir
@type t() :: %Snodo.Session{
  client_capabilities: map(),
  client_info: map() | nil,
  id: String.t() | nil,
  log_level: Snodo.Logging.level() | nil,
  pid: pid() | nil,
  protocol_version: String.t() | nil,
  ready?: boolean()
}
```

# `attach_stream`

```elixir
@spec attach_stream(pid(), pid()) :: :ok | {:error, :conflict | :closed}
```

Makes `stream` the session's standalone stream.

For an HTTP stack that serves the `{:standalone, session}` result of
`Snodo.Transport.StreamableHTTP.prepare/3` itself. The session sends
`stream` `{:mcp_session_message, session_pid, ref, message}` for each
notification, and sends the next one only after `stream` replies with
`send(session_pid, {:mcp_session_written, ref, :ok | {:error, reason}})`.
The session watches `stream` and detaches it when it exits. Returns
`{:error, :conflict}` when the session already has a standalone stream,
which HTTP answers with 409, and `{:error, :closed}` when the session has
ended.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
