|
| 1 | +--- |
| 2 | +title: Roots |
| 3 | +type: docs |
| 4 | +weight: 8 |
| 5 | +description: "MCP protocol specification for root directory and file access" |
| 6 | +draft: false |
| 7 | +params: |
| 8 | + author: Anthropic |
| 9 | +keywords: ["mcp", "roots", "directories", "files", "protocols"] |
| 10 | +--- |
| 11 | + |
| 12 | +Roots enable clients to provide a list of root directories or files that servers can operate on. This allows servers to understand which parts of the filesystem they have access to and should work with. Servers can request the list of roots from clients that support this capability, and clients can notify servers when the list of available roots changes. |
| 13 | + |
| 14 | +## Capabilities |
| 15 | + |
| 16 | +To indicate support for roots, clients MUST include a `roots` capability in their `ClientCapabilities` during initialization: |
| 17 | + |
| 18 | +{{<tabs items="Basic,With Notifications">}} |
| 19 | + {{<tab>}} |
| 20 | +```json |
| 21 | +{ |
| 22 | + "capabilities": { |
| 23 | + "roots": { } |
| 24 | + } |
| 25 | +} |
| 26 | +``` |
| 27 | + {{</tab>}} |
| 28 | + {{<tab>}} |
| 29 | +```json |
| 30 | +{ |
| 31 | + "capabilities": { |
| 32 | + "roots": { |
| 33 | + "listChanged": true |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + {{</tab>}} |
| 39 | +{{</tabs>}} |
| 40 | + |
| 41 | +The optional `listChanged` property indicates whether the client will send notifications when the root list changes. |
| 42 | + |
| 43 | +Servers SHOULD check for this capability before attempting to use any roots functionality. |
| 44 | + |
| 45 | +## Concepts |
| 46 | + |
| 47 | +### Root |
| 48 | + |
| 49 | +A Root in the Model Context Protocol (MCP) represents a directory or file that a server is allowed to operate on. Each Root is uniquely identified by a URI (currently restricted to file:// URIs) and may have an optional human-readable name. Roots define the boundaries of where servers can work within the filesystem. |
| 50 | + |
| 51 | +{{< callout type="info" >}} |
| 52 | +Only URI's starting with **file://** are accepted. |
| 53 | +{{< /callout >}} |
| 54 | + |
| 55 | +## Use Cases |
| 56 | + |
| 57 | +Common use cases for roots include defining workspace directories, project repositories, or specific files that a server should analyze or modify. Here are some examples: |
| 58 | + |
| 59 | +### Project Directory |
| 60 | + |
| 61 | +A root representing a project workspace: |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "uri": "file:///home/user/projects/myproject", |
| 66 | + "name": "My Project" |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### Multiple Repositories |
| 71 | + |
| 72 | +Multiple roots for different Git repositories: |
| 73 | + |
| 74 | +```json |
| 75 | +[ |
| 76 | + { |
| 77 | + "uri": "file:///home/user/repos/frontend", |
| 78 | + "name": "Frontend Repository" |
| 79 | + }, |
| 80 | + { |
| 81 | + "uri": "file:///home/user/repos/backend", |
| 82 | + "name": "Backend Repository" |
| 83 | + } |
| 84 | +] |
| 85 | +``` |
| 86 | + |
| 87 | +## Diagram |
| 88 | + |
| 89 | +The following diagram visualizes the interaction between client and server for roots: |
| 90 | + |
| 91 | +```mermaid |
| 92 | +sequenceDiagram |
| 93 | + participant Server |
| 94 | + participant Client |
| 95 | +
|
| 96 | + Note over Server,Client: Server requests roots list |
| 97 | + Server->>Client: roots/list |
| 98 | + Client-->>Server: ListRootsResult |
| 99 | +
|
| 100 | + Note over Server,Client: Client updates roots |
| 101 | + opt Roots Changed |
| 102 | + Client-)Server: notifications/roots/list_changed |
| 103 | + Server->>Client: roots/list |
| 104 | + Client-->>Server: ListRootsResult |
| 105 | + end |
| 106 | +``` |
| 107 | + |
| 108 | +## Messages |
| 109 | + |
| 110 | +This section defines the protocol messages for root management in the Model Context Protocol (MCP). |
| 111 | + |
| 112 | +### Listing Roots |
| 113 | + |
| 114 | +#### Request |
| 115 | + |
| 116 | +To retrieve a list of available roots from the client, the server MUST send a `roots/list` request. |
| 117 | + |
| 118 | +Method: `roots/list` |
| 119 | +Params: None |
| 120 | + |
| 121 | +Example: |
| 122 | +```json |
| 123 | +{ |
| 124 | + "jsonrpc": "2.0", |
| 125 | + "id": 1, |
| 126 | + "method": "roots/list" |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +#### Response |
| 131 | + |
| 132 | +The client MUST respond with a `ListRootsResult` containing: |
| 133 | + |
| 134 | +- `roots`: An array of `Root` objects |
| 135 | + |
| 136 | +A `Root` object consists of: |
| 137 | +- `uri`: A URI string identifying the root location. Currently only `file://` URIs are supported. This field is required. |
| 138 | +- `name`: An optional string providing a human-readable name for the root. |
| 139 | + |
| 140 | +Example: |
| 141 | +```json |
| 142 | +{ |
| 143 | + "jsonrpc": "2.0", |
| 144 | + "id": 1, |
| 145 | + "result": { |
| 146 | + "roots": [ |
| 147 | + { |
| 148 | + "uri": "file:///home/user/projects/myproject", |
| 149 | + "name": "My Project" |
| 150 | + }, |
| 151 | + { |
| 152 | + "uri": "file:///home/user/repos/backend", |
| 153 | + "name": "Backend Repository" |
| 154 | + } |
| 155 | + ] |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +### Root List Changed Notification |
| 161 | + |
| 162 | +If the client supports the `listChanged` capability for roots, it MUST send a `notifications/roots/list_changed` notification when the list of available roots changes. |
| 163 | + |
| 164 | +#### Notification |
| 165 | + |
| 166 | +Method: `notifications/roots/list_changed` |
| 167 | +Params: None |
| 168 | + |
| 169 | +Example: |
| 170 | +```json |
| 171 | +{ |
| 172 | + "jsonrpc": "2.0", |
| 173 | + "method": "notifications/roots/list_changed" |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +Upon receiving this notification, servers SHOULD request an updated root list using the `roots/list` method to ensure they have the most up-to-date information. |
| 178 | + |
| 179 | +## Error Handling |
| 180 | + |
| 181 | +Servers MUST be prepared to handle cases where: |
| 182 | +- The client does not support roots |
| 183 | +- Listed roots become unavailable |
| 184 | +- Access to roots is denied |
| 185 | + |
| 186 | +The client SHOULD return appropriate error responses in these cases. |
| 187 | + |
| 188 | +Example error response: |
| 189 | +```json |
| 190 | +{ |
| 191 | + "jsonrpc": "2.0", |
| 192 | + "id": 1, |
| 193 | + "error": { |
| 194 | + "code": -32601, |
| 195 | + "message": "Roots not supported", |
| 196 | + "data": { |
| 197 | + "reason": "Client does not have roots capability" |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +## Security Considerations |
| 204 | + |
| 205 | +Implementations MUST carefully consider: |
| 206 | + |
| 207 | +- Access control for root directories and files |
| 208 | +- Validation of root URIs to prevent path traversal attacks |
| 209 | +- Scope limitations to prevent unauthorized access |
| 210 | +- Permission checks before exposing roots |
| 211 | +- Regular validation that roots remain accessible |
| 212 | + |
| 213 | +Clients SHOULD: |
| 214 | +- Only expose roots that the server has permission to access |
| 215 | +- Validate all root URIs before returning them |
| 216 | +- Implement proper filesystem permission checks |
| 217 | +- Monitor for changes in root accessibility |
0 commit comments