You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/filesystem/README.md
+51-1Lines changed: 51 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,58 @@ Node.js server implementing Model Context Protocol (MCP) for filesystem operatio
9
9
- Move files/directories
10
10
- Search files
11
11
- Get file metadata
12
+
- Dynamic directory access control via [Roots](https://modelcontextprotocol.io/docs/concepts/roots)
13
+
14
+
## Directory Access Control
15
+
16
+
The server uses a flexible directory access control system. Directories can be specified via command-line arguments or dynamically via [Roots](https://modelcontextprotocol.io/docs/concepts/roots).
17
+
18
+
### Method 1: Command-line Arguments
19
+
Specify Allowed directories when starting the server:
20
+
```bash
21
+
mcp-server-filesystem /path/to/dir1 /path/to/dir2
22
+
```
23
+
24
+
### Method 2: MCP Roots (Recommended)
25
+
MCP clients that support [Roots](https://modelcontextprotocol.io/docs/concepts/roots) can dynamically update the Allowed directories.
26
+
27
+
Roots notified by Client to Server, completely replace any server-side Allowed directories when provided.
28
+
29
+
**Important**: If server starts without command-line arguments AND client doesn't support roots protocol (or provides empty roots), the server will throw an error during initialization.
30
+
31
+
This is the recommended method, as this enables runtime directory updates via `roots/list_changed` notifications without server restart, providing a more flexible and modern integration experience.
32
+
33
+
### How It Works
34
+
35
+
The server's directory access control follows this flow:
36
+
37
+
1.**Server Startup**
38
+
- Server starts with directories from command-line arguments (if provided)
39
+
- If no arguments provided, server starts with empty allowed directories
40
+
41
+
2.**Client Connection & Initialization**
42
+
- Client connects and sends `initialize` request with capabilities
43
+
- Server checks if client supports roots protocol (`capabilities.roots`)
console.error(`Updated allowed directories from MCP roots: ${validatedRootDirs.length} valid directories`);
905
+
}else{
906
+
console.error("No valid root directories provided by client");
907
+
}
908
+
}
909
+
910
+
// Handles dynamic roots updates during runtime, when client sends "roots/list_changed" notification, server fetches the updated roots and replaces all allowed directories with the new roots.
console.error("Client returned no roots set, keeping current settings");
934
+
}
935
+
}catch(error){
936
+
console.error("Failed to request initial roots from client:",errorinstanceofError ? error.message : String(error));
937
+
}
938
+
}else{
939
+
if(allowedDirectories.length>0){
940
+
console.error("Client does not support MCP Roots, using allowed directories set from server args:",allowedDirectories);
941
+
}else{
942
+
thrownewError(`Server cannot operate: No allowed directories available. Server was started without command-line directories and client either does not support MCP roots protocol or provided empty roots. Please either: 1) Start server with directory arguments, or 2) Use a client that supports MCP roots protocol and provides valid root directories.`);
943
+
}
944
+
}
945
+
};
946
+
893
947
// Start server
894
948
asyncfunctionrunServer(){
895
949
consttransport=newStdioServerTransport();
896
950
awaitserver.connect(transport);
897
951
console.error("Secure MCP Filesystem Server running on stdio");
0 commit comments