Skip to main content
New in version 2.9.1 Use this when you need to react to server-side changes like tool list updates or resource modifications. MCP servers can send notifications to inform clients about state changes. The message handler provides a unified way to process these notifications.

Handling Notifications

The simplest approach is a function that receives all messages and filters for the notifications you care about:

MessageHandler Class

For fine-grained targeting, subclass MessageHandler to use specific hooks:

Handler Template

List Change Notifications

A practical example of maintaining a tool cache that refreshes when tools change:

Server Requests

While the message handler receives server-initiated requests, you should use dedicated callback parameters for most interactive scenarios: The message handler is primarily for monitoring and handling notifications rather than responding to requests.