Agent / Tools from a state machine's transitions

Let an agent call your state machine's transitions

Say your account rules are written once, as a state machine: "withdraw" is allowed only while the account is open and only up to the balance, and the amount must be a positive number of cents. An AI assistant acts on your app through tools: named actions it can call, each with a JSON Schema describing its parameters. Handing it those actions usually means writing the rules a second time: a parameter schema for each tool, a check for when to offer it, and the same guard again before the action runs. The copies drift, and the assistant is offered a tool that then fails, or sends an amount your rules would refuse.

machineTools, from foldkit-agent/metakit, makes each of those tools from the rule itself. A tool's parameters come from the transition's input, it is offered only while the state allows it, and a call the rule refuses is turned down before anything changes.

This guide assumes a Foldkit app: its state is a Model, its events are Messages, and its update function turns a Message into the next Model. It also assumes you have a foldkit-agent contract: the list of your app's Messages that an assistant may send, declared with Agent.expose. The package page shows how to write one, connect it to a running app, and serve it to an assistant through an adapter (a package that speaks one protocol, such as MCP). Install foldkit-agent and @doeixd/metakit (pnpm add foldkit-agent @doeixd/metakit effect). The blocks below continue one file; nothing runs until a host serves the contract, so the results are described in the text.