Architecture
View SourceThis page is for you if you change wasm_tools rather than use it: what each module does, which processes exist, the path of one call, and where to start for a given change. The reasons behind the choices are in the design.
Layers
your code (Erlang, Elixir) MCP clients
| |
| barrel_mcp
| |
| wasm_tools_mcp
| |
+--------- wasm_tools -------+
|
erlang_wasmwasm_tools uses only erlang_wasm's supported API: wasm_component (describe/1,
prepare/1, hold/1, check/2, instantiate/3, call/4, destroy/2,
import_fun/2), wasi_preview2:command/1, wasm_limits. Decoding, the
Canonical ABI, WASI and limits stay there; a gap there is fixed there.
Where things are
| module | what it does |
|---|---|
wasm_tools | the public API: it checks nothing itself and dispatches to the modules below |
wasm_tools_component | inspect and load, in a bounded process; the holder; naming and selection of exports |
wasm_tools_policy | capabilities and limits, checked at load, turned into erlang_wasm's options at each call; the http host rules |
wasm_tools_host | host interfaces: checking handlers, wrapping them with the component's own import types |
wasm_tools_call | one call: argument check, the watchdog and the runner, deadline, cancel, observer events, error classes |
wasm_tools_registry | a registry: names, holders, checkout and admission, counters, MCP ownership |
wasm_tools_json | JSON Schema, and JSON to native values and back |
wasm_tools_mcp | the barrel_mcp adapter: registration, the handler, result mapping |
The supported API is wasm_tools, wasm_tools_host, wasm_tools_json and
wasm_tools_mcp, their documented functions only (the README, "Supported
API"); ex_doc groups them as API and the other four as internals. When users
need a function of an internal module, add one to wasm_tools that delegates to
it, as authorize/2 does, and give helpers that modules share -doc false.
| test suite | covers |
|---|---|
wasm_tools_SUITE | the public API on one component |
wasm_tools_call_SUITE | calls: deadline, cancel, heap limit, caller and registry death, observer |
wasm_tools_policy_SUITE | capabilities and limits, enforced on real components |
wasm_tools_host_SUITE | host interfaces |
wasm_tools_registry_SUITE | registries |
wasm_tools_json_SUITE, wasm_tools_json_prop_SUITE | the JSON mapping, by example and by property |
wasm_tools_mcp_SUITE | MCP, through barrel_mcp's server and client |
wasm_tools_docs_SUITE | every Erlang block of these pages |
wasm_tools_examples_SUITE | every example in examples/ runs as written; the Elixir one through mix test, skipped without mix |
wasm_tools_interop_SUITE | the same calls in wasmtime, answers compared; skipped without wasmtime |
wasm_tools_bench_SUITE | measurements, in a bench group not run by default |
test/wasm_tools_test.erl holds what the suites share: where the test
components are, a load that retries while the node refuses it for its load
rate, and a one-response HTTP server.
The test components are in test/fixtures/component/, with their Rust sources
and WIT; scripts/build-fixtures.sh builds them. Whole programs are in
examples/erlang/ (the calculator, data transform, HTTP client, custom host and
MCP server; each module says how to run it) and examples/elixir/ (a Mix
project).
Processes
| process | started by | lives |
|---|---|---|
| holder | load/2, add/3 | until unload/1, its owner's exit, or the registry removing the component |
| registry | start_link/1 | as long as you supervise it |
| watchdog | each call | the call |
| runner | the watchdog | the call |
Nothing starts with the application: wasm_tools.app.src has no mod.
The path of one call
wasm_tools:call/4finds the export: in the component value, or by a checkout message to the registry, which counts the call againstmax_concurrency.wasm_tools_call:start/3lowers the limits to what the call asks, orders the arguments by parameter, and checks them withwasm_component:check/2. A bad one is answered here; nothing is spawned.- The caller spawns the watchdog, which spawns the runner with
min_heap_sizeandmax_heap_size. - The runner builds the WASI imports and the host wrappers for this call, instantiates the prepared component, calls, destroys, and replies to the caller's alias.
- The watchdog waits for the runner, the deadline, a cancel, the caller's or the registry's death; it kills the runner when it must, says why, and checks the call back in to its registry.
An MCP call is the same, started by the handler of wasm_tools_mcp in
barrel_mcp's process for the request, which converts JSON on both sides and watches for
barrel_mcp's cancel.
If you want to...
| change | start in | prove it in |
|---|---|---|
| a capability | wasi_opts in wasm_tools_policy | wasm_tools_policy_SUITE, with a real component |
| a limit | limits_of and clamp in wasm_tools_policy | wasm_tools_policy_SUITE, wasm_tools_call_SUITE |
| how a call ends | watch in wasm_tools_call | wasm_tools_call_SUITE |
| an error class | normalize in wasm_tools_call, invalid in wasm_tools_component | the suite that produces it |
| a JSON form | s, n and j in wasm_tools_json | both JSON suites |
| tool naming | tool_name and select in wasm_tools_component | wasm_tools_SUITE, wasm_tools_registry_SUITE |
| what MCP clients see | tool_opts and reply in wasm_tools_mcp | wasm_tools_mcp_SUITE |
| a test component | test/fixtures/component/<name>/, then scripts/build-fixtures.sh | the suite using it |
Then update the page that describes it; wasm_tools_docs_SUITE checks the Erlang
in it.