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 flagArgumentsDescription
--version | versionnonePrint the lmmock version string and exit 0.
schema | --lmcode-schema --jsonnoneWrite the embedded HTTP host schema JSON (lmmock_v5) to stdout.
--lmcode-signaturesnonePrint 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.
--builtinllm-api | oauth-codex-deviceUse 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-bodiesnoneEnable 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.
scenarioscrub | 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 / functionSignatureDescription
interface Handlerfn h(x: Exchange) -> void implements HandlerAnnotated HTTP request handler bound to a method and path pattern via route().
Exchange (request)x.method() | x.path() | x.body() | x.request_json() -> stringRead 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) -> stringRead 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) -> voidSet 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() -> voidDelay 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) -> voidFlush 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) -> intRead, 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() -> voidEnqueue/dequeue per-key responses, inspect redacted request log JSON, or clear all shared server state and logs.
route / configroute(method: string, path: string, annotation: string) -> void | config() -> stringRegister a startup HTTP route handler or read the raw JSON string from --config.
Encoding and form helpersform_get(body: string, key: string) -> string | url_encode(string) -> string | url_decode(string) -> string | json_quote(string) -> stringExtract URL-encoded form values, percent-encode/decode strings, or quote a raw string as a JSON string literal.
Auth and crypto helpersbasic_id(string) -> string | basic_secret(string) -> string | random_token() -> string | sha256_url(string) -> string | secure_equal(string, string) -> bool | now_ms() -> intParse 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 zstdjson_with(Json, string, Json) -> Json | template_render(string, Json) -> string | append_zstd(string, string) -> void | read_zstd(string) -> stringApply immutable JSON Pointer updates, render one-pass templates, or read/append zstd-compressed session files.

Stdio RPC and wire protocol schemas

Schema familyInterface / typeMethods and functions
lmmock stdio (--schema)interface RPCHandler(x: RPC) -> voidbind(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|lineWebSocketHandler | HTTP2Handler | GRPCHandler | LineHandler (s: Session) -> voidbind(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 functionSignature / annotationDescription
RestGet .. RestOptions@"/path/:param" fn h(x: RestExchange) -> void implements RestGetBind an HTTP GET, POST, PUT, DELETE, PATCH, HEAD, or OPTIONS route handler (normalizing :param to {param}).
RestHandler@"/path" fn h(x: RestExchange) -> void implements RestHandlerBind a method-agnostic HTTP route handler across all supported methods.
CliCommand@"cmd" fn h(x: RestExchange) -> void implements CliCommandBind a named CLI subcommand dispatched by rest_cli or lmmock --lmcode FILE cmd.
CliOption@"--flag" fn h(x: RestExchange) -> void implements CliOptionBind a CLI option flag evaluated before the target CliCommand or route handler.
rest_configrest_config(key: string, value: string) -> voidConfigure route prefix (prefix or base_path), session mode, or initial shared state keys.
rest_routerest_route(method: string, path: string, handler: string) -> voidExplicitly bind an HTTP method and route pattern to an annotated handler function.
rest_serve / rest_stoprest_serve() -> void | rest_stop() -> voidNo-op compatibility hooks inside lmmock (the host serve/exec lifecycle owns the HTTP listener).
rest_clirest_cli(args: StrList) -> stringDispatch CLI options, commands, and HTTP routes in-process and return the response payload string.
RestExchangex.method() | x.path() | x.body() | x.param(string) | x.query(string) | x.header(string) -> stringRead request method, path, body, path parameter, query parameter, or request header.
RestExchangex.has_query(string) | x.has_header(string) -> boolCheck whether a query parameter or request header is present on the exchange.
RestExchangex.status(int) | x.set_header(string, string) | x.send(string) | x.json(string) -> voidSet response status code and headers, and emit plain text or application/json bodies.
RestExchangex.get(string) -> string | x.set(string, string) -> void | x.increment(string) -> intRead, write, or atomically increment shared server state across requests and CLI invocations.

Exit codes

Exit codeCategoryMeaning
0SuccessServer shutdown, dry-run check, CLI command, check suite, or wrapped client process completed cleanly.
1Operation / check failureStandalone runtime error, check suite assertion failure, or scenario verification mismatch.
2Usage errorInvalid CLI flags, non-loopback --listen address, retired flag spelling, or missing command arguments.
3Host / load errorBytecode validation failure, schema mismatch, missing/duplicate route annotation, or unhandled VM runtime error.
4Application failureWrapped client command exited non-zero under run_lmcode (standalone exec returns the child's exact exit code).