MCP servers can send various notifications to the client to signal events or provide updates on long-running tasks. This is distinct from logging, which is handled separately.
mcp-use
allows you to handle these notifications by providing a message_handler
to the MCPClient
.
fastmcp
), 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.
fastmcp
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 a types.ProgressNotification
. This is used to report the progress of a long-running operation.ctx.send_tool_list_changed()
sends a types.ToolListChangedNotification
. This signals that the tool list has changed (see Tools documentation).ctx.send_resource_list_changed()
sends a types.ResourceListChangedNotification
. This signals that the resource list has changed (see Resources documentation).ctx.send_prompt_list_changed()
sends a types.PromptListChangedNotification
. This signals that the prompt list has changed (see Prompts documentation).message_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.