Wymcp.Tool.Registration (Wymcp v0.10.1)

View Source

What the registration moment builds for each tool, and the one value every runtime reader takes: tools/list serves its definition, tools/call finds the tool by its name and checks structured content against its output root, dispatch's gates and Wymcp.Help read its actions, the value gate reads its input roots, the header-binding check reads its header annotations, and every error and hint text names the tool by its name. No reader calls a tool's callbacks.

Read once, then held

build!/1 calls each callback that feeds a definition exactly once — Wymcp.Tool.actions/0, Wymcp.Tool.name/0, Wymcp.Tool.description/0, Wymcp.Tool.title/0 and Wymcp.Tool.annotations/0 where exported, Wymcp.Tool.output_schema/0 and, for a behaviour-only tool, Wymcp.Tool.input_schema/0 — checks each value against the callback-shape invariant (Wymcp.Tool), and builds every field from those values. A macro tool's input schema is built from the actions map already read, because its generated input_schema/0 would read the actions a second time. So what a client is shown, what help describes and what dispatch enforces are one value, whatever a callback would return if it were called again: a callback that reads application config or the clock is read at the build, and that value is the one served until the next build.

The guarantee covers the calls the build makes itself. A behaviour-only tool whose Wymcp.Tool.input_schema/0 returns Wymcp.Tool.Schema.build(__MODULE__) reads Wymcp.Tool.actions/0 a second time inside the same build, through that function: the registration keeps the first read, and the definition's action enum and summaries come from the second. Such a tool's actions/0 must return the same map on both calls.

Wymcp.CallbackReadTest holds the rule as a gate: every call to one of those callbacks in lib/ sits in this module.

Where a build runs

  • Wymcp.Router.init/1 builds one per mount tool, Wymcp.Help last, and stores them as the mount's ordered list.
  • The hook use Wymcp.Tool injects builds one at the tool module's own compile, through Wymcp.Tool.validate_callback_shape!/1, and discards it.
  • A run whose context carries no registration for its tool builds one on the spot (find_or_build!/2), on every such call, with nothing cached — Wymcp.Testing.build_context/1 builds them for the modules a test names. A tool built this way meets every refusal of the build itself — one malformed action fails the whole tool — but not the callback-surface check, which only a mount (Wymcp.CallbackSurface) and the tool's own compile under use Wymcp.Tool run: a module missing a callback the build reads fails here with UndefinedFunctionError, and one missing any other required callback is not refused here at all.

Under a direct forward "/mcp", Wymcp.Router, ... the registration moment recurs per request, so the registrations are built once per build of the mount's configuration, and every reader within one request reads one build.

flowchart TD
    RT[Wymcp.Router] -->|"build!/1"| R[Tool.Registration]
    T[Wymcp.Tool] -->|"build!/1, find_or_build!/2, fetch_action/2"| R
    H[Wymcp.Help] -->|"find_or_build!/2, fetch_action/2"| R
    TS[Wymcp.Testing] -->|"build!/1"| R
    R -->|"check!/2"| AC[Tool.Actions]
    R -->|"build_from_actions/1, header_annotations/1"| SC[Tool.Schema]
    R -->|"build_roots!/2"| VG[Tool.ValueGate]
    R -->|"build/1"| JV[JSV]
    R -->|"name/0, description/0, actions/0, ..."| CB(Consumer Tool)

Summary

Types

t()

A tool's registration: what the registration moment built for it — its name, description, validated actions, definition, schema roots and header annotations, read from its callbacks once, and the one value every runtime reader takes.

Functions

The tool's action names as the wire spells them, sorted.

Builds module's registration, reading each callback that feeds it once. Raises Wymcp.ConfigurationError on the first value it refuses, naming the module and the callback at fault: a return of the wrong shape, an action schema the chain in Wymcp.Tool.Actions refuses, a schema JSV cannot build, a :defaults value its property schema refuses, or a header annotation Wymcp.Tool.Schema.validate_header_annotations!/2 refuses.

The schema of the action named action_str on the wire. Returns {:ok, action, schema}, or {:error, {:unknown_action, names}} carrying the tool's action names as action_names/1 spells them.

The registration of module among registrations — a context's tools — or, where none is there, one built on the spot by build!/1.

Types

t()

@type t() :: %Wymcp.Tool.Registration{
  actions: %{required(atom()) => Wymcp.Tool.action_schema()},
  definition: %{required(String.t()) => term()},
  description: String.t(),
  header_annotations: [map()],
  input_schema_roots: %{required(atom()) => JSV.Root.t()},
  module: module(),
  name: String.t(),
  output_schema_root: JSV.Root.t() | nil
}

A tool's registration: what the registration moment built for it — its name, description, validated actions, definition, schema roots and header annotations, read from its callbacks once, and the one value every runtime reader takes.

Functions

action_names(registration)

The tool's action names as the wire spells them, sorted.

build!(module)

Builds module's registration, reading each callback that feeds it once. Raises Wymcp.ConfigurationError on the first value it refuses, naming the module and the callback at fault: a return of the wrong shape, an action schema the chain in Wymcp.Tool.Actions refuses, a schema JSV cannot build, a :defaults value its property schema refuses, or a header annotation Wymcp.Tool.Schema.validate_header_annotations!/2 refuses.

The actions are read and validated first, because a macro tool's input schema is built from them. Wymcp.Tool.title/0 and Wymcp.Tool.annotations/0 are optional, and are read only when module exports them. That probe does not wait for a module still compiling, so a caller at compile time must know module is compiled: Wymcp.Router.init/1 runs the callback-surface check first, which does wait.

A schema JSV cannot build includes a remote $ref: JSV's default resolvers fetch nothing over the network, so one is refused here rather than resolved.

fetch_action(registration, action_str)

The schema of the action named action_str on the wire. Returns {:ok, action, schema}, or {:error, {:unknown_action, names}} carrying the tool's action names as action_names/1 spells them.

Matched as a string against the declared names, so no atom is made from the wire's input.

find_or_build!(registrations, module)

The registration of module among registrations — a context's tools — or, where none is there, one built on the spot by build!/1.

Looked up by module, never by name: the module is the identity the caller holds, and a hand-built list may carry two tools sharing a name, which a mount refuses. A registration found is used as it is; a built one is not kept.