MCP.Transport behaviour (MCP Elixir SDK v1.1.0)

Copy Markdown View Source

Behaviour for MCP transports.

A transport handles the I/O layer for MCP communication — framing, sending, and receiving JSON-RPC messages over a specific channel (stdio, HTTP, etc.).

Transports run as processes (typically GenServers) owned by a client or server. Incoming messages are delivered to the owner process via:

send(owner, {:mcp_message, decoded_map})

Transport closure is signaled via:

send(owner, {:mcp_transport_closed, reason})

Summary

Callbacks

Closes the transport, releasing all resources.

Sends a JSON-RPC message (as a map) through the transport.

Starts the transport process, linked to the caller.

Types

message()

@type message() :: map()

opts()

@type opts() :: keyword()

Callbacks

close(pid)

@callback close(pid :: pid()) :: :ok

Closes the transport, releasing all resources.

send_message(pid, message)

@callback send_message(pid :: pid(), message()) :: :ok | {:error, term()}

Sends a JSON-RPC message (as a map) through the transport.

start_link(opts)

@callback start_link(opts()) :: GenServer.on_start()

Starts the transport process, linked to the caller.

Options must include :owner — the pid that will receive incoming messages.