Guide: Setup mcp-server-filesystem Correctly
Detailed guide on configuring mcp-server-filesystem, including allowedDirectories setup, path specifications, and security best practices. For developers who need AI to safely read/write local files.
Guide: Setup mcp-server-filesystem Correctly
mcp-server-filesystem enables AI Agents to safely read/write local files. This guide covers configuration and security best practices.
What is filesystem tool?
Capabilities:
read_file: Read file contentwrite_file: Write to filelist_directory: List directory contentssearch_files: Search filesget_file_info: Get file information
Basic Configuration
Installation
# Using npx (recommended)
npx -y @modelcontextprotocol/server-filesystem /path/to/allowed/dir
# Or global install
npm install -g @modelcontextprotocol/server-filesystem
Claude Code Config
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/username/Documents",
"/Users/username/Projects"
]
}
}
}
allowedDirectories Details
Why Whitelist?
Security: Prevent AI from accessing sensitive files
- ❌ Do not open root
/ - ❌ Do not open system dirs
/etc,/usr - ❌ Do not open home
~ - ✅ Only open project directories
Multiple Directories
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/username/project-a",
"/Users/username/project-b",
"/Users/username/Downloads"
]
}
}
}
Path Specifications
Must use absolute paths
✅ /Users/username/project
❌ ~/project
❌ ./project
❌ ../project
Security Best Practices
1. Principle of Least Privilege
Only open necessary directories:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/username/workspace/my-project"
]
}
}
}
2. Separate Read/Write Permissions
Configure multiple filesystem instances:
{
"mcpServers": {
"filesystem-readonly": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/Users/username/readonly-data"]
},
"filesystem-write": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/Users/username/workspace"]
}
}
}
3. Avoid Symlink Issues
Problem: Symlinks may point outside whitelist
Solution: Use real paths
realpath /path/to/symlink
readlink -f /path/to/symlink
Common Issues
Path not allowed
Cause: Directory not in allowedDirectories
Fix: Add directory to allowedDirectories
Permission denied
Cause: OS-level permission insufficient
Fix:
chmod 644 /path/to/file
chmod 755 /path/to/directory
Verify Configuration
- Restart Claude Code
- Type
/mcpto see filesystem tools - Test reading a file
Next Steps
FAQ
Why cannot open root directory?▼
Security reasons. Opening root allows AI to access system files and sensitive configs, risking data leaks or system damage.
Can allowedDirectories use relative paths?▼
No. Must use absolute paths like /Users/username/project, cannot use ~/project or ./project.
How to configure multiple directories?▼
Add multiple paths in args array: "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/1", "/path/2", "/path/3"]
Verification Records
人类专家验证
官方机器人验证