MCP
View SourceThis page exposes your tools to MCP clients, such as AI agents and editors, through barrel_mcp. The component needs no MCP code: an MCP call runs exactly as a native call does, in a fresh instance with the component's limits and capabilities. You need this page when a model should be able to call your components.
Add barrel_mcp
wasm_tools does not depend on barrel_mcp: add it yourself, 4.9 or later, and start it with your application.
{deps, [{wasm_tools, "~> 0.1", {pkg, erlang_wasm_tools}},
{barrel_mcp, "~> 4.9"}]}.{ok, _} = application:ensure_all_started(barrel_mcp),
{ok, _} = barrel_mcp:start_http_stream(#{port => 9090}).Expose a registry's tools
{ok, Reg} = wasm_tools:start_link(#{name => my_tools}),
Meta = #{<<"add">> => #{title => <<"Add">>,
description => <<"Add two integers">>,
annotations => #{read_only => true, idempotent => true}}},
{ok, Calc} = wasm_tools:load({file, "calculator.component.wasm"}, #{meta => Meta}),
{ok, _} = wasm_tools:add(Reg, Calc, #{namespace => <<"calc">>}),
{ok, Exposed} = wasm_tools_mcp:register(Reg, #{tools => [<<"calc_add">>]}).- The tools belong to the registry:
wasm_tools:remove/2takes them out of MCP before it returns, and when the registry stops barrel_mcp drops them. wasm_tools_mcp:unregister(Exposed)takes them out of MCP and leaves them in the registry.toolsisallby default.scoperegisters them for the barrel_mcp listeners started with that scope only.
Expose one component
{ok, Exposed} = wasm_tools_mcp:register(Calc, #{namespace => <<"calc">>}).A component registered directly needs a namespace, and its tools belong to the process that registers them.
What clients see
| MCP | comes from |
|---|---|
name | the tool's name in the registry, or Namespace_Name |
inputSchema, outputSchema | the component's types (JSON mapping) |
title, description | the meta you gave at load; never the component itself |
annotations | meta's read_only, destructive, idempotent, open_world; openWorldHint is true when the component may reach the network |
A name barrel_mcp already has, a native tool of your application for instance,
is never replaced: register/2 answers
{error, #{kind := name_conflict}} and registers nothing.
What a call answers
| the call | MCP result |
|---|---|
| returns a value | structuredContent {"result": V}, and the same JSON as text |
returns a WIT err | isError: true, the error value as JSON text |
| has an argument that does not fit | isError: true, the reason |
| traps, runs out of a limit, passes its deadline | isError: true, a short reason |
| is cancelled by the client | stopped at once |
barrel_mcp's own timeout is set past the tool's deadline, so the tool answers with its own timeout error first.
Who may call
barrel_mcp's auth providers decide who sees and who may call each tool
(visible/4, applied to tools/call too since 4.9.0). A tool that one listener
should serve and another not goes in a scope.
Notes
- barrel_mcp must be started before
register/2; without it the answer is{error, #{kind := barrel_mcp_not_started}}, and without barrel_mcp in the release{error, #{kind := barrel_mcp_unavailable}}. - A type with no JSON form (an option of an option) cannot be exposed:
register/2refuses the whole batch and names the export.