Build

Write an engine plugin

Edit on GitHub

An 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

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