Snodo.Transport.Stdio.Framing (snodo v0.1.0)

Copy Markdown View Source

Newline-delimited JSON framing for the MCP stdio binding.

Summary

Functions

Decodes one line of JSON. A trailing newline and carriage return, and a leading UTF-8 byte order mark, are ignored.

Encodes one message as a line of JSON.

Functions

decode_line(line)

@spec decode_line(iodata()) :: {:ok, term()} | {:error, Snodo.Error.t()}

Decodes one line of JSON. A trailing newline and carriage return, and a leading UTF-8 byte order mark, are ignored.

encode_message(message)

@spec encode_message(map()) :: iodata()

Encodes one message as a line of JSON.

Every character outside ASCII is written as a JSON \uXXXX escape, with a surrogate pair above U+FFFF. The line is then the same bytes whatever the output device's encoding. Raw UTF-8 written with IO.binwrite/2 to a Unicode-mode device, which :stdio is under elixir, would be encoded a second time. The escapes also keep U+2028 and U+2029 out of the stream.