# `Anubis.Server.Transport.StreamableHTTP.Plug`

A Plug implementation for the Streamable HTTP transport.

This plug handles the MCP Streamable HTTP protocol as specified in MCP 2025-03-26.
It provides a single endpoint that supports both GET and POST methods:

- GET: Opens an SSE stream for server-to-client communication
- POST: Handles JSON-RPC messages from client to server
- DELETE: Closes a session

## Usage in Phoenix Router

    pipeline :mcp do
      plug :accepts, ["json"]
    end

    scope "/mcp" do
      pipe_through :mcp
      forward "/", to: Anubis.Server.Transport.StreamableHTTP.Plug, server: :your_server_name
    end

## Configuration Options

- `:server` - The server process name (required)
- `:session_header` - Custom header name for session ID (default: "mcp-session-id")
- `:request_timeout` - Request timeout in milliseconds (default: 30000)
- `:subscriber_metadata` - A 1-arity function `(Plug.Conn.t() -> map())` called
  when an SSE stream is opened. Its return value is stored verbatim as the
  subscriber's opaque metadata (see
  `Anubis.Server.Transport.StreamableHTTP.register_sse_handler/3`) and can later
  be selected on with `send_message_to_subscribers/4` and `handler_count/2`.
  Tag subscribers by tenant, user, feature scope, etc. derived from the request.
  Defaults to `fn _conn -> %{} end`. Use a remote function capture
  (`&MyApp.sse_metadata/1`) so it survives compile-time plug option escaping.

# `call`

Authorizes an HTTP request and dispatches it to its protocol-era binding.

Takes options returned by `init/1` and returns the response connection.
Serves OAuth resource metadata on its well-known path; other requests use
the declared protocol versions, with duplicate version headers rejected
when the server supports the stateless era.

# `init`

Builds transport options from a keyword list with a required `:server`.

Applies the default session header, timeout and subscriber metadata callback.
Raises `KeyError` when `:server` is absent.

---

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