Anubis. Transport. Behaviour behaviour
(anubis_mcp v2.2.0)
Copy Markdown
Defines the behaviour that all transport implementations must follow.
Two kinds of callbacks:
- Process callbacks (
start_link/1,send_message/3,shutdown/1) — the GenServer-oriented I/O contract, required. - Functional callbacks (
transport_init/1,parse/2,encode/2,extract_metadata/2) — pure message framing used when the transport owns a parse state (newline-delimited JSON, SSE chunks, HTTP bodies). Optional; the client checksfunction_exported?/3before using them.
Summary
Callbacks
Encode an MCP message map for this transport's wire format.
Extract transport-specific metadata from raw input.
Parse raw input into decoded MCP message(s).
Returns the list of MCP protocol versions supported by this transport.
Initialize transport-specific state (parse options, configure connection).
Types
@type message() :: String.t()
The JSON-RPC message encoded
@type raw_message() :: binary()
@type reason() :: term() | Anubis.MCP.Error.t()
@type t() :: GenServer.server()
@type transport_state() :: term()
Callbacks
@callback encode(message :: map(), transport_state()) :: {:ok, raw_message(), transport_state()} | {:error, term()}
Encode an MCP message map for this transport's wire format.
Returns the encoded binary ready to be sent.
@callback extract_metadata(raw_input :: term(), transport_state()) :: map()
Extract transport-specific metadata from raw input.
For HTTP, this extracts session_id from headers, request context, etc. For STDIO, this returns basic process metadata.
@callback parse(raw_message() | map(), transport_state()) :: {:ok, [map()], transport_state()} | {:error, term()}
Parse raw input into decoded MCP message(s).
For STDIO, raw input is newline-delimited JSON. For HTTP, raw input is a JSON body string or already-parsed map. For SSE, raw input is SSE event data.
@callback send_message(t(), message(), [opt]) :: :ok | {:error, reason()} when opt: {:timeout, pos_integer()} | {:session_id, String.t()}
@callback start_link(keyword()) :: GenServer.on_start()
@callback supported_protocol_versions() :: [String.t()] | :all
Returns the list of MCP protocol versions supported by this transport.
Examples
iex> MyTransport.supported_protocol_versions()
["2025-03-26", "2025-06-18"]
@callback transport_init(keyword()) :: {:ok, transport_state()} | {:error, term()}
Initialize transport-specific state (parse options, configure connection).