Resources

Resources are static or dynamic data provided by the MCP server to the LLM. Unlike tools, resources are read-only and allow the server to expose files, database records, or live telemetry data to the model.

In mcp-cpp-sdk, resources can be registered either directly or via templates. Resources are identified by URIs, which should follow a hierarchical structure appropriate for your application.

Using Resources

add_text_resource() registers a resource whose contents are the text a function returns, read again on each resources/read. In sync mode the function returns std::string; in async mode it is a coroutine that returns mcp::Task<std::string>:

Sync mode
server.add_text_resource("greeter://motd", "Message of the day",
                         [] { return std::string("Be kind to each other."); });
Async mode
server.add_text_resource("greeter://motd", "Message of the day",
                         []() -> mcp::Task<std::string> { co_return "Be kind to each other."; });

The MIME type is text/plain unless a fourth argument names another. A Resource passed in place of the URI and name sets the title, description, annotations, icons and size as well.

A client reads a resource with read_resource(). Its contents hold one TextResourceContents or BlobResourceContents per part:

Sync mode
const auto motd = client.sync().read_resource("greeter://motd");
std::cout << std::get<mcp::TextResourceContents>(motd.contents.at(0)).text << "\n";
Async mode
const auto motd = co_await client.read_resource("greeter://motd");
std::cout << std::get<mcp::TextResourceContents>(motd.contents.at(0)).text << "\n";

Static Resources

A static resource is a direct mapping from a fixed URI to a specific value or handler. These are ideal for configuration files, documentation, or fixed data sets that don’t change their identification scheme.

Register metadata and a typed read handler with add_resource:

#include <mcp/mcp.hpp>

mcp::Resource resource;
resource.uri = "mcp://logs/system";
resource.name = "System logs";
resource.mimeType = "text/plain";

server.add_resource<mcp::ReadResourceRequestParams, mcp::ReadResourceResult>(
    resource, [](mcp::ReadResourceRequestParams request) {
        mcp::TextResourceContents contents;
        contents.uri = std::move(request.uri);
        contents.mimeType = "text/plain";
        contents.text = "System is running normally.";

        mcp::ReadResourceResult result;
        result.contents.emplace_back(std::move(contents));
        return result;
    });

For more complex resources, you can use the structured protocol types:

Example of a structured static resource registration.
 1Resource config_resource;
 2config_resource.uri = "config://app/settings";
 3config_resource.name = "App Settings";
 4config_resource.description = "Current application settings";
 5config_resource.mimeType = "application/json";
 6
 7server.add_resource<ReadResourceRequestParams, ReadResourceResult>(
 8    config_resource, [](const ReadResourceRequestParams& params) -> ReadResourceResult {
 9        TextResourceContents contents;
10        contents.uri = params.uri;
11        contents.text = R"({"theme":"dark","language":"en"})";
12        contents.mimeType = "application/json";
13        ReadResourceResult result;
14        result.contents.emplace_back(std::move(contents));
15        return result;
16    });

Resource Templates

Resource templates let one handler serve a family of URIs. The current matcher supports the common RFC 6570 expression forms for routing, but it is not a full RFC 6570 expansion or variable-extraction engine. The handler receives the full requested URI and can parse or validate application-specific segments itself.

mcp::ResourceTemplate logs;
logs.uriTemplate = "mcp://logs/{node}";
logs.name = "Node logs";
logs.mimeType = "text/plain";

server.add_resource_template<mcp::ReadResourceRequestParams,
                             mcp::ReadResourceResult>(
    logs, [](mcp::ReadResourceRequestParams request) {
        mcp::TextResourceContents contents;
        contents.uri = std::move(request.uri);
        contents.mimeType = "text/plain";
        contents.text = "Logs for requested node";

        mcp::ReadResourceResult result;
        result.contents.emplace_back(std::move(contents));
        return result;
    });

Templates are particularly useful when you have a large or open-ended set of resources that share the same schema or purpose.

Dynamic Resources

Dynamic resources are those whose content or availability changes over time. While the registration might look similar to static resources, the handler is invoked every time the client reads the resource, allowing you to return fresh data.

If the list of available resources itself changes (e.g., new files are created), you should notify the client so they can refresh their cache:

// After adding or removing resources
co_await server.notify_resources_list_changed();

Resource Subscriptions

Clients can subscribe to resources to receive notifications when their content changes. This is essential for live data like logs, telemetry, or collaborative documents.

The server can track these subscriptions and perform actions (like starting a polling loop or opening a file watch) when a client expresses interest.

Handling resource subscriptions and unsubscriptions.
1server->on_subscribe([subscribed](const std::string& uri) { subscribed->push_back(uri); });
2server->on_unsubscribe([unsubscribed](const std::string& uri) { unsubscribed->push_back(uri); });

For more details on subscriptions and the notifications they deliver, see Notifications and Subscriptions.

Resource Change Notifications

When a resource’s content changes, call notify_resource_updated on the Server instance for the relevant session. The method sends only when that session subscribed to the exact URI, with resources/subscribe in a session of an older revision or in the filter of a subscriptions/listen stream in a 2026-07-28 session (see Notifications and Subscriptions). In a multi-session deployment, the application is responsible for invoking the notification on each relevant per-session server instance.

// Notify subscribers that a specific resource has changed
co_await server.notify_resource_updated("mcp://status/counter");

The notification tells a subscribed client that it should read the resource again; delivery and refresh timing still depend on the connection and client.

Triggering a resource update notification.
1std::cout << "[Server] Triggering notify_resource_updated\n";
2co_await server.notify_resource_updated(resource_uri);

Resource Capabilities

A server that has a resource or resource template advertises the resources capability on its own. What registration cannot imply, such as subscriptions or list-changed notifications, is set in the capabilities the server is constructed with, or on server.capabilities() before it serves:

mcp::ServerCapabilities caps;
caps.resources = mcp::ServerCapabilities::ResourcesCapability{
    .subscribe = true // Enable support for subscriptions
};

mcp::Server server(info, std::move(caps));

Best Practices

  • Use Descriptive URIs: Follow a clear scheme like myapp://records/{id}.

  • MIME Types: Always provide accurate MIME types to help the client interpret the data.

  • Error Handling: Throwing an exception in a resource handler will automatically return an appropriate JSON-RPC error to the client.

  • Context Awareness: A resource handler can use Context for logging and reverse requests. Resource reads do not currently propagate a request progressToken into the handler context, so do not rely on progress reporting for them. See Server Context for more information.