mcp-use
allows you to handle these notifications by providing a message_handler
to the MCPClient
.
Server-Side: Sending Notifications
On the server (usingfastmcp
), you can send notifications from within a tool’s context (ctx
). Here’s an example of a tool that sends multiple types of notifications, including progress updates.
Available Notification Types
Thefastmcp
server context provides several methods for sending specific notifications. Here are the ones you can use and the corresponding type to check for in your client’s message_handler
:
ctx.report_progress(...)
sends atypes.ProgressNotification
. This is used to report the progress of a long-running operation.ctx.send_tool_list_changed()
sends atypes.ToolListChangedNotification
. This signals that the tool list has changed (see Tools documentation).ctx.send_resource_list_changed()
sends atypes.ResourceListChangedNotification
. This signals that the resource list has changed (see Resources documentation).ctx.send_prompt_list_changed()
sends atypes.PromptListChangedNotification
. This signals that the prompt list has changed (see Prompts documentation).
Client-Side: Handling Notifications
On the client, you create amessage_handler
function and pass it to the MCPClient
. This function will receive all messages from the server, including notifications.
Since the message_handler
receives all message types (requests, notifications, exceptions), you need to check the type of the incoming message to handle it correctly. Notifications are of type mcp.types.ServerNotification
, and their specific type is stored in the .root
attribute.