Architecture

View Source

This 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_wasm

wasm_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

modulewhat it does
wasm_toolsthe public API: it checks nothing itself and dispatches to the modules below
wasm_tools_componentinspect and load, in a bounded process; the holder; naming and selection of exports
wasm_tools_policycapabilities and limits, checked at load, turned into erlang_wasm's options at each call; the http host rules
wasm_tools_hosthost interfaces: checking handlers, wrapping them with the component's own import types
wasm_tools_callone call: argument check, the watchdog and the runner, deadline, cancel, observer events, error classes
wasm_tools_registrya registry: names, holders, checkout and admission, counters, MCP ownership
wasm_tools_jsonJSON Schema, and JSON to native values and back
wasm_tools_mcpthe 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 suitecovers
wasm_tools_SUITEthe public API on one component
wasm_tools_call_SUITEcalls: deadline, cancel, heap limit, caller and registry death, observer
wasm_tools_policy_SUITEcapabilities and limits, enforced on real components
wasm_tools_host_SUITEhost interfaces
wasm_tools_registry_SUITEregistries
wasm_tools_json_SUITE, wasm_tools_json_prop_SUITEthe JSON mapping, by example and by property
wasm_tools_mcp_SUITEMCP, through barrel_mcp's server and client
wasm_tools_docs_SUITEevery Erlang block of these pages
wasm_tools_examples_SUITEevery example in examples/ runs as written; the Elixir one through mix test, skipped without mix
wasm_tools_interop_SUITEthe same calls in wasmtime, answers compared; skipped without wasmtime
wasm_tools_bench_SUITEmeasurements, 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

processstarted bylives
holderload/2, add/3until unload/1, its owner's exit, or the registry removing the component
registrystart_link/1as long as you supervise it
watchdogeach callthe call
runnerthe watchdogthe call

Nothing starts with the application: wasm_tools.app.src has no mod.

The path of one call

  1. wasm_tools:call/4 finds the export: in the component value, or by a checkout message to the registry, which counts the call against max_concurrency.
  2. wasm_tools_call:start/3 lowers the limits to what the call asks, orders the arguments by parameter, and checks them with wasm_component:check/2. A bad one is answered here; nothing is spawned.
  3. The caller spawns the watchdog, which spawns the runner with min_heap_size and max_heap_size.
  4. 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.
  5. 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...

changestart inprove it in
a capabilitywasi_opts in wasm_tools_policywasm_tools_policy_SUITE, with a real component
a limitlimits_of and clamp in wasm_tools_policywasm_tools_policy_SUITE, wasm_tools_call_SUITE
how a call endswatch in wasm_tools_callwasm_tools_call_SUITE
an error classnormalize in wasm_tools_call, invalid in wasm_tools_componentthe suite that produces it
a JSON forms, n and j in wasm_tools_jsonboth JSON suites
tool namingtool_name and select in wasm_tools_componentwasm_tools_SUITE, wasm_tools_registry_SUITE
what MCP clients seetool_opts and reply in wasm_tools_mcpwasm_tools_mcp_SUITE
a test componenttest/fixtures/component/<name>/, then scripts/build-fixtures.shthe suite using it

Then update the page that describes it; wasm_tools_docs_SUITE checks the Erlang in it.