2.9.1
MCP clients can receive various types of messages from servers, including requests that need responses and notifications that don’t. The message handler provides a unified way to process all these messages.
Function-Based Handler
The simplest way to handle messages is with a function that receives all messages:Message Handler Class
For fine-grained targeting, FastMCP provides aMessageHandler class you can subclass to take advantage of specific hooks:
Available Handler Methods
All handler methods receive a single argument - the specific message type:Message Handler Methods
Any MCP message
Called for ALL messages (requests and notifications)
mcp.types.ClientRequest
Called for requests that expect responses
mcp.types.ServerNotification
Called for notifications (fire-and-forget)
mcp.types.ToolListChangedNotification
Called when the server’s tool list changes
mcp.types.ResourceListChangedNotification
Called when the server’s resource list changes
mcp.types.PromptListChangedNotification
Called when the server’s prompt list changes
mcp.types.ProgressNotification
Called for progress updates during long-running operations
mcp.types.LoggingMessageNotification
Called for log messages from the server
Example: Handling Tool Changes
Here’s a practical example of handling tool list changes:Handling Requests
While the message handler receives server-initiated requests, for most use cases you should use the dedicated callback parameters instead:- Sampling requests: Use
sampling_handler - Progress requests: Use
progress_handler - Log requests: Use
log_handler