Snodo.Transport.Plug (snodo_plug v0.5.0)

Copy Markdown View Source

Optional Plug binding for the stateless Streamable HTTP adapter.

The application supplies an immutable :runtime and a supervised :executor. This Plug starts no listener or global runtime. Install it before body parsers, after the application's authentication Plug. Only conn.assigns[:mcp_auth] supplies Snodo.Context.auth; request headers and JSON never supply identity.

Requests are admitted before entering the bounded executor. Valid cancellation notifications bypass that queue. Cross-request cancellation is opt-in through the trusted :mcp_cancellation_scope assign, combined with the complete auth value; applications must distinguish authenticated client instances in this scope. Anonymous requests cannot cancel each other by guessing request IDs.

init/1 may run once at compile time or on every request (Phoenix's forward does the latter), so the options must not hold a function or a pid that has to outlive compilation. Cancellation keys also carry an :instance, which defaults to {executor, path} and keeps two mounts that share an executor apart; set it explicitly when two mounts share both.

Portable Plug APIs only reveal a disconnect when a write fails. So that a client disconnect cancels a silent handler, as the 2026-07-28 cancellation rules require, a request still running after :disconnect_probe_ms (default 5,000) turns its response into an event stream and sends a keepalive comment every interval; a failed write cancels the work, and the result arrives as the stream's final event. :infinity keeps plain JSON responses. A finite executor enforces :request_timeout (default 30,000 ms) from admission through queue wait and execution, including with an injected executor.

At most :max_subscriptions (default 256) subscriptions/listen streams are open at once. init/1 can run at compile time, so the count is held in the executor, which lives as long as the runtime; Plugs that share an executor share the count. A stream over the limit is closed at its source and the request gets 503.

The request body must arrive within :body_timeout (default 10,000 ms from when this Plug starts reading it) or the request gets 408. The Plug reads the body in slices of at most 64 KiB and checks the deadline between them; each read waits at most :read_timeout (default 5,000) and never past the deadline. Under Bandit a Content-Length body is one socket read per slice, so the deadline bounds the whole body. A request that declares Transfer-Encoding gets 411 without its body being read: an adapter returns from a chunked read only once it has the requested bytes, however many socket reads that takes, so no deadline checked between reads can bound it. Over HTTP/2, Bandit 1.12.5 restarted a read's :read_timeout wait on every DATA frame, even an empty one, so a client that kept sending small or empty frames could hold a read past the deadline. Bandit 1.14.0 gives each read one deadline that empty frames do not extend and that data extends only in proportion to the bytes delivered (Bandit.HTTP2.Stream.read_data/2), so the Plug's deadline bounds the body there too; a test covers this. Use Bandit 1.14 or later for untrusted HTTP/2 clients. After a body error over HTTP/1.x the response closes the connection, so the adapter does not read the rest of the body.

Server Cards

Set :server_card to %{endpoint_url: "https://example.com/mcp"} to publish the public card at the MCP path with /server-card appended. The default is false. Allow anonymous requests to this path through any upstream authentication Plug.

Initialize-era sessions

:sessions names a Snodo.Session.Manager the application supervises, which gives clients of the enabled initialize-era dialects a Snodo.Session as described in Snodo.Transport.StreamableHTTP. It defaults to nil, which keeps the dialects sessionless and answers GET and DELETE with 405. A session is bound to the value of the auth assign. A handler's input requests are written on the request's event stream, GET opens the session's standalone stream with a keepalive at the shorter of :subscription_keepalive_ms and :disconnect_probe_ms, and notifications/cancelled cancels a request in flight on the same session. Only a request that uses a session calls the manager, so 2026-07-28 requests are served while it is down. Sessions live on the node that created them, so several nodes need sticky routing by Mcp-Session-Id.