•
By Paul Scanlon Introducing Tool Hooks for Mastra Agents
You can now add tool hooks to your Mastra agents, letting you log, audit, and validate tool calls before, during, and after they run — or block them entirely.
Agent-level hooks fire at two points during a tool call:
beforeToolCall: Just before the tool is called — can block the call or return a pre-defined resultafterToolCall: After the tool has run and returned its output, or thrown an error (skipped when the call is blocked)
Tool-level hooks fire at four separate points during tool calls:
onInputStart: When the model has picked a tool and is about to start writing its argumentsonInputDelta: On each chunk of the arguments as they stream inonInputAvailable: When the full arguments are parsed and validated, ready to be passed to the toolonOutput: After the tool has run and returned its output (only when execution succeeded)
When using .stream(), tool-level hooks fire in real time, letting you monitor different stages of the execution. With .generate(), the same hooks fire in the same order, but won’t be surfaced until the agent’s run is complete.