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:

  1. beforeToolCall: Just before the tool is called — can block the call or return a pre-defined result
  2. afterToolCall: 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:

  1. onInputStart: When the model has picked a tool and is about to start writing its arguments
  2. onInputDelta: On each chunk of the arguments as they stream in
  3. onInputAvailable: When the full arguments are parsed and validated, ready to be passed to the tool
  4. onOutput: 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.

Hey!

Leave a reaction and let me know how I'm doing.

  • 0
  • 0
  • 0
  • 0
  • 0
Powered byNeon
Close