Filesystem MCP Server
Enables comprehensive filesystem operations including reading/writing files, directory management, file searching, editing with diff preview, compression, hashing, and merging with dynamic directory access control.
README
Filesystem MCP Server
Enterprise-grade Node.js server implementing Model Context Protocol (MCP) for comprehensive filesystem operations with unified tool architecture.
Features
- Unified Tool Architecture: 10 powerful tools instead of 48, optimized for LLM efficiency
- 100% Type Safety: Strict TypeScript with Zod validation, zero
anytypes - Comprehensive File Operations: Read, write, edit, copy, move, delete with multiple modes
- Advanced Search: File search, content search, and fuzzy matching
- Git Integration: Status, log, diff, branch, show, and blame operations
- Compression & Hashing: Gzip/Brotli compression, multiple hash algorithms
- Backup & Merge: File versioning, backup rotation, text/JSON merging
- Validation: Syntax checking and linting for TypeScript, JavaScript, JSON
- Dynamic Access Control: Flexible directory permissions via CLI args or MCP Roots
Architecture
Unified Tools (10 Total)
All tools use a unified pattern with type, mode, or operation parameters to access multiple capabilities through a single interface, reducing token cost and improving LLM understanding.
Directory Access Control
The server uses a flexible directory access control system. Directories can be specified via command-line arguments or dynamically via Roots.
Method 1: Command-line Arguments
Specify allowed directories when starting the server:
mcp-server-filesystem /path/to/dir1 /path/to/dir2
Method 2: MCP Roots (Recommended)
MCP clients that support Roots can dynamically update allowed directories.
Important: If server starts without command-line arguments AND client doesn't support roots protocol, the server will throw an error during initialization.
How It Works
- Server Startup: Starts with directories from command-line arguments (if provided)
- Client Connection: Client connects and sends
initializerequest - Roots Protocol:
- Server requests roots from client via
roots/list - Client responds with configured roots
- Server replaces ALL allowed directories with client's roots
- Runtime updates via
notifications/roots/list_changed
- Server requests roots from client via
- Access Control: All operations restricted to allowed directories
API Reference
1. read - Unified Read Operations
Read files with multiple modes in a single tool.
Parameters:
type:'text'|'binary'|'media'|'multiple'(default:'text')path: string (for single file)paths: string[] (for multiple files)encoding:'utf8'|'utf16le'|'ascii'|'latin1'|'base64'|'hex'head: number (first N lines)tail: number (last N lines)lineRange: { start: number, end: number }stream: boolean (stream large files)includeMetadata: boolean
Examples:
// Text file
{ type: 'text', path: '/file.txt', head: 10 }
// Binary file
{ type: 'binary', path: '/image.png' }
// Multiple files
{ type: 'multiple', paths: ['/a.txt', '/b.txt'] }
// Media with base64
{ type: 'media', path: '/photo.jpg' }
2. write - Unified Write Operations
Write files with single, batch, or template modes.
Parameters:
mode:'single'|'batch'|'template'(default:'single')path: string (for single/template)content: string (for single)operations: Array<{ path, content, encoding }> (for batch)template: string (template content with {{variables}})variables: Record<string, string> (for template)append: booleanatomic: boolean (temp file + rename)backup: booleanencoding: string
Examples:
// Single write
{ mode: 'single', path: '/file.txt', content: 'Hello' }
// Batch write (3 files at once)
{ mode: 'batch', operations: [
{ path: '/a.txt', content: 'A' },
{ path: '/b.txt', content: 'B' },
{ path: '/c.txt', content: 'C' }
]}
// Template write
{ mode: 'template', path: '/config.json',
template: '{"name": "{{name}}", "version": "{{version}}"}',
variables: { name: 'MyApp', version: '1.0' }
}
3. file - Unified File Operations
Perform various file operations through a single tool.
Parameters:
operation:'edit'|'mkdir'|'move'|'copy'|'delete'path: string (target path)source: string (for move/copy)destination: string (for move/copy)edits: Array<{ oldText, newText, useRegex, flags }> (for edit)recursive: booleandryRun: boolean (preview edits)overwrite: boolean
Examples:
// Edit file
{ operation: 'edit', path: '/file.ts',
edits: [{ oldText: 'const', newText: 'let' }],
dryRun: true
}
// Create directory
{ operation: 'mkdir', path: '/new-dir', recursive: true }
// Copy file
{ operation: 'copy', source: '/a.txt', destination: '/b.txt' }
// Move file
{ operation: 'move', source: '/old.txt', destination: '/new.txt' }
// Delete recursively
{ operation: 'delete', path: '/dir', recursive: true }
4. list - Unified Directory Listing
List directory contents with multiple display modes.
Parameters:
mode:'simple'|'detailed'|'tree'|'recursive'(default:'simple')path: stringpattern: string (glob pattern)includeHidden: booleanincludeSize: booleanincludePermissions: booleansortBy:'name'|'size'|'mtime'|'atime'maxDepth: numberpage: numberpageSize: number
Examples:
// Simple list
{ mode: 'simple', path: '/dir', pattern: '*.ts' }
// Detailed with sizes
{ mode: 'detailed', path: '/dir', includeSize: true, sortBy: 'size' }
// Tree view
{ mode: 'tree', path: '/dir', maxDepth: 3 }
// Recursive with pagination
{ mode: 'recursive', path: '/dir', page: 1, pageSize: 100 }
5. search - Unified Search Operations
Search files, content, or use fuzzy matching.
Parameters:
type:'files'|'content'|'fuzzy'(default:'files')path: string (starting directory)pattern: string (for file search)query: string (for content/fuzzy search)caseSensitive: booleanuseRegex: booleanmaxDepth: numbermaxResults: numberfileTypes: string[]excludePatterns: string[]threshold: number (0-1, for fuzzy)contextLines: number (for content search)
Examples:
// File search
{ type: 'files', path: '/src', pattern: '*.ts', maxDepth: 5 }
// Content search
{ type: 'content', path: '/src', query: 'TODO', contextLines: 2 }
// Fuzzy search
{ type: 'fuzzy', path: '/src', query: 'usr', threshold: 0.7 }
6. info - Unified File Information
Get file/directory metadata, MIME types, disk usage, or symlink info.
Parameters:
type:'metadata'|'mime'|'disk-usage'|'symlink'(default:'metadata')path: stringincludeExtended: boolean (for metadata)recursive: boolean (for disk-usage)maxDepth: numbersortBy:'size'|'name'limit: number
Examples:
// Metadata
{ type: 'metadata', path: '/file.txt', includeExtended: true }
// MIME type
{ type: 'mime', path: '/image.png' }
// Disk usage
{ type: 'disk-usage', path: '/dir', recursive: true, limit: 20 }
// Symlink info
{ type: 'symlink', path: '/link' }
7. compare - Unified File Comparison
Compare files or directories with text, binary, or recursive modes.
Parameters:
type:'text'|'binary'|'directory'(default:'text')path1: stringpath2: stringignoreWhitespace: booleancontextLines: number (for text)recursive: boolean (for directory)compareContent: boolean (for directory)
Examples:
// Text diff
{ type: 'text', path1: '/old.txt', path2: '/new.txt', contextLines: 3 }
// Binary comparison
{ type: 'binary', path1: '/a.bin', path2: '/b.bin' }
// Directory comparison
{ type: 'directory', path1: '/dir1', path2: '/dir2', recursive: true }
8. utility - Unified Utility Operations
Backup, compress, hash, and merge operations in one tool.
Parameters:
operation:- Backup:
'backup-create'|'backup-restore'|'backup-list'|'backup-rotate' - Compression:
'compress'|'decompress' - Hashing:
'hash'|'hash-verify'|'hash-batch'|'hash-directory' - Merging:
'merge-text'|'merge-json'
- Backup:
path: stringpaths: string[] (for batch/merge)format:'gzip'|'brotli'(for compression)algorithm:'md5'|'sha1'|'sha256'|'sha512'(for hash)outputPath: stringversioned: boolean (for backup)keepLast: number (for backup rotation)separator: string (for merge-text)strategy:'shallow'|'deep'(for merge-json)
Examples:
// Create versioned backup
{ operation: 'backup-create', path: '/file.txt', versioned: true }
// Compress with brotli
{ operation: 'compress', path: '/large.txt', format: 'brotli' }
// Hash file
{ operation: 'hash', path: '/file.txt', algorithm: 'sha256' }
// Batch hash
{ operation: 'hash-batch', paths: ['/a.txt', '/b.txt'], algorithm: 'md5' }
// Merge text files
{ operation: 'merge-text', paths: ['/a.txt', '/b.txt'],
outputPath: '/merged.txt', separator: '\n---\n' }
// Merge JSON (deep)
{ operation: 'merge-json', paths: ['/a.json', '/b.json'],
outputPath: '/merged.json', strategy: 'deep' }
9. git - Unified Git Operations
Execute git commands through MCP.
Parameters:
command:'status'|'log'|'diff'|'branch'|'show'|'blame'path: string (repository path, default: '.')short: boolean (for status)staged: boolean (for diff)file: string (specific file)unified: number (context lines for diff)limit: number (for log)oneline: boolean (for log)graph: boolean (for log)author: string (for log)since: string (for log)remote: boolean (for branch)all: boolean (for branch)commit: string (for show, default: 'HEAD')stat: boolean (for show)lineStart: number (for blame)lineEnd: number (for blame)
Examples:
// Status (short)
{ command: 'status', path: '/repo', short: true }
// Log (last 10, oneline)
{ command: 'log', limit: 10, oneline: true, graph: true }
// Diff (staged)
{ command: 'diff', staged: true }
// Blame (specific lines)
{ command: 'blame', file: 'src/index.ts', lineStart: 10, lineEnd: 20 }
10. validate - Unified Validation Operations
Syntax checking and linting for code files.
Parameters:
type:'syntax'|'lint'(default:'syntax')path: stringlanguage:'typescript'|'javascript'|'json'|'auto'(default:'auto')strict: booleanfix: boolean (for lint)configPath: string (eslint config)
Examples:
// Syntax check (auto-detect)
{ type: 'syntax', path: '/file.ts' }
// Lint with fixes
{ type: 'lint', path: '/code.js', fix: true, configPath: '.eslintrc.json' }
// Strict JSON validation
{ type: 'syntax', path: '/data.json', language: 'json', strict: true }
Performance & Optimization
- 79% Fewer Tools: 48 → 10 unified tools reduces token cost for LLM calls
- Type Safety: 100% strict typing with Zod runtime validation
- Efficient Batching: Batch operations for multiple files in single call
- Streaming: Large file support with streaming capabilities
- Caching: Smart caching for repeated operations
Usage with Claude Desktop
Add to your claude_desktop_config.json:
Docker
{
"mcpServers": {
"filesystem": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--mount", "type=bind,src=/Users/username/Desktop,dst=/projects/Desktop",
"mcp/filesystem",
"/projects"
]
}
}
}
NPX
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/username/Desktop",
"/path/to/other/allowed/dir"
]
}
}
}
Usage with VS Code
For quick installation, click the installation buttons below:
For manual installation, add to .vscode/mcp.json in your workspace:
Docker
{
"servers": {
"filesystem": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--mount", "type=bind,src=${workspaceFolder},dst=/projects/workspace",
"mcp/filesystem",
"/projects"
]
}
}
}
NPX
{
"servers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"${workspaceFolder}"
]
}
}
}
Build
# Install dependencies
bun install
# Build
bun run build
# Docker build
docker build -t mcp/filesystem -f src/filesystem/Dockerfile .
Development
# Run tests
bun test
# Type check
bun run build
# Watch mode
bun run dev
Technical Details
- Language: TypeScript with 100% type safety
- Runtime: Node.js
- Validation: Zod schemas with runtime checking
- Protocol: Model Context Protocol (MCP)
- Architecture: Unified tool pattern for efficiency
License
MIT License - see LICENSE file for details.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。