Registries
View SourceA registry holds tools from several components under names you choose, in a process you supervise. You need one when an application manages more than one component, wants a bound on concurrent calls or counters, or exposes tools through MCP. For a single component, call it directly (Call a component).
Start one in your supervision tree
init([]) ->
Children = [wasm_tools:child_spec(#{name => my_tools, max_concurrency => 64})],
{ok, {#{strategy => one_for_one}, Children}}.Or from a shell:
{ok, _} = application:ensure_all_started(wasm_tools),
{ok, Reg} = wasm_tools:start_link(#{max_concurrency => 64}),
unlink(Reg).| option | what it does |
|---|---|
name | registers the registry locally, so calls can name it |
max_concurrency | calls running at once; past it a call is refused with overloaded (infinity by default) |
observer | a fun told about every call (Observability) |
There is no global registry: each one has its own tools, counters and bound.
Add components
Fixture = fun(N) -> {file, "test/fixtures/component/" ++ N ++ ".component.wasm"} end,
{ok, Calc} = wasm_tools:load(Fixture("calculator")),
wasm_tools:add(Reg, Calc, #{namespace => <<"calc">>}).
%% => {ok, [<<"calc_add">>, <<"calc_multiply">>]}namespaceprefixes every name withNamespace_.exportspicks the functions to add, by export or tool name; every callable one by default.- The registry keeps the component itself: you can unload yours.
Names are computed among the functions you pick. A component that exports two
interfaces named ops cannot be added whole, but each can be added on its own:
{ok, Conflict} = wasm_tools:load(Fixture("iface_conflict")),
{error, #{kind := name_conflict}} = wasm_tools:add(Reg, Conflict, #{}),
{ok, [<<"alpha_ops_run">>]} =
wasm_tools:add(Reg, Conflict, #{namespace => <<"alpha">>,
exports => [<<"example:alpha/ops@1.0.0#run">>]}),
wasm_tools:add(Reg, Conflict, #{namespace => <<"beta">>,
exports => [<<"example:beta/ops@1.0.0#run">>]}).
%% => {ok, [<<"beta_ops_run">>]}A name already in the registry is never replaced: the add is refused.
Call
wasm_tools:call(Reg, <<"calc_add">>, #{<<"a">> => 20, <<"b">> => 22}).
%% => {ok, 42}describe/2, list/1, schema/2, check/3 and start_call/4 take a registry
as they take a component.
Count
maps:with([active, calls, ok, tools], wasm_tools:stats(Reg)).
%% => #{active := 0, calls := 1, ok := 1, tools := 4}stats/1 also has rejected (calls refused past max_concurrency) and
errors, a count per error class.
Remove
ok = wasm_tools:remove(Reg, [<<"calc_multiply">>]),
wasm_tools:call(Reg, <<"calc_multiply">>, #{<<"a">> => 1, <<"b">> => 2}).
%% => {error, #{class := link, kind := unknown_tool}}- No call starts on a tool once
remove/2has returned. Calls already running finish;remove(Reg, Names, #{kill => true})stops them. - When the last tool of a component is removed, the registry releases it.
- Tools exposed through MCP are taken out of MCP first.
Stop
ok = wasm_tools:stop(Reg),
ok = wasm_tools:unload(Calc),
ok = wasm_tools:unload(Conflict).When a registry stops, the calls it started stop, the components it kept are released, and its MCP tools are dropped.