Anubis.Transport behaviour (anubis_mcp v1.0.0)

Copy Markdown

Functional behaviour for MCP transport implementations.

Unlike Anubis.Transport.Behaviour (which defines a GenServer-oriented transport interface), this behaviour defines a functional transport interface for parsing, encoding, sending messages, and extracting metadata.

Transport modules implementing this behaviour provide pure functions for message framing — the actual I/O process (Port, Plug conn, SSE handler) already exists and calls these functions internally.

Adapters

Example

{:ok, state} = MyTransport.transport_init(opts)
{:ok, message, state} = MyTransport.parse(raw_data, state)
{:ok, encoded, state} = MyTransport.encode(response, state)

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).

Initialize transport-specific state (parse options, configure connection).

Types

raw_message()

@type raw_message() :: binary()

transport_state()

@type transport_state() :: term()

Callbacks

encode(message, transport_state)

@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.

extract_metadata(raw_input, transport_state)

@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.

parse(arg1, transport_state)

@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.

transport_init(keyword)

@callback transport_init(keyword()) :: {:ok, transport_state()} | {:error, term()}

Initialize transport-specific state (parse options, configure connection).