Packages

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

Current section

Files

Jump to
erlang_wasm_tools docs host-interfaces.md
Raw

docs/host-interfaces.md

# Host interfaces

A component can import an interface of your application, such as a key-value
store, a logger or a business rule, and you implement it in Erlang. You need
this page when a component's WIT has an `import` that is not WASI. The types come
from the component's own declaration: you write no descriptor.

The `custom_host` test component imports this interface and exports `run`, which
reads a key, marks it as seen, and answers `key=value`:

```text
package example:app;

interface kv {
    get: func(key: string) -> option<string>;
    put: func(key: string, value: string);
}

world app {
    import kv;
    export run: func(key: string) -> string;
}
```

## Implement it with a module

```erlang
-module(my_kv_host).
-behaviour(wasm_tools_host).
-export([handle/3]).

handle(<<"get">>, [Key], #{context := #{tenant := Tenant}}) ->
    case my_store:get(Tenant, Key) of
        {ok, Value} -> {some, Value};
        not_found -> none
    end;
handle(<<"put">>, [Key, Value], #{context := #{tenant := Tenant}}) ->
    ok = my_store:put(Tenant, Key, Value),
    undefined.
```

and give it at load:

```erlang
{ok, App} = wasm_tools:load({file, "app.component.wasm"},
                            #{host => #{<<"example:app/kv">> => my_kv_host}}).
```

## Or with a fun

<!-- check: run -->
```erlang
{ok, _} = application:ensure_all_started(wasm_tools),
Store = ets:new(kv, [public]),
true = ets:insert(Store, {<<"k">>, <<"v">>}),
Kv = fun(<<"get">>, [K], _Ctx) ->
             case ets:lookup(Store, K) of
                 [{_, V}] -> {some, V};
                 [] -> none
             end;
        (<<"put">>, [K, V], _Ctx) ->
             true = ets:insert(Store, {K, V}),
             undefined
     end,
{ok, App} = wasm_tools:load({file, "test/fixtures/component/custom_host.component.wasm"},
                            #{host => #{<<"example:app/kv">> => Kv}}),
wasm_tools:call(App, <<"run">>, #{<<"key">> => <<"k">>}).
%% => {ok, <<"k=v">>}
```

## Pass a context

Whatever a call gives as `context` reaches the handler, so one component serves
several tenants or users without global state:

<!-- check: run -->
```erlang
Tenant = fun(<<"get">>, [K], #{context := #{tenant := T}}) -> {some, <<T/binary, "/", K/binary>>};
            (<<"put">>, _, _) -> undefined
         end,
{ok, App2} = wasm_tools:load({file, "test/fixtures/component/custom_host.component.wasm"},
                             #{host => #{<<"example:app/kv">> => Tenant}}),
wasm_tools:call(App2, <<"run">>, #{<<"key">> => <<"k">>}, #{context => #{tenant => <<"acme">>}}).
%% => {ok, <<"k=acme/k">>}
```

The handler's `Ctx` holds:

| key | value |
| --- | --- |
| `context` | the call's `context` option, `undefined` without one |
| `tool` | the name of the tool called |
| `interface` | the interface, without its version |
| `deadline` | when the call stops, in `erlang:monotonic_time(millisecond)`, or `infinity` |

## What a handler must know

- It runs in the call's process, so it stops with the call: a deadline or a
  cancel stops it too. Work it hands to another process is its own to stop.
- An exception in it traps the call: `{error, #{class := trap, kind := host_error}}`.
- Its return value is checked against the function's result type, as arguments
  are; a value that does not fit traps the call.
- `Func` is a binary and nothing a component names becomes an atom.
- A component whose imports are not all provided does not load:
  `{error, #{class := link, kind := missing_import, ctx := #{missing := [...]}}}`.

<!-- check: run -->
```erlang
wasm_tools:load({file, "test/fixtures/component/custom_host.component.wasm"}).
%% => {error, #{class := link, kind := missing_import}}
```

## Test a handler

A handler module is a plain module: test `handle/3` directly, with the context
you expect, before you put a component in front of it.

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