Use this file to discover all available pages before exploring further.
Roots allow MCP clients to inform servers about relevant filesystem locations. This helps servers understand the working context and scope operations to specific directories or projects.
Roots are a client-provided feature. The client declares root URIs during initialization, and servers can request them to understand which directories, projects, or repositories are relevant to the current session.
Roots can be determined dynamically based on runtime conditions:
var options = new McpClientOptions{ Handlers = new() { RootsHandler = (request, cancellationToken) => { var roots = new List<Root>(); // Add current working directory roots.Add(new Root { Uri = new Uri(Directory.GetCurrentDirectory()).AbsoluteUri, Name = "Current Directory" }); // Add git repositories var gitRoot = FindGitRoot(Directory.GetCurrentDirectory()); if (gitRoot is not null) { roots.Add(new Root { Uri = new Uri(gitRoot).AbsoluteUri, Name = "Git Repository" }); } // Add user's home directory roots.Add(new Root { Uri = new Uri(Environment.GetFolderPath( Environment.SpecialFolder.UserProfile )).AbsoluteUri, Name = "Home" }); return ValueTask.FromResult(new ListRootsResult { Roots = roots }); } }};static string? FindGitRoot(string path){ var dir = new DirectoryInfo(path); while (dir is not null) { if (Directory.Exists(Path.Combine(dir.FullName, ".git"))) { return dir.FullName; } dir = dir.Parent; } return null;}
Notify the server when roots change (e.g., user opens a new project):
await using var client = await McpClient.CreateAsync(transport, options);// Later, when roots change...await client.SendNotificationAsync( NotificationMethods.RootsListChangedNotification, new RootsListChangedNotificationParams());
The server will call RootsHandler again to get the updated list.
Servers request roots to understand the client’s context:
// In a server toolvar result = await server.RequestRootsAsync( new ListRootsRequestParams(), cancellationToken);foreach (var root in result.Roots){ Console.WriteLine($"Root: {root.Name} at {root.Uri}");}
Servers can also listen for changes:
server.RegisterNotificationHandler( NotificationMethods.RootsListChangedNotification, async (notification, cancellationToken) => { // Re-request roots to get updated list var result = await server.RequestRootsAsync( new ListRootsRequestParams(), cancellationToken ); Console.WriteLine($"Roots updated: {result.Roots.Count} roots"); });