mcp_client/transport

MCP STDIO Transport — real communication with MCP servers via stdin/stdout.

Spawns an external process via Erlang port and communicates via newline-delimited JSON-RPC 2.0 over STDIO. This is the primary transport for local MCP servers.

Usage

let assert Ok(transport) = transport.start("npx", ["-y", "server"], [])
let assert Ok(response) = transport.send_and_receive(transport, "{\"jsonrpc\":\"2.0\",...}", 5000)
transport.stop(transport)

Types

Opaque type for Erlang port references.

pub type Dynamic

Messages for the STDIO transport actor.

pub type StdioMessage {
  SendReceive(
    reply_to: process.Subject(Result(String, String)),
    data: String,
    timeout_ms: Int,
  )
  SendOnly(
    reply_to: process.Subject(Result(Nil, String)),
    data: String,
  )
  NotificationReceived(data: String)
  Stop(reply_to: process.Subject(Nil))
}

Constructors

  • SendReceive(
      reply_to: process.Subject(Result(String, String)),
      data: String,
      timeout_ms: Int,
    )

    Send a JSON-RPC message and wait for response. Any server-sent notifications intercepted during the wait are forwarded to the notification handler after the response is delivered.

  • SendOnly(
      reply_to: process.Subject(Result(Nil, String)),
      data: String,
    )

    Send a JSON-RPC message without waiting (for notifications)

  • NotificationReceived(data: String)

    Internal: a server-sent notification was received.

  • Stop(reply_to: process.Subject(Nil))

    Stop the transport and kill the process

The transport handle.

pub type StdioTransport =
  process.Subject(StdioMessage)

Values

pub fn send_and_receive(
  transport: process.Subject(StdioMessage),
  data: String,
  timeout_ms: Int,
) -> Result(String, String)

Send a JSON-RPC message and wait for a response. The timeout_ms parameter controls how long to wait for the response.

pub fn send_only(
  transport: process.Subject(StdioMessage),
  data: String,
) -> Result(Nil, String)

Send a JSON-RPC notification (no response expected).

pub fn start(
  command: String,
  args: List(String),
  env: List(#(String, String)),
) -> Result(process.Subject(StdioMessage), String)

Start a STDIO transport to an MCP server. Opens an Erlang port to the external process.

pub fn start_with_notifications(
  command: String,
  args: List(String),
  env: List(#(String, String)),
  notification_handler: process.Subject(String),
) -> Result(process.Subject(StdioMessage), String)

Start a STDIO transport with a notification handler. Server-sent notifications (e.g. notifications/tools/list_changed) are forwarded to the given subject as raw JSON strings.

pub fn stop(transport: process.Subject(StdioMessage)) -> Nil

Stop the transport and kill the server process.

Search Document