FastMCP Middleware
73%
Hooks: Choosing the Right Level
FastMCP provides hooks at three levels of specificity, and multiple hooks fire for a single request in a chain from general to specific. When a client calls a tool, FastMCP processes on_message first, then on_request, then on_call_tool. Each hook in the chain calls call_next to reach the next one, so if you override both on_message and on_call_tool in the same middleware, both will run.
| Hook | Fires when… | Typical use case |
|---|---|---|
on_message | Any MCP message arrives | Observability, broad logging |
on_request | A request expecting a response arrives | Authentication, authorization |
on_notification | A fire-and-forget notification arrives | Async side effects, event logging |
on_initialize | A client connects and initialises |
Practical MCP with FastMCP & LangChain
Engineering the Agentic ExperienceEnroll now to unlock current content and receive all future updates for free. Your purchase supports the author and fuels the creation of more exciting content. Act fast, as the price will rise as the course nears completion!
