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

The message handler receives only notifications and transport exceptions; server-initiated requests never reach it. Answer those through the client’s dedicated parameters: Progress and log notifications reach the message handler as well, but their dedicated callbacks are simpler to use: