Packages

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

Current section

Files

Jump to
Raw

README.md

<div align="center">
  <h1 align="center">erlang_wasm_tools</h1>
  <p><b>Load WebAssembly components and call them as tools from Erlang, Elixir and MCP</b></p>
  <p>
    <a href="https://hexdocs.pm/erlang_wasm_tools/quick-start.html">Quick start</a> |
    <a href="https://hexdocs.pm/erlang_wasm_tools">Documentation</a> |
    <a href="https://hexdocs.pm/erlang_wasm_tools/features.html">Features</a> |
    <a href="https://hexdocs.pm/erlang_wasm_tools/changelog.html">Changelog</a> |
    <a href="https://hexdocs.pm/erlang_wasm_tools/contributing.html">Contributing</a>
  </p>
</div>

> [!WARNING]
> **Early development**: this project is not production ready. 0.1.0 is its
> first release: the API, the defaults and the error terms may change between
> 0.x releases, and erlang_wasm, the runtime underneath, is young too. Pin an
> exact version, and check [Features] before you rely on something.

## Why wasm_tools?

- **No glue code**: you call a component's functions with ordinary Erlang
  values. wasm_tools reads their names and types from the component, and refuses
  a value that does not fit before the component runs.
- **Reusability**: a component is a standard WebAssembly component, with nothing
  specific to Erlang or MCP in it. You build it in Rust, Python or JavaScript,
  and the same file runs here and in wasmtime.
- **Security**: each call runs in a fresh instance, in a process of its own,
  with a deadline and limits on work, memory and heap. A component reaches only
  the directories, variables and hosts you grant it.
- **MCP when you want it**: the same tools can be exposed to AI agents through
  [barrel_mcp], with no MCP code in the component.

## Architecture

```text
  your Erlang or Elixir code            MCP clients (AI agents)
              |                                   |
              |                              barrel_mcp
              |                                   |
              |                            wasm_tools_mcp
              |                                   |
              +----------- wasm_tools ------------+
                    names, calls, registries,
                   capabilities, limits, JSON
                               |
                          erlang_wasm
             a WebAssembly runtime written in Erlang
                               |
                    your WebAssembly components
```

wasm_tools runs on [erlang_wasm]: no native runtime, no port, no NIF.
[Architecture](docs/architecture.md) maps each box to its module.

## Installation

rebar3:

```erlang
{deps, [{wasm_tools, "~> 0.1", {pkg, erlang_wasm_tools}}]}.
```

Mix:

```elixir
{:wasm_tools, "~> 0.1", hex: :erlang_wasm_tools}
```

wasm_tools needs Erlang/OTP 29 or later. Add `barrel_mcp` (4.9 or later) as
well if you expose tools through MCP. The OTP application is `wasm_tools`; the
Hex package is `erlang_wasm_tools`, so it is not mistaken for the Bytecode
Alliance `wasm-tools` CLI.

## Using wasm_tools

Start the application and load a component. This repository and the Hex package
ship a calculator built from Rust; [Write your first component] shows how.

<!-- check: run -->
```erlang
{ok, _} = application:ensure_all_started(wasm_tools),
{ok, Calc} = wasm_tools:load({file, "test/fixtures/component/calculator.component.wasm"}).
```

Call one of its functions with Erlang values:

<!-- check: run -->
```erlang
wasm_tools:call(Calc, <<"add">>, #{<<"a">> => 20, <<"b">> => 22}).
%% => {ok, 42}
```

A value that does not fit the function's type is refused, by name, before the
component runs:

<!-- check: run -->
```erlang
{error, #{class := argument, ctx := #{path := Path}}} =
    wasm_tools:call(Calc, <<"add">>, #{<<"a">> => 1 bsl 40, <<"b">> => 1}),
Path.
%% => [<<"a">>]
```

Each function has a JSON Schema, for an agent or a form:

<!-- check: run -->
```erlang
{ok, #{input := Input}} = wasm_tools:schema(Calc, <<"add">>),
maps:get(<<"required">>, Input).
%% => [<<"a">>, <<"b">>]
```

With barrel_mcp running, one call exposes the calculator to MCP clients, as the
tools `calculator_add` and `calculator_multiply` ([MCP] shows a whole server):

```erlang
{ok, Exposed} = wasm_tools_mcp:register(Calc, #{namespace => <<"calculator">>}).
```

When you are done, unload it:

<!-- check: run -->
```erlang
ok = wasm_tools:unload(Calc).
```

## When to use it

Use `wasm_tools` when you want to:

- call a component's functions without writing their types;
- run each call in a fresh instance with a deadline, a cancel and limits on
  fuel, memory and heap;
- grant a component a directory, some variables or some hosts by name, and
  nothing else;
- give a component functions of your application (host interfaces);
- manage many components together, bound how many calls run at once, and count
  them;
- expose components to MCP clients.

Use erlang_wasm's `wasm_component` directly when you need the instance itself:
resources that live across calls, streams, or control over instantiation.

## Documentation

| to | read |
| --- | --- |
| call a component in five minutes | [Quick start] |
| build a component | [Write your first component] |
| call one component | [Call a component](docs/call-a-component.md) |
| manage many, in your supervision tree | [Registries](docs/registries.md) |
| decide what a component may reach | [Capabilities and security](docs/capabilities.md) |
| give it functions of your application | [Host interfaces](docs/host-interfaces.md) |
| know who owns what, and what stops when | [Lifecycle](docs/lifecycle.md) |
| expose tools to AI agents | [MCP] |
| use it from Elixir | [Elixir](docs/elixir.md) |
| watch it in production | [Observability](docs/observability.md) |
| send and receive JSON | [JSON mapping](docs/json.md) |
| know what is supported, and with which toolchains | [Features] |
| know what a call costs | [Benchmarks](docs/benchmarks.md) |
| see whole programs | the examples in the repository: `examples/erlang/` and `examples/elixir/` |
| change the library | [Architecture](docs/architecture.md), [Contributing] |

The published documentation is on [HexDocs][Documentation].

## Supported API

You call four modules. Their documented functions, types and callbacks are the
supported API:

| module | use it to |
| --- | --- |
| `wasm_tools` | inspect, load and call components, manage registries, get schemas, check arguments, and ask what an `http` grant decides (`authorize/2`) |
| `wasm_tools_host` | implement a host interface (the behaviour) |
| `wasm_tools_json` | get JSON Schemas, and convert JSON to native values and back |
| `wasm_tools_mcp` | expose tools to MCP clients (`register/2`, `unregister/1`) |

`wasm_tools_component`, `wasm_tools_call`, `wasm_tools_policy` and
`wasm_tools_registry` are internal, and so is any function without
documentation. Their pages explain options the API takes, such as the
capabilities and limits in `wasm_tools_policy`, but do not call them: they can
change in any release.

## Contributing

[Contributing] gives the checks every change passes and the rules a review
holds you to; [Architecture](docs/architecture.md) tells you where the code for
each part lives.

## License

Apache-2.0, in the `LICENSE` file.

[Quick start]: docs/quick-start.md
[Documentation]: https://hexdocs.pm/erlang_wasm_tools
[Features]: docs/features.md
[Changelog]: CHANGELOG.md
[Contributing]: CONTRIBUTING.md
[Write your first component]: docs/first-component.md
[MCP]: docs/mcp.md
[barrel_mcp]: https://github.com/barrel-platform/barrel_mcp
[erlang_wasm]: https://github.com/benoitc/erlang_wasm