wasm_tools (wasm_tools v0.1.0)
View SourceLoad WebAssembly components and call their exports as tools.
You give wasm_tools a component, the .wasm file a Component Model toolchain
builds (cargo build --target wasm32-wasip2, wasm-tools component new, and
others). It reads the component's own types, so you never write a signature:
each exported function becomes a tool you call with native Erlang values, and
that you can describe with a JSON Schema or expose through MCP.
{ok, Calc} = wasm_tools:load({file, "calculator.wasm"}),
{ok, 42} = wasm_tools:call(Calc, <<"add">>, #{<<"a">> => 20, <<"b">> => 22}),
ok = wasm_tools:unload(Calc).Use it instead of erlang_wasm's wasm_component directly when you want the
types read for you, a fresh instance per call with a deadline and a cancel,
capabilities granted by name, or several components managed together. Use
wasm_component when you need the instance itself: resources across calls,
streams, or control over instantiation.
- A loaded component is a value you can pass to any process. Its compiled code
is kept by a small holder process that belongs to the process that loaded
it;
unload/1, or that process exiting, releases it. - Every call gets a fresh instance, in a process of its own, with the limits of
the component (
wasm_tools_policy). Nothing a call does is seen by the next. - Arguments are a map keyed by parameter name. Values are erlang_wasm's:
records are maps with binary keys, variants
{Case, Payload}, enums and flags binaries, optionsnone | {some, V}, results{ok, V} | {error, E},list<u8>a binary, a char an integer. - Errors are maps
#{class, kind, msg, ctx}.classis one ofinvalid,link,argument,trap,exhaustion,timeout,cancelled,overloaded.
This module is the one you call. With wasm_tools_host, wasm_tools_json
and wasm_tools_mcp it is the supported API; the other modules are internal.
The design is in docs/design.md.
Summary
Types
An outbound HTTP request as the http capability sees it: before the host
name is resolved, and again with the address it resolved to.
What a call names a tool in: one component, or a registry.
Functions
Add a component's exports to a registry as tools, and return their names.
What a component's http capability decides for one request: allow or
deny. Nothing runs: use it to check a grant, or to explain a request the
component saw refused.
Wait for a call started with start_call/4. When Timeout runs out, the call
keeps running: wait again or cancel it.
Call a tool with no options.
Call a tool and wait for its result.
Stop a call. Returns once its process is gone and leaves no message behind; a cancelled call is not awaited.
Check arguments as a call would, without calling: ok, or the argument error
the call would answer.
A child spec for a registry in your supervision tree.
One tool's definition: name, export, interface, func, params,
result, callable and meta.
What a component exports and imports, read without running it: each export
with its tool name, parameters, result and whether it can be called; each
import with what would provide it. See wasm_tools_component:inspect/1.
Every export of a component, or every tool of a registry, as describe/2
gives them.
Load a component with no capability and the untrusted limits.
Load a component.
Remove tools from a registry. Calls running on them finish.
Remove tools; with #{kill => true}, calls running on them are stopped.
The JSON Schemas of a tool: #{input := Schema, output := Schema | undefined}.
See wasm_tools_json for the mapping. A type with no JSON form, such as an
option of an option, is an error.
Start a registry, linked to the caller. Options: name (registered locally),
max_concurrency (calls at once, infinity by default), observer. See
wasm_tools_registry.
A registry's counters: active, tools, calls, ok, rejected, and
errors by class.
Stop a registry: its calls stop and the components it kept are released.
Release a component's compiled code. A call to it afterwards answers
#{class := link, kind := unloaded}; calls already running finish.
Types
-type component() :: wasm_tools_component:component().
-type http_request() :: #{scheme := binary(), host := binary(), port := inet:port_number(), address => inet:ip_address()}.
An outbound HTTP request as the http capability sees it: before the host
name is resolved, and again with the address it resolved to.
-type registry() :: wasm_tools_registry:registry().
What a call names a tool in: one component, or a registry.
Functions
Add a component's exports to a registry as tools, and return their names.
Options: namespace (each name becomes Namespace_Name), exports (the
exports to add, by export or tool name; every callable one by default). A name
that collides refuses the whole add with kind := name_conflict.
-spec authorize(component(), http_request()) -> allow | deny.
What a component's http capability decides for one request: allow or
deny. Nothing runs: use it to check a grant, or to explain a request the
component saw refused.
These are two checks, as the component's request meets them: ask with
#{scheme, host, port} for the request before its name is resolved, and with
address added for the address the name resolved to. The request goes out
only when both answer allow. A name passes when it matches a host you
granted, on that host's port (the scheme's default, 80 or 443, when you gave
none); an address passes when it is public, or when you granted it as a
literal address. A component loaded without http denies every request.
-spec await(wasm_tools_call:job(), timeout()) -> {ok, term()} | {error, error()}.
Wait for a call started with start_call/4. When Timeout runs out, the call
keeps running: wait again or cancel it.
Call a tool with no options.
Call a tool and wait for its result.
Name is the tool's name, or for a component the export's full name.
Options: timeout and limits (each can only lower the component's own),
context (handed to host interfaces), observer.
A function returning result<T, E> answers {ok, {ok, T}} or
{ok, {error, E}}: the call worked, and the function said which.
-spec cancel(wasm_tools_call:job()) -> ok | {error, error()}.
Stop a call. Returns once its process is gone and leaves no message behind; a cancelled call is not awaited.
Check arguments as a call would, without calling: ok, or the argument error
the call would answer.
-spec child_spec(wasm_tools_registry:opts()) -> supervisor:child_spec().
A child spec for a registry in your supervision tree.
One tool's definition: name, export, interface, func, params,
result, callable and meta.
-spec inspect(wasm_tools_component:source()) -> {ok, map()} | {error, error()}.
What a component exports and imports, read without running it: each export
with its tool name, parameters, result and whether it can be called; each
import with what would provide it. See wasm_tools_component:inspect/1.
Every export of a component, or every tool of a registry, as describe/2
gives them.
-spec load(wasm_tools_component:source()) -> {ok, component()} | {error, error()}.
Load a component with no capability and the untrusted limits.
-spec load(wasm_tools_component:source(), map()) -> {ok, component()} | {error, error()}.
Load a component.
Options:
capabilities: what it may reach, nothing by default (wasm_tools_policy);limits:untrusted(the default),trusted, or a map overuntrusted;host:#{Interface => Handler}, the host interfaces it imports (wasm_tools_host);meta:#{ToolName => #{title, description, annotations}}, what you say about each tool, for MCP;owner: the process its holder belongs to, the caller by default;observer: called with[wasm_tools, load, stop]and, for its calls, the call events and[wasm_tools, http, denied](wasm_tools_call);sha256: the digest the bytes must have, in hex;max_bytes: the largest component accepted, 64 MiB by default.
A component whose imports are not all provided does not load.
Remove tools from a registry. Calls running on them finish.
Remove tools; with #{kill => true}, calls running on them are stopped.
-spec schema(target(), binary()) -> {ok, #{input := map(), output := map() | undefined}} | {error, error()}.
The JSON Schemas of a tool: #{input := Schema, output := Schema | undefined}.
See wasm_tools_json for the mapping. A type with no JSON form, such as an
option of an option, is an error.
-spec start_call(target(), binary(), map(), map()) -> {ok, wasm_tools_call:job()} | {error, error()}.
Start a call and return at once, with a job to await/2 or cancel/1 from
the same process. The call stops if that process exits.
-spec start_link(wasm_tools_registry:opts()) -> {ok, pid()} | {error, term()}.
Start a registry, linked to the caller. Options: name (registered locally),
max_concurrency (calls at once, infinity by default), observer. See
wasm_tools_registry.
A registry's counters: active, tools, calls, ok, rejected, and
errors by class.
-spec stop(registry()) -> ok.
Stop a registry: its calls stop and the components it kept are released.
-spec unload(component()) -> ok.
Release a component's compiled code. A call to it afterwards answers
#{class := link, kind := unloaded}; calls already running finish.