relay/stdio

Serves an MCP server over standard input and output.

serve(server, context) reads newline-delimited JSON-RPC frames from standard input until it closes, writes replies to standard output, and keeps standard output free of anything else. It blocks the calling process, which is usually main. serve_with takes a Config for the read chunk size and the runtime limits (relay/runtime.config). The stdio transport enforces no authorization: run it only under a trusted parent process. A client launches such a server with relay/client.stdio.

import relay/server
import relay/stdio

pub fn main() {
  let assert Ok(Nil) = stdio.serve(server.new([]), Nil)
}
SettingDefaultSetter
read chunk4 KiBwith_chunk_size
frame, depth, handler limitsrelay/runtime.config()with_runtime

Types

Stdio settings. Build with config() and the with_* setters.

pub opaque type Config

Why serve stopped with an error.

pub type StdioError {
  InvalidChunkSize(size: Int)
  InvalidRuntimeConfig(field: runtime.ConfigField)
  ReadFailed
  StdoutBroken
  StartupFailed
  InvalidTrailingData(bytes: BitArray)
}

Constructors

  • InvalidChunkSize(size: Int)
  • InvalidRuntimeConfig(field: runtime.ConfigField)
  • ReadFailed

    Standard input could not be switched to binary mode or read.

  • StdoutBroken

    Standard output was closed or failed while writing a reply.

  • StartupFailed
  • InvalidTrailingData(bytes: BitArray)

    Standard input ended in the middle of a frame.

Values

pub fn config() -> Config

A 4 KiB read chunk and the default runtime limits.

pub fn describe_error(error: StdioError) -> String

A one-line description of a stdio error.

pub fn serve(
  server: server.Server(context),
  context: context,
) -> Result(Nil, StdioError)

Serves server with the default settings until standard input closes.

pub fn serve_with(
  server: server.Server(context),
  context: context,
  config: Config,
) -> Result(Nil, StdioError)

Serves server with these settings until standard input closes.

pub fn with_chunk_size(config: Config, bytes: Int) -> Config

The largest chunk read from standard input at once.

pub fn with_runtime(
  config: Config,
  runtime: runtime.Config,
) -> Config

The runtime limits: frame size, nesting depth, handler timeout and the rest of relay/runtime.Config.

✨ Search Document