lmmock reference
Complete command-line subcommands and flags, HTTP Exchange and host functions, stdio RPC and wire Session interfaces, standard lmcode_std_v3 REST and CLI interfaces, and exit codes for lmmock.
CLI subcommands and flags
| Command or flag | Arguments | Description |
|---|---|---|
| --version | version | none | Print the lmmock version string and exit 0. |
| schema | --lmcode-schema --json | none | Write the embedded HTTP host schema JSON (lmmock_v5) to stdout. |
| --lmcode-signatures | none | Print all canonical host and standard library (lmcode_std_v3) signatures. |
| --lmcode | <file> [cli-args...] | Load and validate compiled bytecode, and optionally dispatch stateless CLI arguments via engine.CLI. |
| exec | --lmcode FILE | --builtin NAME [flags] -- CMD... | Start a loopback mock server, run CMD with LMMOCK_URL set, and close the server when CMD exits. |
| serve | --lmcode FILE | --builtin NAME [flags] | Start a loopback mock server, print readiness JSON to stdout, and serve until SIGINT or SIGTERM. |
| --builtin | llm-api | oauth-codex-device | Use an embedded precompiled mock program instead of --lmcode FILE. |
| --listen | <loopback-ip:port> | Loopback bind address for serve, exec, proto, or wire (default 127.0.0.1:0). |
| --config | <file.json> | Trusted JSON configuration file exposed to scripts via config() or to protocol/wire engines. |
| --ready-file | <path> | Atomically write readiness JSON to disk once listening and remove it on graceful shutdown. |
| --tls / --tls-host | [--tls] [--tls-host DNS_OR_IP]... | Enable HTTPS with a per-run CA certificate and optional SAN DNS/IP identities. |
| --upstream | <http(s)://host/base/> | Forward requests with no matching mock route to a fixed upstream HTTP/1.1 origin. |
| --upstream-timeout / --upstream-read-only | [duration] [bool] | Set total upstream request deadline (default 2m) or restrict passthrough to GET/HEAD without credentials. |
| --audit-log | <file.jsonl> | Append redacted JSONL records for mocked, forwarded, and unmatched requests. |
| --request-log / --request-log-bodies | none | Enable in-memory redacted request history (and optional 1 MiB bodies) for Exchange.request_log(). |
| check | --suite <file.json> [--url URL] | Run declarative HTTP assertion steps against --url or LMMOCK_URL and print a JSON pass summary. |
| proto | --list | <name> [--listen IP:P] [--config F] [--ready-file F] | List or run in-memory wire servers: amqp, imap, kafka, mongodb, mqtt, postgres, redis, smtp. |
| stdio | --schema | acp|mcp|mcp-client|agent|once|rpc --scenario F [flags] | Export stdio RPC schema or run stdio JSON-RPC / agent scenario mocks (--preset, --lmcode, --alias, --state, --framing, --fake-bin, --stdin-prompt). |
| wire | --list | schema KIND | serve|exec KIND --config F [--listen IP:P] | List, inspect schema/signatures, or run frame-level mocks for ws, http2, grpc, and line protocols. |
| scenario | scrub | import | verify --input FILE [--output FILE | --target URL] | Scrub credentials from raw lminspect JSONL captures, compile scenario JSON, or verify replay fidelity. |
HTTP host schema: Handler, Exchange, and functions
| Type / function | Signature | Description |
|---|---|---|
| interface Handler | fn h(x: Exchange) -> void implements Handler | Annotated HTTP request handler bound to a method and path pattern via route(). |
| Exchange (request) | x.method() | x.path() | x.body() | x.request_json() -> string | Read the HTTP method, decoded URL path, raw request body bytes, or full request summary JSON. |
| Exchange (lookup) | x.query(string) | x.param(string) | x.header(string) -> string | Read a query parameter, route path parameter, or request header ("" when absent). |
| Exchange (response) | x.status(int) | x.set_header(string, string) | x.send(string) | x.json(string) -> void | Set HTTP status (200-599), response header, raw body bytes, or application/json body. |
| Exchange (timing/faults) | x.delay(int) -> void | x.pause(int) -> bool | x.disconnect() -> void | Delay response (0-60000 ms), pause execution while respecting client cancellation, or drop the connection. |
| Exchange (streaming) | x.stream_event(string) -> void | x.stream_fault(string) -> void | Flush ordered chunk bytes immediately, or inject disconnect, truncated-chunk, or malformed-chunk HTTP/1 faults. |
| Exchange (state/queues) | x.get(string) -> string | x.set(string, string) -> void | x.take(string) -> string | x.increment(string) -> int | Read, write, atomically pop shared string state, or atomically increment an integer counter (starting at 1). |
| Exchange (queue/admin) | x.queue_response(string) -> void | x.next_queued_response(string) -> string | x.request_log() -> string | x.reset() -> void | Enqueue/dequeue per-key responses, inspect redacted request log JSON, or clear all shared server state and logs. |
| route / config | route(method: string, path: string, annotation: string) -> void | config() -> string | Register a startup HTTP route handler or read the raw JSON string from --config. |
| Encoding and form helpers | form_get(body: string, key: string) -> string | url_encode(string) -> string | url_decode(string) -> string | json_quote(string) -> string | Extract URL-encoded form values, percent-encode/decode strings, or quote a raw string as a JSON string literal. |
| Auth and crypto helpers | basic_id(string) -> string | basic_secret(string) -> string | random_token() -> string | sha256_url(string) -> string | secure_equal(string, string) -> bool | now_ms() -> int | Parse HTTP Basic auth headers, generate random tokens, compute base64url SHA-256 PKCE digests, compare secrets in constant time, or read epoch ms. |
| JSON, templates, and zstd | json_with(Json, string, Json) -> Json | template_render(string, Json) -> string | append_zstd(string, string) -> void | read_zstd(string) -> string | Apply immutable JSON Pointer updates, render one-pass templates, or read/append zstd-compressed session files. |
Stdio RPC and wire protocol schemas
| Schema family | Interface / type | Methods and functions |
|---|---|---|
| lmmock stdio (--schema) | interface RPCHandler(x: RPC) -> void | bind(method, annotation), x.message(), x.method(), x.params(), x.alias(), x.get(k), x.set(k, v), x.next(k), x.reply(json), x.error(code, msg), x.notify(method, json), x.request(method, json), x.emit(json), x.write_text(bytes), x.forward(key), x.close(), x.abort(msg). |
| lmmock wire schema ws|http2|grpc|line | WebSocketHandler | HTTP2Handler | GRPCHandler | LineHandler (s: Session) -> void | bind(key, annotation), s.receive() -> string, s.send(string) -> void, s.close() -> void, s.metadata(string) -> string, s.set_metadata(string, string) -> void. |
Standard lmcode_std_v3 REST and CLI interfaces
| Interface or function | Signature / annotation | Description |
|---|---|---|
| RestGet .. RestOptions | @"/path/:param" fn h(x: RestExchange) -> void implements RestGet | Bind an HTTP GET, POST, PUT, DELETE, PATCH, HEAD, or OPTIONS route handler (normalizing :param to {param}). |
| RestHandler | @"/path" fn h(x: RestExchange) -> void implements RestHandler | Bind a method-agnostic HTTP route handler across all supported methods. |
| CliCommand | @"cmd" fn h(x: RestExchange) -> void implements CliCommand | Bind a named CLI subcommand dispatched by rest_cli or lmmock --lmcode FILE cmd. |
| CliOption | @"--flag" fn h(x: RestExchange) -> void implements CliOption | Bind a CLI option flag evaluated before the target CliCommand or route handler. |
| rest_config | rest_config(key: string, value: string) -> void | Configure route prefix (prefix or base_path), session mode, or initial shared state keys. |
| rest_route | rest_route(method: string, path: string, handler: string) -> void | Explicitly bind an HTTP method and route pattern to an annotated handler function. |
| rest_serve / rest_stop | rest_serve() -> void | rest_stop() -> void | No-op compatibility hooks inside lmmock (the host serve/exec lifecycle owns the HTTP listener). |
| rest_cli | rest_cli(args: StrList) -> string | Dispatch CLI options, commands, and HTTP routes in-process and return the response payload string. |
| RestExchange | x.method() | x.path() | x.body() | x.param(string) | x.query(string) | x.header(string) -> string | Read request method, path, body, path parameter, query parameter, or request header. |
| RestExchange | x.has_query(string) | x.has_header(string) -> bool | Check whether a query parameter or request header is present on the exchange. |
| RestExchange | x.status(int) | x.set_header(string, string) | x.send(string) | x.json(string) -> void | Set response status code and headers, and emit plain text or application/json bodies. |
| RestExchange | x.get(string) -> string | x.set(string, string) -> void | x.increment(string) -> int | Read, write, or atomically increment shared server state across requests and CLI invocations. |
Exit codes
| Exit code | Category | Meaning |
|---|---|---|
| 0 | Success | Server shutdown, dry-run check, CLI command, check suite, or wrapped client process completed cleanly. |
| 1 | Operation / check failure | Standalone runtime error, check suite assertion failure, or scenario verification mismatch. |
| 2 | Usage error | Invalid CLI flags, non-loopback --listen address, retired flag spelling, or missing command arguments. |
| 3 | Host / load error | Bytecode validation failure, schema mismatch, missing/duplicate route annotation, or unhandled VM runtime error. |
| 4 | Application failure | Wrapped client command exited non-zero under run_lmcode (standalone exec returns the child's exact exit code). |