barrel_mcp_http_listener (barrel_mcp v4.0.0)

View Source

Built-in HTTP/1.1 + HTTP/2 server for the MCP HTTP transports.

A small acceptor pool on a single port. Cleartext binds speak HTTP/1.1; TLS binds advertise ALPN [h2, http/1.1] and hand each accepted socket to the negotiated protocol library, so one URL serves both.

What this module owns: the listen socket, the acceptors, the connection and request caps, and the translation between the wire library and barrel_mcp_http_engine. What the h1 and h2 libraries own, through serve_socket/2: the connection process, framing, pipelining order, one process per request or stream, and the 500 for a handler that crashes.

Per request the library invokes our handler in its own process. That process is the translator: it admits the request against max_requests, collects the body, runs the engine in a linked child, and turns the library's stream-reset and close messages into the engine's mcp_disconnect until the child ends.

Summary

Functions

The live acceptor pids of a listener. For tests.

Requests in flight on a listener, admitted and not yet ended.

Start an unsupervised listener registered as Name.

Start a listener linked to the caller, for use as a supervised child. A shutdown from the parent closes the listen socket and takes every connection with it.

Stop a listener by registered name.

Functions

acceptors(Name)

-spec acceptors(atom()) -> [pid()].

The live acceptor pids of a listener. For tests.

connection_init(Transport, Serve, Listener)

in_flight(Name)

-spec in_flight(atom()) -> non_neg_integer().

Requests in flight on a listener, admitted and not yet ended.

start(Name, ListenOpts, EngineConfig)

-spec start(atom(), map(), barrel_mcp_http_engine:config()) -> {ok, pid()} | {error, term()}.

Start an unsupervised listener registered as Name.

For use without the barrel_mcp application, where the caller owns the lifecycle. Under the application, listeners go through barrel_mcp_listener_sup:start_listener/3 instead, which restarts them on a crash and stops them with the application.

ListenOpts: #{port, ip, ssl, acceptors, max_connections, max_requests, max_body_bytes, body_timeout_ms}. ssl is undefined (cleartext) or #{certfile, keyfile, cacertfile => _}. EngineConfig is passed verbatim to barrel_mcp_http_engine:handle/6.

start_link(Name, ListenOpts, EngineConfig)

-spec start_link(atom(), map(), barrel_mcp_http_engine:config()) -> {ok, pid()} | {error, term()}.

Start a listener linked to the caller, for use as a supervised child. A shutdown from the parent closes the listen socket and takes every connection with it.

stop(Name)

-spec stop(atom()) -> ok | {error, not_found | stop_timeout}.

Stop a listener by registered name.