wasm_tools_json (wasm_tools v0.1.0)

View Source

JSON Schema for a component function, and JSON values to native values and back.

A component function's types are Component Model descriptors, the terms erlang_wasm's wasm_component:call/4 takes (s32, {list, string}, {record, [{Name, Desc}]}, ...). This module turns them into JSON Schema 2020-12 and converts between JSON terms, as OTP's json module decodes them (maps with binary keys, lists, binaries, numbers, true, false, null), and the native values erlang_wasm passes and returns. Nothing here runs guest code, and the native API never calls it: JSON is only for consumers that need it, such as MCP.

WITJSON SchemaJSON value
boolbooleantrue
s8 to s32, u8 to u32integer with minimum and maximum42
s64, u64anyOf: an integer in range, or a decimal stringa number up to 2^53-1 in magnitude, a string beyond
f32, f64numberNaN and the infinities have no JSON form and are refused
chara string of one character"a"
stringstring"text"
list<u8>string with contentEncoding: base64"aGk="
list<T>array[1, 2]
recordobject; every field required except an optionevery field written, none as null
tuplearray with prefixItems, fixed length[1, "a"]
variantoneOf objects with a tag and, for a case with a payload, a val{"tag": "circle", "val": 2.0}
enumstring with enum"red"
flagsarray of distinct names["read", "write"], in declaration order
option<T>anyOf [T, null]null for none
result<T, E>oneOf {"ok": T} and {"error": E}{"ok": 1}; a missing arm is null

option<option<T>> is refused: both none and some(none) would be null. Resources, streams, futures and error contexts are refused: a handle means nothing outside the instance that made it.

A function is a map #{params := [{Name, Desc}], result := Desc | none}. Its input is an object of its parameters; its output is an object {"result": V}, because MCP wants structured content to be an object. A function returning result<T, E> has T as its output and its error value reported apart (output/2).

Errors are values, never raised: #{class, kind, msg, ctx}. A JSON value that does not fit is #{class := argument, kind := bad_argument} with ctx holding path, the JSON pointer segments to the value, expected, the descriptor, and got, the value when it is a number, a boolean or null, and its JSON type otherwise.

Summary

Types

One function: its named parameters and its result.

A Component Model value type, as erlang_wasm's wasm_component:call/4 takes it.

A JSON term, as OTP's json:decode/1 returns it.

Functions

A function's arguments, from a JSON object keyed by parameter name, as the native map wasm_tools:call/3 takes.

A native value of type Desc, from its JSON form.

A function's result as the JSON object its output schema describes.

The input and output JSON Schemas of a function.

The JSON form of a native value of type Desc.

The JSON Schema of one value type.

Types

definition()

-type definition() :: #{params := [{binary(), desc()}], result := desc() | none, atom() => term()}.

One function: its named parameters and its result.

desc()

-type desc() ::
          u8 | u16 | u32 | u64 | s8 | s16 | s32 | s64 | f32 | f64 | bool | char | string |
          {list, desc()} |
          {record, [{binary(), desc()}]} |
          {tuple, [desc()]} |
          {enum, [binary()]} |
          {variant, [{binary(), desc() | none}]} |
          {option, desc()} |
          {result, desc() | none, desc() | none} |
          {flags, [binary()]} |
          term().

A Component Model value type, as erlang_wasm's wasm_component:call/4 takes it.

error()

-type error() :: #{class := argument | invalid, kind := atom(), msg := binary(), ctx := map()}.

json()

-type json() :: null | boolean() | number() | binary() | [json()] | #{binary() => json()}.

A JSON term, as OTP's json:decode/1 returns it.

Functions

args/2

-spec args(definition(), json()) -> {ok, #{binary() => term()}} | {error, error()}.

A function's arguments, from a JSON object keyed by parameter name, as the native map wasm_tools:call/3 takes.

A missing option parameter is none; any other missing parameter, and any key that is not a parameter, is an error.

native(Desc, Json)

-spec native(desc(), json()) -> {ok, term()} | {error, error()}.

A native value of type Desc, from its JSON form.

output/2

-spec output(definition(), term()) ->
                {ok, #{binary() => json()} | undefined} | {guest_error, json()} | {error, error()}.

A function's result as the JSON object its output schema describes.

{ok, undefined} for a function without a result. When the function returns result<T, E>, an ok is {ok, #{<<"result">> => T}} and an error is {guest_error, E}, so a caller such as MCP can report it as a tool error.

schema/1

-spec schema(definition()) -> {ok, #{input := map(), output := map() | undefined}} | {error, error()}.

The input and output JSON Schemas of a function.

output is undefined for a function without a result.

value(Desc, Native)

-spec value(desc(), term()) -> {ok, json()} | {error, error()}.

The JSON form of a native value of type Desc.

value_schema(Desc)

-spec value_schema(desc()) -> {ok, map()} | {error, error()}.

The JSON Schema of one value type.