Mcp.Server

AI

Stdio MCP server for this process.

Mcp spawns a child and speaks MCP as a client. Mcp.Server is the other side: a Platform.worker owns stdin and stdout and speaks newline-delimited JSON-RPC (MCP stdio transport). The runtime answers initialize, notifications/initialized, ping, and tools/list. A tools/call for a registered tool arrives as ToolCall. Reply with text, result, or error.

Stdout is the protocol stream. Do not Debug.log from a server, and do not pass --sname to selm run (that prints a line before the VM starts). selm run passes -noinput so the VM does not also read stdin.

One server per VM. It can run beside an Mcp client; the client still spawns its own children. v1 is tools only: no resources, prompts, or sampling.

Mcp.Server.serve
    { name = "echo"
    , version = "0.1.0"
    , instructions = ""
    , tools = []
    }

Types

Tool

type alias Tool =
{ name : String
, description : String
, inputSchema : Value
}

One tool advertised from tools/list. inputSchema is a JSON object.

Examples

{ name = "echo", description = "Echo text", inputSchema = schema }

Event

type Event
= Ready
| ToolCall String String Value
| Closed String

Traffic from the MCP session.

ToolCall is the request id, the tool name, and the arguments object. Pass that id to text, result, or error. The runtime echoes the original JSON id type (number or string).

Examples

Mcp.Server.ToolCall "2" "echo" args

Values

serve

serve : { name : String , version : String , instructions : String , tools : List Tool } -> Cmd msg

Start the stdio server. Call from init. Later calls replace the registered tools.

Examples

Mcp.Server.serve
    { name = "echo"
    , version = "0.1.0"
    , instructions = ""
    , tools =
        [ { name = "echo"
          , description = "Echo text"
          , inputSchema = Json.Encode.object [ ( "type", Json.Encode.string "object" ) ]
          }
        ]
    }

subscribe

subscribe : (Event -> msg) -> Sub msg

Deliver Event values into the running program.

Examples

Mcp.Server.subscribe GotServer

text

text : String -> String -> Cmd msg

Reply to ToolCall with a text content block (isError false).

Examples

Mcp.Server.text id "pong"

result

result : String -> Value -> Cmd msg

Reply to ToolCall with a raw JSON-RPC result object.

Examples

Mcp.Server.result id value

error

error : String -> String -> Cmd msg

Reply to ToolCall with a JSON-RPC error (not a tool isError result).

Examples

Mcp.Server.error id "unknown tool"

notify

notify : String -> Value -> Cmd msg

Send a JSON-RPC notification (no id, no reply).

Examples

Mcp.Server.notify "notifications/message" params