Snodo.Transport.StreamableHTTP.Server (snodo v0.5.0)

Copy Markdown View Source

Small dependency-free HTTP/1.1 listener for Snodo.Transport.StreamableHTTP.

It is intentionally a binding, not a web framework. An accepted connection serves further requests after a complete response (HTTP/1.1 keep-alive), bounded by :keepalive_timeout and :max_keepalive_requests. It closes after an error response, a stream, Connection: close, or an HTTP/1.0 request. Header admission happens in the connection process; admitted MCP work runs through the reusable bounded Snodo.Server.Executor. A peer disconnect while work is pending cancels that execution and tears down its reply owner.

:request_timeout bounds queue wait plus execution and defaults to 30,000 ms, including with an injected executor. Progress does not extend the deadline. :max_queue_wait sets an optional shorter queue cap for an owned executor, defaulting to :infinity. Configure an injected executor directly. Body reads and socket writes have their own transport bounds.

Connection and stream bounds, each a positive integer:

  • :max_connections - the most connections open at once, default 1,024. A connection accepted at the limit is closed without being read. Keep it below the process's open file limit (ulimit -n), which also covers files, ports, and outbound sockets. If accepting fails because descriptors or buffers run out, the listener logs a warning and retries every 100 ms instead of stopping.
  • :backlog - the listen socket's queue of connections not yet accepted, default 1,024, matching :max_connections. The kernel caps it, on Linux at net.core.somaxconn, and decides what happens to connection attempts that overflow it. On one Linux machine, a burst of 1,000 connections against a backlog of 128 had clients retrying after about a second, before any other limit applied.
  • :head_timeout - milliseconds from accept until the request head must be complete, default 10,000. :read_timeout (default 5,000) still bounds each read; this deadline bounds the whole head, so a client that sends it a byte at a time is closed when the deadline passes.
  • :keepalive_timeout - milliseconds an idle connection waits for the next request after a response, default 5,000. The :head_timeout for that request starts when its first bytes arrive.
  • :max_keepalive_requests - the most requests one connection serves, default 100. The response to the last one carries Connection: close.
  • :request_gate_timeout - milliseconds allowed for a request gate to check the head, default 10,000. A timed-out gate gets a 504 response.
  • :body_timeout - milliseconds from the end of the head, or from gate admission when a gate is configured, until the request body must be read, default 10,000. Each body read returns what has arrived and waits at most :read_timeout; this deadline bounds the whole body.
  • :max_subscriptions - the most subscriptions/listen streams open at once, default 256. The executor holds one count per listener and returns a slot when the connection serving that stream exits. A stream over the limit is closed at its source and the request gets 503.
  • :drain_timeout - milliseconds the listener waits on shutdown for open connections to finish, default 5,000.

Server Cards

Set :server_card to %{endpoint_url: "https://example.com/mcp"} to serve public discovery metadata under the MCP path at /server-card. The default is false. The card route uses wildcard CORS, applies :allowed_hosts, and runs before the request gate.

Initialize-era sessions

sessions: true, or a keyword list of Snodo.Session.Manager options (:max_sessions, :idle_timeout, :registry, :principal), gives clients of the enabled initialize-era dialects a Snodo.Session, as described in Snodo.Transport.StreamableHTTP. The listener starts its own manager and stops it on shutdown. Sessions are off by default: the dialects then run sessionless and GET and DELETE get 405. With a :request_gate, the identity it returns is the auth value a session is bound to.

With sessions on, a handler's input requests are written as SSE events on the request's response stream, and GET opens the session's standalone stream, which sends a keepalive comment every :subscription_keepalive_ms. A notifications/cancelled for a request in flight on the session cancels it. Sessions live on this node, so several listeners behind a load balancer need sticky routing by Mcp-Session-Id.

Shutdown

The listener drains when it terminates, including when its supervisor stops it:

  1. The listening socket closes, so new connections are refused.
  2. A connection whose request head or body is still arriving gets 503 and closes. A request read in full before the drain began is admitted and runs to its response.
  3. Each open subscriptions/listen stream gets its successful completion response, its source is closed with reason :shutdown, and the connection closes.
  4. Connections still open when :drain_timeout passes are killed, which cancels their executor work and closes their subscription sources.
  5. An executor the listener started is stopped. An application-owned executor keeps running.

child_spec/1 sets the child's :shutdown to :drain_timeout plus 5,000 ms, the default worker shutdown, so the supervisor does not kill the listener during the drain. A child spec that overrides :shutdown must keep it above :drain_timeout. A listener that is killed does not drain.

Applications that already run Plug, Bandit, or Cowboy can translate their request into Snodo.Transport.StreamableHTTP.Request and use the pure adapter directly instead of starting this listener.

The listener has no built-in authentication. Set :request_gate to a {module, options} implementing Snodo.Transport.StreamableHTTP.RequestGate to serve discovery routes and authenticate requests before reading their bodies. A verified identity returned by the gate is available to Snodo.Authorization. snodo_oauth provides an OAuth resource-server gate.

The listener serves plaintext HTTP. It warns when bound outside loopback, including when a request gate is configured. Bind it privately behind a TLS reverse proxy, or run Snodo.Transport.Plug in an HTTP server with TLS.

Summary

Functions

Returns the bound IP, actual port, and configured MCP endpoint path.

Returns a worker child spec whose :shutdown exceeds :drain_timeout by 5,000 ms. An invalid :drain_timeout is rejected when the listener starts.

Returns a URL for a listener bound to a local IPv4 or IPv6 address.

Functions

address(server)

Returns the bound IP, actual port, and configured MCP endpoint path.

child_spec(init_arg)

Returns a worker child spec whose :shutdown exceeds :drain_timeout by 5,000 ms. An invalid :drain_timeout is rejected when the listener starts.

url(server)

@spec url(GenServer.server()) :: String.t()

Returns a URL for a listener bound to a local IPv4 or IPv6 address.