wasm_tools_json (wasm_tools v0.1.0)
View SourceJSON 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.
| WIT | JSON Schema | JSON value |
|---|---|---|
bool | boolean | true |
s8 to s32, u8 to u32 | integer with minimum and maximum | 42 |
s64, u64 | anyOf: an integer in range, or a decimal string | a number up to 2^53-1 in magnitude, a string beyond |
f32, f64 | number | NaN and the infinities have no JSON form and are refused |
char | a string of one character | "a" |
string | string | "text" |
list<u8> | string with contentEncoding: base64 | "aGk=" |
list<T> | array | [1, 2] |
| record | object; every field required except an option | every field written, none as null |
| tuple | array with prefixItems, fixed length | [1, "a"] |
| variant | oneOf objects with a tag and, for a case with a payload, a val | {"tag": "circle", "val": 2.0} |
| enum | string with enum | "red" |
| flags | array 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
One function: its named parameters and its result.
-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.
A JSON term, as OTP's json:decode/1 returns it.
Functions
-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.
A native value of type Desc, from its JSON form.
-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.
-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.
The JSON form of a native value of type Desc.
The JSON Schema of one value type.