Packages
Load WebAssembly components and call them as tools from Erlang, Elixir and MCP
Current section
Files
Jump to
Current section
Files
docs/quick-start.md
# Quick start
This page takes you from nothing to a component called from Erlang, its JSON
Schema, and a registry, in about five minutes. Read it first; the other pages go
deeper into each step.
## 1. Add the dependency
```erlang
{deps, [{wasm_tools, "~> 0.1", {pkg, erlang_wasm_tools}}]}.
```
Start it with your application, or in a shell:
<!-- check: run -->
```erlang
{ok, _} = application:ensure_all_started(wasm_tools).
```
## 2. Get a component
Any WebAssembly component works. This repository ships one,
`test/fixtures/component/calculator.component.wasm`, built from this WIT:
```text
package example:calculator;
world calculator {
export add: func(a: s32, b: s32) -> s32;
export multiply: func(a: s32, b: s32) -> s32;
}
```
[Write your first component](first-component.md) builds it from Rust.
## 3. Look inside without running it
<!-- check: run -->
```erlang
Path = "test/fixtures/component/calculator.component.wasm",
{ok, #{exports := [Add | _]}} = wasm_tools:inspect({file, Path}),
maps:with([name, params, result], Add).
%% => #{name := <<"add">>, params := [{<<"a">>, s32}, {<<"b">>, s32}], result := s32}
```
## 4. Load it and call it
<!-- check: run -->
```erlang
{ok, Calc} = wasm_tools:load({file, Path}),
wasm_tools:call(Calc, <<"add">>, #{<<"a">> => 20, <<"b">> => 22}).
%% => {ok, 42}
```
Arguments are a map keyed by parameter name. A value that does not fit its type
is refused before the component runs:
<!-- check: run -->
```erlang
wasm_tools:call(Calc, <<"add">>, #{<<"a">> => 1 bsl 40, <<"b">> => 1}).
%% => {error, #{class := argument, msg := <<"integer out of range">>, ctx := #{path := [<<"a">>], expected := s32}}}
```
## 5. Get its JSON Schema
<!-- check: run -->
```erlang
{ok, #{input := Input}} = wasm_tools:schema(Calc, <<"add">>),
maps:get(<<"required">>, Input).
%% => [<<"a">>, <<"b">>]
```
## 6. Put it in a registry
A registry holds tools from many components under names you choose, in a
process you supervise:
<!-- check: run -->
```erlang
{ok, Reg} = wasm_tools:start_link(#{}),
{ok, [<<"calc_add">>, <<"calc_multiply">>]} =
wasm_tools:add(Reg, Calc, #{namespace => <<"calc">>}),
wasm_tools:call(Reg, <<"calc_multiply">>, #{<<"a">> => 6, <<"b">> => 7}).
%% => {ok, 42}
```
<!-- check: run -->
```erlang
unlink(Reg),
ok = wasm_tools:stop(Reg),
ok = wasm_tools:unload(Calc).
```
## Next
- [Call a component](call-a-component.md): every value type, errors, deadlines.
- [Capabilities and security](capabilities.md): what a component may reach.
- [MCP](mcp.md): the same tools for AI agents.