- auth
- rate limiting
- caching
- tenancy resolution
- request shaping
- short-circuiting before the handler runs
Example
When to use middleware instead of hooks
Choose middleware when the logic can:- block execution
- change what happens next
- depend on the result of
next() - wrap the handler path
Common mistake
Do not use globalonAfterHandle when you really need to prevent execution. If something should stop a tool call, it belongs in middleware.