Port
Processes
Open an Erlang port from a program.
spawn takes a port from a port module (lines or feed) so the command is tied to that module. Return it from init. Incoming ports are subscriptions (lines GotLine); outgoing ports are commands (feed text). Subscribe with onExit to learn the OS exit status (requires exitStatus in the spawn settings).
On the Erlang target, update runs commands before the next subscription sync. A short-lived child can exit in that window. The runtime buffers the exit status until onExit attaches, so this pattern is safe:
1. Set a running flag and return Port.spawn from the same update 2. Gate Port.onExit (and the port's data sub) on that flag in subscriptions
Subscribing to onExit before any successful Port.spawn for that module still fails with port_not_spawned.
For an external MCP server over stdio, use Mcp. To be an MCP server in this process, use Mcp.Server. For ACP agents over stdio (agent acp, acp-bridge, …), use Acp. Prefer those over framing JSON-RPC by hand on a raw port.
When a port is stopped (unsubscribe / replace / ACP·MCP disconnect), the runtime SIGTERM/SIGKILL’s the OS process **tree** on Unix — not only the direct child — so wrappers like uvx do not leave orphan grandchildren.
Types
Setting
type Setting = SettingOne open_port setting.
Examples
Port.line 1024Values
spawn
spawn : a -> String -> List Setting -> Cmd msgOpen command for the port module that declares portFun.
Examples
Port.spawn lines "/usr/bin/wc" [ Port.args [ "-l" ], Port.exitStatus ]onExit
onExit : a -> (Int -> msg) -> Sub msgSubscribe to the OS exit status of a spawned port. Uses the same port function as spawn to identify the module; does not open a second process. Pass exitStatus in the spawn settings or the status never arrives.
If the OS process exits before this subscription attaches (commands flush before subscription sync), the runtime still delivers exactly one exit for that spawn once onExit is subscribed.
Examples
Port.onExit lines GotExitbinary
binary : SettingDeliver port data as binaries.
Examples
Port.binarystream
stream : SettingA stream of bytes. This is the default.
Examples
Port.streampacket
packet : Int -> SettingPacket framing of 1, 2, or 4 length bytes.
Examples
Port.packet 4line
line : Int -> SettingDeliver one line at a time, up to max bytes.
Examples
Port.line 1024exitStatus
exitStatus : SettingAsk the OS process for an exit status.
Examples
Port.exitStatuseof
eof : SettingSend a message when the port reaches end of file.
Examples
Port.eofuseStdio
useStdio : SettingUse stdin and stdout. This is the default.
Examples
Port.useStdionoUseStdio
noUseStdio : SettingDo not use stdin and stdout.
Examples
Port.noUseStdiostderrToStdout
stderrToStdout : SettingFold the OS process stderr into stdout.
Examples
Port.stderrToStdoutinput
input : SettingOpen the port for input.
Examples
Port.inputoutput
output : SettingOpen the port for output.
Examples
Port.outputhide
hide : SettingHide the OS process window.
Examples
Port.hideoverlap
overlap : SettingAllow overlapping port_command calls.
Examples
Port.overlapcd
cd : String -> SettingWorking directory for the OS process.
Examples
Port.cd "/tmp"env
env : List (String, String) -> SettingEnvironment variables for the OS process.
Examples
Port.env [ ("LANG", "C") ]args
args : List String -> SettingArguments for the OS process.
Examples
Port.args [ "-l", "README.md" ]arg0
arg0 : String -> Settingargv[0] for the OS process.
Examples
Port.arg0 "wc"