wasm_tools_mcp (wasm_tools v0.1.0)

View Source

Expose tools to MCP clients through barrel_mcp.

The same tools you call from Erlang can be called by an MCP client, with no MCP code in the component and no second execution path: an MCP call runs exactly as wasm_tools:start_call/4 does, in a runner of its own, with the component's limits and capabilities.

{ok, Reg} = wasm_tools:start_link(#{}),
{ok, _} = wasm_tools:add(Reg, Calc, #{namespace => <<"calc">>}),
{ok, Exposed} = wasm_tools_mcp:register(Reg, #{tools => [<<"calc_add">>]}),
...
ok = wasm_tools_mcp:unregister(Exposed).

barrel_mcp (4.9 or later) must be in your release and started; wasm_tools does not depend on it otherwise.

  • Who owns a tool. Tools registered from a registry belong to the registry process: wasm_tools:remove/2 takes them out of MCP before it returns, and when the registry stops barrel_mcp drops them. Tools registered from a component belong to the process that registers them and need a namespace.
  • Names. A registry's tools keep their names. A component's are named as a registry names them (Namespace_Name). A name barrel_mcp already has, a native tool of your application for instance, is refused, never replaced. scope registers the tools for that barrel_mcp listener scope only.
  • Definitions. Input and output schemas come from the component's types (wasm_tools_json). title, description and annotations (read_only, destructive, idempotent, open_world) come from the meta you gave at load, never from the component; openWorldHint is set when the component may reach the network. barrel_mcp's own timeout is set past the tool's deadline, so the tool's deadline answers first.
  • Results. A value is structuredContent {"result": V}, with the same JSON as text. A WIT err, an argument that does not fit, a trap, an exhausted limit, a deadline or a cancel is a tool error (isError: true).
  • Cancel. A client's cancel stops the call at once: the handler watches for barrel_mcp's {cancel, RequestId} while it waits.
  • Who may call. barrel_mcp checks an auth provider's visible/4 on tools/call too (its default since 4.9.0).

Summary

Functions

Register tools with barrel_mcp.

Take tools out of MCP. Only the tools the owner registered are removed.

Types

error()

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

exposed()

-type exposed() ::
          #{names := [binary()],
            owner := pid(),
            registry := wasm_tools:registry() | undefined,
            scope := atom() | binary() | undefined}.

Functions

register/2

-spec register(wasm_tools:target(), map()) -> {ok, exposed()} | {error, error()}.

Register tools with barrel_mcp.

For a registry: #{tools => [Name] | all} (all by default), scope. For a component: #{namespace := Namespace}, exports (as for wasm_tools:add/3), scope.

unregister/1

-spec unregister(exposed()) -> ok.

Take tools out of MCP. Only the tools the owner registered are removed.