Packages

Load WebAssembly components and call them as tools from Erlang, Elixir and MCP

Current section

Files

Jump to
erlang_wasm_tools docs first-component.md
Raw

docs/first-component.md

# Write your first component

This page builds a WebAssembly component from Rust and calls it from Erlang. You
need it when you write the tools yourself. You do not need to know Erlang, MCP or
wasm_tools to write the component: you describe its functions in WIT and build
it with your language's usual tools. [Features](features.md) lists the
toolchains tested.

You need Rust with the `wasm32-wasip2` target (`rustup target add wasm32-wasip2`)
and, to look at the result, the `wasm-tools` CLI (`cargo install wasm-tools`).

## 1. Describe the functions in WIT

`wit/calculator.wit`:

```text
package example:calculator;

/// A calculator with two operations.
world calculator {
    /// Add two integers. Wraps on overflow.
    export add: func(a: s32, b: s32) -> s32;

    /// Multiply two integers. Wraps on overflow.
    export multiply: func(a: s32, b: s32) -> s32;
}
```

The parameter names (`a`, `b`) become the keys of the arguments map, and the
types become the JSON Schema. Name things for the person or the model that will
call them.

## 2. Implement them

`Cargo.toml`:

```text
[package]
name = "calculator"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
wit-bindgen = "0.62"

[profile.release]
opt-level = "s"
strip = true
panic = "abort"
```

`src/lib.rs`:

```text
wit_bindgen::generate!({ world: "calculator" });

struct Calculator;

impl Guest for Calculator {
    fn add(a: i32, b: i32) -> i32 { a.wrapping_add(b) }
    fn multiply(a: i32, b: i32) -> i32 { a.wrapping_mul(b) }
}

export!(Calculator);
```

## 3. Build it

```sh
cargo build --release --target wasm32-wasip2
cp target/wasm32-wasip2/release/calculator.wasm calculator.component.wasm
wasm-tools component wit calculator.component.wasm
```

The last command prints the component's interface back. A component built for
`wasm32-wasip2` imports the WASI interfaces its code uses; this one uses none.

A component that must import nothing at all can also be built for
`wasm32-unknown-unknown` and wrapped:

```sh
cargo build --release --target wasm32-unknown-unknown
wasm-tools component new target/wasm32-unknown-unknown/release/calculator.wasm \
    -o calculator.component.wasm
```

`scripts/build-fixtures.sh` builds every test component of this repository both
ways.

## 4. Call it

<!-- check: run -->
```erlang
{ok, _} = application:ensure_all_started(wasm_tools),
{ok, Calc} = wasm_tools:load({file, "test/fixtures/component/calculator.component.wasm"}),
{ok, Defs} = wasm_tools:list(Calc),
[N || #{name := N} <- Defs].
%% => [<<"add">>, <<"multiply">>]
```

<!-- check: run -->
```erlang
{ok, 42} = wasm_tools:call(Calc, <<"multiply">>, #{<<"a">> => 6, <<"b">> => 7}),
ok = wasm_tools:unload(Calc).
```

## Notes

- A panic in the component is a trap: the call answers
  `{error, #{class := trap}}`. Return a `result<T, E>` for errors the caller
  should handle.
- Every call gets a fresh instance: a global variable in the component starts
  from its initial value on each call.
- Doc comments (`///`) are not carried into a runnable component by the
  `wasm-tools` CLI. Give descriptions as `meta` when you load the component
  (see [MCP](mcp.md)).