Notifications and Subscriptions
Notifications and subscriptions enable real-time communication between MCP servers and clients. While the standard request-response cycle is suitable for most interactions, notifications allow either side to push updates asynchronously, and subscriptions specifically allow clients to stay informed about changes to resources.
Subscriptions Overview
A client can ask a server to push two kinds of notification: that a list of tools, prompts or resources changed, and that a resource it is interested in changed.
Key benefits include: - Reduced latency for data updates. - Lower overhead compared to polling. - Real-time synchronization for telemetry, logs, or shared state.
How a client asks depends on the protocol revision of the session:
In a 2026-07-28 session the client opens a
subscriptions/listenstream withClient::listen(). See Listening for Notifications.In a session of 2025-11-25 or earlier the client sends
resources/subscribefor each resource URI. See Resource Subscriptions.
The server side is the same in both: the server calls notify_resource_updated()
and the notify_*_list_changed() methods, and the SDK sends each
notification to the clients that asked for it.
Using Resource Subscriptions
A server that lets clients subscribe to its resources sets the subscribe
flag of its resources capability, and calls notify_resource_updated() when a
resource changes. In sync mode a plain-function handler on the handler pool sends
the notification through the blocking server.sync(); in async mode a
coroutine awaits it:
// Registering a resource declares the resources capability; this flag adds subscriptions.
const std::string last_uri = "greeter://last";
auto last = std::make_shared<std::string>("nobody");
auto last_mutex = std::make_shared<std::mutex>();
server.add_text_resource(last_uri, "The last name remembered", [last, last_mutex] {
const std::scoped_lock lock(*last_mutex);
return *last;
});
server.capabilities().resources->subscribe = true;
server.add_tool(
"remember", "Remembers a name", mcp::tool_params({{"name", "string", "The name"}}),
[&server, &last_uri, last, last_mutex](const nlohmann::json& args) -> nlohmann::json {
{
const std::scoped_lock lock(*last_mutex);
*last = args.at("name").get<std::string>();
}
server.sync().notify_resource_updated(last_uri);
return "Remembered";
});
// Registering a resource declares the resources capability; this flag adds subscriptions.
const std::string last_uri = "greeter://last";
auto last = std::make_shared<std::string>("nobody");
auto last_mutex = std::make_shared<std::mutex>();
server.add_text_resource(last_uri, "The last name remembered",
[last, last_mutex]() -> mcp::Task<std::string> {
const std::scoped_lock lock(*last_mutex);
co_return *last;
});
server.capabilities().resources->subscribe = true;
server.add_tool("remember", "Remembers a name", mcp::tool_params({{"name", "string", "The name"}}),
[&server, &last_uri, last,
last_mutex](const nlohmann::json& args) -> mcp::Task<nlohmann::json> {
{
const std::scoped_lock lock(*last_mutex);
*last = args.at("name").get<std::string>();
}
co_await server.notify_resource_updated(last_uri);
co_return "Remembered";
});
server.sync() throws std::logic_error on the thread of the server’s
event loop, where handlers run when no handler executor is set.
A client of protocol revision 2026-07-28 subscribes with listen() and takes
each notification with next(), or its blocking form on sync():
mcp::SubscriptionFilter filter;
filter.resourceSubscriptions = {"greeter://last"};
auto subscription = client.sync().listen(filter);
if (!subscription.unhonored().resourceSubscriptions.empty()) {
throw std::runtime_error("the server does not offer subscriptions to greeter://last");
}
client.sync().call_tool("remember", {{"name", "Ada"}});
const auto event = subscription.sync().next();
if (event) {
std::cout << "updated: " << event->method << "\n";
}
subscription.sync().close();
mcp::SubscriptionFilter filter;
filter.resourceSubscriptions = {"greeter://last"};
auto subscription = co_await client.listen(filter);
if (!subscription.unhonored().resourceSubscriptions.empty()) {
throw std::runtime_error("the server does not offer subscriptions to greeter://last");
}
co_await client.call_tool("remember", ada);
const auto event = co_await subscription.next();
if (event) {
std::cout << "updated: " << event->method << "\n";
}
co_await subscription.close();
Listening for Notifications
Protocol revision 2026-07-28 has no resources/subscribe. A client sends one
subscriptions/listen request that names every notification it opts in to,
and the server keeps that request open and sends the notifications on it.
Client::listen() takes a SubscriptionFilter and returns a
Subscription once the server has acknowledged the request:
mcp::SubscriptionFilter filter;
filter.toolsListChanged = true;
filter.resourceSubscriptions.push_back("mcp://status/counter");
mcp::Subscription subscription = co_await client.listen(filter);
for (;;) {
auto event = co_await subscription.next();
if (!event) {
break; // the stream ended; see subscription.end_reason()
}
std::cout << event->method << "\n";
}
The filter has toolsListChanged, promptsListChanged and
resourcesListChanged flags, a list of resourceSubscriptions URIs, and a
list of taskIds for the notifications of the tasks extension (see
Tasks). The
server first answers with notifications/subscriptions/acknowledged, which
lists the part of the filter it honors. It honors a list-changed type only if
the matching capability sets listChanged, and resource URIs only if the
resources capability sets subscribe. Subscription::acknowledged()
returns what the server honors, and Subscription::unhonored() what it left
out.
Subscription::next() returns the notifications of this stream in the order
they arrived, as a SubscriptionEvent with the method and params. The
params._meta of each carries the id of the listen request under
io.modelcontextprotocol/subscriptionId (g_META_SUBSCRIPTION_ID), which is
how streams on one connection are told apart. Any number of streams may be open
on one client. The client holds at most ClientOptions::subscription_queue_limit
(1024 by default) notifications that next() has not taken yet, and drops the
oldest beyond that; Subscription::dropped_events() counts them.
Subscription::close() ends the stream. A handle does not close its stream
when it is destroyed. Subscription::end_reason() says how a stream ended
(SubscriptionEnd): the server closed it, close() did, the connection
dropped, or the server answered with an error. After a dropped connection,
connect a new client and call listen() again; the server keeps no
subscription across connections.
For a client constructed from an Endpoint, Subscription::sync() makes
next() and close() block until they are done.
Client::listen() throws McpError with g_METHOD_NOT_FOUND in a
session of an older revision.
On the server, nothing changes in the handlers. notify_tools_list_changed(),
notify_prompts_list_changed(), notify_resources_list_changed() and
notify_resource_updated() also send to every open stream that asked for the
notification. A stream stays open until the client cancels it or closes the
connection. Server::close_subscriptions() ends every open stream, and
Server::subscription_count() counts them. The on_subscribe() and
on_unsubscribe() callbacks below belong to resources/subscribe and do
not run for subscriptions/listen.
Which transports carry the stream:
stdio, WebSocket and in-memory transports served by
Server::run().StreamableHttpSessionManager, and soServer::run_http(), as a server-sent event stream. The client must accepttext/event-stream. The stream runs on the manager’s shared modern server, which is the server itself forrun_http(), see Deployment Guide.HttpServerTransport, as a server-sent event stream on the server that reads the transport. The client must accepttext/event-stream. Closing the stream, or closing the transport, ends the subscription.
Resource Subscriptions
In a session of protocol revision 2025-11-25 or earlier, a client subscribes to
a resource URI with the resources/subscribe request to indicate interest in
updates whenever that resource changes. A 2026-07-28 session has no such
request; it lists the URIs in a subscriptions/listen filter instead.
Server-side Subscription Callbacks
Servers can track when clients subscribe to or unsubscribe from resources. This is useful for managing underlying data sources, such as opening a database connection or starting a hardware sensor only when active subscribers exist.
You can register subscription handlers using on_subscribe() and on_unsubscribe():
1server->on_subscribe([subscribed](const std::string& uri) { subscribed->push_back(uri); });
2server->on_unsubscribe([unsubscribed](const std::string& uri) { unsubscribed->push_back(uri); });
Resource Change Notifications
When a resource’s content is updated, the server should notify all active subscribers. The notify_resource_updated() method sends a notifications/resources/updated message to all clients that have an active subscription for the given URI.
1std::cout << "[Server] Triggering notify_resource_updated\n";
2co_await server.notify_resource_updated(resource_uri);
List Change Notifications
Beyond individual resource updates, servers can notify clients when their entire collection of tools, resources, or prompts has changed. This is common when new capabilities are dynamically added or removed at runtime.
The following methods are available for list change notifications:
notify_tools_list_changed(): Sendsnotifications/tools/list_changed.notify_resources_list_changed(): Sendsnotifications/resources/list_changed.notify_prompts_list_changed(): Sendsnotifications/prompts/list_changed.
Example of triggering list notifications:
1std::cout << "[Server] Triggering notify_tools_list_changed\n";
2co_await server.notify_tools_list_changed();
3std::cout << "[Server] Triggering notify_resources_list_changed\n";
4co_await server.notify_resources_list_changed();
5// Not asked for by anything that was honored, so no stream receives it.
6std::cout << "[Server] Triggering notify_prompts_list_changed (nobody listens)\n";
7co_await server.notify_prompts_list_changed();
Client-side Notification Handling
In a 2026-07-28 session the notifications of a subscriptions/listen stream
arrive through Subscription::next(), as shown in Listening for
Notifications. Other notifications, and every notification in a session of an
older revision, go to handlers registered with on_notification(), which maps
a notification method name to a callback function.
1auto transport = start_server(server, executor);
2
3auto client_owner = make_client(std::move(transport), executor, mcp::ClientProtocol::legacy());
4mcp::Client& client = *client_owner;
5
6bool updated = false;
7bool list_changed = false;
8client.on_notification("notifications/resources/updated", [&updated](const json& params) {
9 std::cout << " resource updated: " << params.dump() << '\n';
10 updated = true;
11});
Architecture of Notifications
Notifications in MCP are one-way messages that do not expect a response. This makes them ideal for high-frequency updates or background events where the overhead of a full request-response cycle would be prohibitive.
Methods: Notification methods are prefixed with
notifications/.Parameters: Notifications can carry arbitrary JSON parameters.
Delivery: Notifications are delivered asynchronously over the established transport.
Subscription State Management
While the mcp-cpp-sdk handles the underlying message routing, the server application is responsible for managing the logic associated with subscriptions.
Persistence: Subscriptions are typically session-based and do not persist across reconnections unless re-established by the client.
Scalability: When notifying thousands of clients, consider the performance impact of JSON serialization. The SDK optimizes this where possible by reusing serialized buffers.
Example Walkthrough
examples/features/notifications_subscriptions.cpp demonstrates a complete
flow in a 2026-07-28 session:
Server Setup: The server declares the tools and resources capabilities with
listChanged, andsubscribefor resources. It declares no prompts capability.Registration: The server registers a counter resource.
Listening: The client opens a stream with
listen()for the tools, prompts and resources list changes and the counter’s URI. The acknowledgement leaves the prompt notifications out.Notification: The server calls
notify_resource_updated(),notify_tools_list_changed()andnotify_resources_list_changed().Handling: The client reads each notification with
next().Ending: The client ends the stream with
close(), then opens a second one that the server ends withclose_subscriptions().
The resources/subscribe flow of the older revisions is in
examples/features/legacy_protocols.cpp.
Advanced Notification Patterns
Custom Notifications
While MCP defines several standard notification methods, you can also send custom notifications using the underlying protocol layer if your client and server agree on the method names. However, for most resource-related needs, the standard updated and list_changed methods are preferred.
Throttling and Debouncing
For rapidly changing resources, it is recommended to throttle or debounce notifications on the server side to avoid overwhelming the client. For example, if a log file is being updated multiple times per millisecond, sending a single notification every 100ms is often more efficient.
For a complete working example, see examples/features/notifications_subscriptions.cpp in the source tree.
See Also
Resources: Deep dive into how resources work.
Server Context: Using the request context for logging and progress.
Transports: How notifications are carried over different transport layers.