Sub topic 13
Utilities: list-changed notifications, progress, cancellation, and logging
This chapter assumes you know the JSON-RPC request/notification split and the initialization handshake from earlier chapters. It also assumes you have seen a tool call. The utilities here are small features that make long-running or changing servers easier to use.
Notifications versus requests
A notification is a JSON-RPC message with no id, so the receiver sends no reply. All four utilities rely on notifications, because they report events instead of asking for results.
| Utility | Direction | Message |
|---|---|---|
| List changed | server to client | `notifications/tools/list_changed`, plus `resources` and `prompts` variants |
| Progress | either way | `notifications/progress` |
| Cancellation | either way | `notifications/cancelled` |
| Logging | server to client | `notifications/message` |
List-changed notifications
A server's tool list can change at runtime, for example when a plugin loads. The server declares this during initialization with "tools": {"listChanged": true}. When the list changes, it sends:
{"jsonrpc": "2.0", "method": "notifications/tools/list_changed"}The notification carries no list. The client must call tools/list again to get the new one. Resources and prompts work the same way. A client should only expect these notifications for capabilities where the server declared listChanged.
Progress
A client asks for progress by adding a progressToken to the request's _meta field. The token must be unique among that client's active requests.
{"jsonrpc": "2.0", "id": 7, "method": "tools/call", "params": {"name": "slow_count", "arguments": {"n": 3}, "_meta": {"progressToken": "job-1"}}}The server can then send updates that reference the token:
{"jsonrpc": "2.0", "method": "notifications/progress", "params": {"progressToken": "job-1", "progress": 1, "total": 3}}The progress value must increase with each update. total is optional, since some work has no known size. If the request had no token, the server should send no progress.
Cancellation
Either side can cancel a request it sent earlier by naming its id:
{"jsonrpc": "2.0", "method": "notifications/cancelled", "params": {"requestId": 7, "reason": "User pressed stop"}}The receiver should stop work and send no response for that request. Cancellation races with completion. A response may already be on the wire, so the sender must ignore late results. The initialize request cannot be cancelled.
Logging
A server that declares the logging capability can send log messages to the client. The client sets a minimum severity with logging/setLevel. Levels run from debug through info, notice, warning, error, critical, and alert to emergency.
{"jsonrpc": "2.0", "method": "notifications/message", "params": {"level": "info", "logger": "slow_count", "data": "Starting count to 3"}}These protocol messages differ from a server's own diagnostics. On the stdio transport, a server must never write logs to stdout, because stdout carries the protocol. Use stderr for local diagnostics.
Using them in the Python SDK
FastMCP gives a tool a Context object when you add a parameter annotated with Context. The context sends log and progress notifications for you.
import asyncio
from mcp.server.fastmcp import Context, FastMCP
mcp = FastMCP("demo")
@mcp.tool()
async def slow_count(n: int, ctx: Context) -> str:
"""Count to n, reporting progress each second."""
await ctx.info(f"Starting count to {n}")
for i in range(1, n + 1):
await asyncio.sleep(1)
await ctx.report_progress(i, n)
return f"Counted to {n}"
if __name__ == "__main__":
mcp.run(transport="stdio")ctx.info becomes a notifications/message. ctx.report_progress sends a progress notification only if the client supplied a token.
Common errors
- Sending
list_changedwithout declaringlistChanged. Clients may ignore it. - Printing debug text to stdout in a stdio server. This corrupts the message stream.
- Reusing a progress token across concurrent requests, or sending a lower
progressvalue than before. - Treating a cancelled request as failed. Cancellation is a normal outcome.
Exercise
A stdio server adds a tool while running. What must it have declared at initialization, which notification does it send, and what must the client do next?
Show answer
It must have declared "tools": {"listChanged": true}. It sends notifications/tools/list_changed, which carries no tool list. The client then calls tools/list to fetch the updated list.
Exercise
A client cancels request 7, but a result for id 7 arrives afterward. What should the client do, and why can this happen?
Show answer
It should ignore the result. Cancellation is a notification that races with completion, so the server may have already sent the response before it read the cancel message.