Roots
Note
Protocol revision 2026-07-28 deprecates roots; the SDK keeps them for peers that use them. See Deprecated Features.
Roots allow a client to declare specific URI prefixes that it has access to. Servers can then discover these roots to understand the boundaries of the client environment, which is particularly useful for tools that need to perform file system operations or access specific resources.
Roots Overview
In the Model Context Protocol, roots represent the “home” or “workspace” directories of a client. By declaring roots, the client informs the server about:
The absolute URIs (typically
file:///) that the server is allowed to interact with.The names of these workspaces to provide context to the LLM.
Using Roots
A tool asks for the client’s roots with ctx.request_roots() and awaits
them, so its handler is a coroutine in both modes, and so is the client’s
handler, which on_roots_list() registers:
server.add_tool("count_roots", "Counts the client's roots", nlohmann::json{{"type", "object"}},
[](mcp::Context& ctx, const nlohmann::json&) -> mcp::Task<nlohmann::json> {
const auto listed = co_await ctx.request_roots();
co_return "The client has " + std::to_string(listed.roots.size()) + " root(s)";
});
client.on_roots_list([](const nlohmann::json&) -> mcp::Task<mcp::ListRootsResult> {
mcp::ListRootsResult roots;
roots.roots.push_back({"file:///home/user/projects", "projects", std::nullopt});
co_return roots;
});
Client-side Setup
A client provides its list of roots using the set_roots() method. This method stores the roots internally, registers a handler for roots/list, and—when called before connect()—automatically advertises the matching client capability.
std::vector<Root> client_roots = {
Root{.uri = "file:///workspace/", .name = "Workspace"},
Root{.uri = "mcp://docs/", .name = "Documentation"},
};
std::cout << "Client: Setting " << client_roots.size() << " root(s)\n";
for (const auto& root : client_roots) {
std::cout << "Client: - " << root.uri;
if (root.name) {
std::cout << " (" << *root.name << ")";
}
std::cout << "\n";
}
client.set_roots(client_roots);
The set_roots method also takes an optional notify parameter. If set to
true, the client sends notifications/roots/list_changed only when its
initialization capabilities advertised roots.listChanged=true. This is a
client capability; servers do not advertise roots support.
In a 2026-07-28 session there is no notifications/roots/list_changed, and
set_roots() sends nothing even with notify set. The client declares the
roots capability in each request instead, when a roots/list handler
exists.
Server-side Discovery
Servers can discover client roots at any time using the request_roots() method available in the Server Context. This is a “reverse RPC” call where the server sends a request to the client. In a 2026-07-28 session the server asks through an input_required result and the client answers on a retry of its request (see Protocol Revisions); this works in the handler of a tools/call, prompts/get or resources/read.
try {
auto roots_result = co_await ctx.request_roots();
Handling Roots Requests
While set_roots() provides a convenient way to manage a static or semi-static list of roots, you can also register a dynamic handler using on_roots_list(). This is useful if the roots need to be calculated on-the-fly when requested.
client.on_roots_list([](const nlohmann::json& params) -> Task<ListRootsResult> {
ListRootsResult result;
result.roots = {
Root{.uri = "file:///dynamic/path", .name = "Dynamic"}
};
co_return result;
});
Roots Change Notifications
When the set of roots changes (e.g., the user opens a new folder in their IDE), the client should inform the server.
Client: Call
set_roots(new_roots, true)to update and notify.Server: If the server wants to react to these changes, it should monitor the
roots/liststate or re-request roots when needed.
Example Walkthrough
The complete implementation of roots discovery can be found in the feature examples. It demonstrates a server tool that, when called, asks the client for its roots and returns them as a text summary.
nlohmann::json tool_schema = {
{"type", "object"},
{"properties", nlohmann::json::object()},
{"required", nlohmann::json::array()},
};
server.add_tool<nlohmann::json, nlohmann::json>(
"discover_roots", "Discovers and displays the client's declared root URIs", tool_schema,
[](Context& ctx, const nlohmann::json&) -> Task<nlohmann::json> {
std::cout << "Tool: Requesting roots from client\n";
try {
auto roots_result = co_await ctx.request_roots();
std::cout << "Tool: Received " << roots_result.roots.size() << " root(s)\n";
std::string output = "Discovered roots:\n";
for (const auto& root : roots_result.roots) {
std::cout << "Tool: - URI: " << root.uri;
if (root.name) {
std::cout << " (name: " << *root.name << ")";
}
std::cout << "\n";
output += " - " + root.uri;
if (root.name) {
output += " (" + *root.name + ")";
}
output += '\n';
}
co_return nlohmann::json{
{
"content",
nlohmann::json::array({nlohmann::json{{"type", "text"}, {"text", output}}}),
},
{"isError", false},
};
} catch (const std::exception& e) {
std::cout << "Tool: Error requesting roots: " << e.what() << "\n";
std::string error_msg = "Error: " + std::string(e.what());
co_return nlohmann::json{
{
"content",
nlohmann::json::array({
nlohmann::json{
{"type", "text"},
{"text", error_msg},
},
}),
},
{"isError", true},
};
}
});