Write an engine plugin
Edit on GitHubAn engine plugin is a whole inference engine of its own, behind the same table as the built-in ds4 (raw Metal),
llama.cpp and Neural Engine engines. It owns the weights, the KV cache, batching, sampling and its GPU runtime, and it
never calls MLX. The host keeps HTTP and every API surface (OpenAI, Anthropic, Ollama), the chat template, tool calls,
reasoning, stops, queueing and admission. Contract: src/abi/engine.zig. Overview: plugins.md. The kit,
install and test flow is in plugin-model.md.
mlx-serve plugin new myengine --engine --kit <kit dir> --dir ~/work
The scaffold streams a fixed reply one byte a token on its own thread, up to four requests at once. Replace its
round with your runtime.
If your model can run on MLX, write a model plugin instead: it gets the prefix cache, speculative decoding and batching for free.
Which models it serves
An engine serves the model folders it names, by folder name, in sdk.Plugin.models; it serves them over every
built-in. Two plugins that name the same folder are ambiguous, and neither serves it. The folder must still be one the
host can read: a config.json and a tokenizer, or a GGUF. probe then looks at the folder and its config.json and
refuses by name what it cannot open.
The table
abi.Engine in plugin.zig:
| Field | What it does |
|---|---|
probe |
Can this folder be opened? No GPU; refuse by name |
load_bytes |
The bytes open will hold: admission bills the load with it |
open / close |
Start the engine and fill Info; close cancels every request, and each still gets finished |
submit / cancel |
Take a request and stream its events to the Sink; stop one by id |
step |
null for an engine on its own thread (Threading.own_thread) |
status, memory |
Requests running and waiting; bytes held |
report_json, trim |
Optional: extra /props and /metrics fields; drop caches under memory pressure |
media_rows, embed, decide |
Optional: images and audio in, embeddings, typed decisions |
tokenize, token_bytes, chat_template, render |
Optional: only with Caps.owns_tokenizer / owns_template (a GGUF that carries its own vocabulary) |
open fills Info: the Caps of the opened model, threading, the context_window the host advertises (prompt plus
reply), max_concurrent, the EOS ids, and one startup line for the log.
A request's life
submitgets the prompt as token ids, already rendered and tokenized by the host.Requestis valid only during the call: copy what you keep.- Each request emits at most one
prefilled, then any number oftokens, then exactly onefinished, last. A request that ends before its prefill getsfinishedalone, withcancelledorfailedand a namedVerdict. - Sink callbacks return at once and never call back into the engine. Never hold your own lock across one: the host
may ask
statusormemoryfrom inside it. Capsis a promise. Declare only what you honour (penalties, logit bias, logprobs, grammar, think budget, tool gate, images, batching). Startsubmitwithif (abi.refusal(caps, abi.needs(r))) |v| return v;and a request that needs more is refused by name rather than silently ignored.- Every fallible call returns a
Verdictwhosewhyis static text. A full engine answers.busy.
Options
Settings a user may change are declared in sdk.Plugin.options (a name, a kind, a default, one line of help) and
read with getenv when the engine opens. The app draws them on the plugin's page and saves them in
~/.mlx-serve/plugins.json; a change applies at the next start. Names are the plugin's name upper-cased, then _
(MYENGINE_THREADS). The scaffold's one option is the reply it streams.
Check and try it
cd ~/work/myengine && zig build test && zig build
mlx-serve plugin check ./myengine # opens it on its fixture and serves one request
mlx-serve plugin fixture ./myengine ~/work/packs/myengine # its tiny pack, in a folder it names
mlx-serve plugin install myengine/zig-out/lib/libmyengine.dylib
mlx-serve --serve --port 18082 --model ~/work/packs/myengine
curl -s localhost:18082/v1/chat/completions -H 'Content-Type: application/json' \
-d '{"messages":[{"role":"user","content":"hi"}],"max_tokens":32}'
Enable plugins in ~/.mlx-serve/plugins.json, same as for model plugins (see the gotchas in
plugin-model.md). An engine plugin links no MLX, so it is never refused for another MLX pin.
Read these in the kit
examples/_example_engine/plugin.zigand itsconfig.zig: the scaffoldplugin newcopies (about 260 lines).src/abi/engine.zig:Engine,Open,Info,Caps,Request,Event,Sink. Field comments are the reference; plugin-abi.md lists every field.include/mlx_serve_plugin.h: the same ABI for C, C++ or Rust engines.