mcp-server-filesystem 0.1.2

A comprehensive Model Context Protocol (MCP) server for filesystem operations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# ๐Ÿ“ Filesystem MCP Server

[![Crates.io](https://img.shields.io/crates/v/mcp-server-filesystem.svg)](https://crates.io/crates/mcp-server-filesystem)
[![Documentation](https://docs.rs/mcp-server-filesystem/badge.svg)](https://docs.rs/mcp-server-filesystem)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A comprehensive **Model Context Protocol (MCP) server** that provides secure filesystem operations with **directory allowlisting**, robust error handling, and comprehensive file management capabilities.

## โœจ Features

- ๐Ÿ”’ **Security First** - Directory allowlisting prevents unauthorized access
- ๐Ÿ“– **Comprehensive File Reading** - Text files, media files, and batch operations
- โœ๏ธ **Advanced File Editing** - Line-based edits with git-style diff output
- ๐Ÿ“ **Directory Management** - Create, list, and navigate directory structures
- ๐Ÿ” **Powerful Search** - Pattern-based file search with exclusion filters
- ๐ŸŒณ **Tree Views** - Recursive directory tree visualization as JSON
- ๐Ÿ“Š **File Information** - Detailed metadata including size, permissions, and timestamps
- ๐Ÿšš **File Operations** - Move, rename, and organize files safely
- ๐Ÿ›ก๏ธ **Robust Error Handling** - Comprehensive error messages with context
- ๐Ÿงน **Input Validation** - Automatic path validation and sanitization
- ๐Ÿ“š **Rich Documentation** - Built-in help and operation guidance
- ๐ŸŽฏ **SOLID Architecture** - Clean, maintainable, and testable codebase

## ๐Ÿš€ Installation & Usage

### Install from Crates.io

```bash
cargo install mcp-server-filesystem
```

### Run the Server

```bash
# Start the MCP server with allowed directories (communicates via stdio)
mcp-server-filesystem /path/to/allowed/directory

# Allow multiple directories
mcp-server-filesystem /home/user/projects /tmp/workspace

# Enable debug logging
mcp-server-filesystem /path/to/dir --log-level debug
```

### Test with MCP Inspector

```bash
# Install and run the MCP Inspector to test the server
npx @modelcontextprotocol/inspector mcp-server-filesystem /path/to/test/dir
```

### Use with Claude Desktop

Add to your Claude Desktop MCP configuration:

```json
{
  "mcpServers": {
    "filesystem": {
      "command": "mcp-server-filesystem",
      "args": ["."]
    }
  }
}
```

## ๐Ÿ› ๏ธ Available Tools

### File Reading Operations

### `read_text_file`

Read the complete contents of a text file with optional head/tail functionality.

**Parameters:**

- `path` (string): Path to the file to read
- `head` (optional number): Read only the first N lines
- `tail` (optional number): Read only the last N lines

**Example Request:**

```json
{
  "path": "/home/user/projects/README.md",
  "head": 10
}
```

**Example Response:**

```json
{
  "content": "# My Project\n\nThis is a sample project...",
  "path": "/home/user/projects/README.md",
  "size": 1024,
  "encoding": "utf-8"
}
```

### `read_media_file`

Read image or audio files and return base64 encoded data with MIME type detection.

**Parameters:**

- `path` (string): Path to the media file

**Example Request:**

```json
{
  "path": "/home/user/images/photo.jpg"
}
```

**Example Response:**

```json
{
  "content": "/9j/4AAQSkZJRgABAQEAYABgAAD...",
  "mime_type": "image/jpeg",
  "path": "/home/user/images/photo.jpg",
  "size": 245760
}
```

### `read_multiple_files`

Read multiple files simultaneously for efficient batch operations.

**Parameters:**

- `paths` (array of strings): Array of file paths to read

**Example Request:**

```json
{
  "paths": [
    "/home/user/config.json",
    "/home/user/settings.yaml",
    "/home/user/data.txt"
  ]
}
```

### File Writing Operations

### `write_file`

Create a new file or completely overwrite an existing file with new content.

**Parameters:**

- `path` (string): Path where the file should be written
- `content` (string): Content to write to the file

**Example Request:**

```json
{
  "path": "/home/user/projects/new_file.txt",
  "content": "Hello, World!\nThis is a new file."
}
```

### `edit_file`

Make line-based edits to a text file with git-style diff output.

**Parameters:**

- `path` (string): Path to the file to edit
- `edits` (array): Array of edit operations
- `dry_run` (optional boolean): Preview changes without applying them

**Edit Operation Format:**

```json
{
  "old_text": "original line content",
  "new_text": "new line content"
}
```

**Example Request:**

```json
{
  "path": "/home/user/config.py",
  "edits": [
    {
      "old_text": "DEBUG = False",
      "new_text": "DEBUG = True"
    }
  ],
  "dry_run": false
}
```

### Directory Operations

### `create_directory`

Create a new directory or ensure a directory exists, including parent directories.

**Parameters:**

- `path` (string): Path of the directory to create

**Example Request:**

```json
{
  "path": "/home/user/projects/new_project/src"
}
```

### `list_directory`

Get a detailed listing of all files and directories in a specified path.

**Parameters:**

- `path` (string): Path to the directory to list

**Example Response:**

```json
{
  "entries": [
    {
      "name": "README.md",
      "type": "[FILE]",
      "size": 1024
    },
    {
      "name": "src",
      "type": "[DIRECTORY]",
      "size": 0
    }
  ],
  "path": "/home/user/projects",
  "total_entries": 2
}
```

### `list_directory_with_sizes`

Get a detailed listing with file sizes and sorting options.

**Parameters:**

- `path` (string): Path to the directory to list
- `sort_by` (string): Sort criteria ("name", "size", "modified")

### `directory_tree`

Get a recursive tree view of files and directories as JSON.

**Parameters:**

- `path` (string): Root path for the tree
- `exclude_patterns` (optional array): Glob patterns to exclude

**Example Request:**

```json
{
  "path": "/home/user/projects",
  "exclude_patterns": ["*.log", "node_modules/**", ".git/**"]
}
```

### File Management Operations

### `move_file`

Move or rename files and directories safely.

**Parameters:**

- `source` (string): Source path
- `destination` (string): Destination path

**Example Request:**

```json
{
  "source": "/home/user/old_name.txt",
  "destination": "/home/user/documents/new_name.txt"
}
```

### `search_files`

Search for files and directories matching a pattern with exclusion support.

**Parameters:**

- `path` (string): Directory to search in
- `pattern` (string): Glob pattern to match
- `exclude_patterns` (optional array): Patterns to exclude

**Example Request:**

```json
{
  "path": "/home/user/projects",
  "pattern": "*.rs",
  "exclude_patterns": ["target/**", "*.tmp"]
}
```

### `get_file_info`

Retrieve detailed metadata about a file or directory.

**Parameters:**

- `path` (string): Path to the file or directory

**Example Response:**

```json
{
  "name": "config.json",
  "type": "[FILE]",
  "size": 2048,
  "is_directory": false,
  "modified": 1642694400,
  "path": "/home/user/config.json",
  "permissions": {
    "readable": true,
    "writable": true,
    "executable": false
  }
}
```

### Utility Operations

### `list_allowed_directories`

Returns the list of directories that this server is allowed to access.

**Example Response:**

```
Allowed directories:
/home/user/projects
/home/user/documents
/tmp/workspace
```

## ๐Ÿ“š Available Resources

The server provides built-in resources for help and status information:

### `fs://status`

Current server status, configuration, and capabilities information.

**Example Content:**

```
Filesystem MCP Server Status

Server: Running
Allowed Directories: /home/user/projects, /home/user/documents
Total Allowed Paths: 2
Tools Available: 13
Resources Available: 3

Capabilities:
- Secure file reading (text and media files)
- File writing and editing with line-based operations
- Directory management and navigation
- File search with pattern matching and exclusions
- File metadata and information retrieval
- File operations (move, rename, copy)
- Directory tree visualization
- Security through directory allowlisting
```

### `fs://help`

Comprehensive help documentation with tool descriptions, examples, and usage patterns.

**Content includes:**

- Complete tool reference with parameters and examples
- Security model explanation
- Pattern syntax guide
- Example workflows for common tasks
- Allowed directories listing

### `fs://allowed-directories`

Detailed information about configured allowed directories and security model.

**Example Content:**

```
Allowed Directories Configuration

The Filesystem MCP Server is configured with the following allowed directories.
All file operations are restricted to these paths and their subdirectories.

ALLOWED PATHS:
  1. /home/user/projects
  2. /home/user/documents

SECURITY INFORMATION:
- All file paths are validated against these allowed directories
- Operations outside these paths will be rejected
- Symlinks pointing outside allowed directories trigger warnings
- Path traversal attempts (../) are blocked
```

## ๐Ÿ”ง Configuration

### Command Line Options

```bash
mcp-server-filesystem [OPTIONS]

Options:
  -a, <PATH>    Add an allowed directory (can be used multiple times)
  -l, --log-level <LEVEL>     Set logging level [default: info] [possible values: trace, debug, info, warn, error]
  -f, --log-format <FORMAT>   Set log format [default: pretty] [possible values: pretty, json, compact]
      --help                  Print help information
      --version               Print version information
```

### Security Model

The server implements a strict security model:

- **Directory Allowlisting**: Only specified directories can be accessed
- **Path Validation**: All paths are validated and normalized
- **Symlink Protection**: Symlinks are handled safely with warnings
- **Size Limits**: Configurable file size limits prevent abuse
- **Error Sanitization**: Error messages don't leak sensitive information

## ๐Ÿ“– Usage Examples

### With Claude Desktop

Once configured, you can ask Claude:

> "Read the contents of my project's README.md file"

> "Create a new directory structure for a Python project"

> "Search for all Python files in my project and show their contents"

> "Edit my configuration file to enable debug mode"

> "Show me a tree view of my project directory, excluding build artifacts"

> "Show me the server status and available resources"

### With MCP Inspector

```bash
# Test the server interactively
npx @modelcontextprotocol/inspector mcp-server-filesystem /path/to/test

# Try these operations:
# 1. Use read_text_file to examine files
# 2. Use directory_tree to explore structure
# 3. Use search_files to find specific files
# 4. Use edit_file to make changes with dry_run: true
# 5. Browse resources: fs://status, fs://help, fs://allowed-directories
```

### Command Line Testing

```bash
# Use MCP Inspector for interactive testing
npx @modelcontextprotocol/inspector mcp-server-filesystem /home/user/projects

# Test specific operations:
# - File reading: read_text_file, read_multiple_files
# - Directory operations: list_directory, directory_tree
# - File management: write_file, edit_file, move_file
# - Search: search_files with various patterns
# - Resources: Browse fs://status, fs://help, fs://allowed-directories
```

## ๐Ÿšจ Error Handling

The server provides detailed error messages for common issues:

- **Path Not Found**: Clear indication when files or directories don't exist
- **Permission Denied**: Helpful messages for access issues
- **Invalid Patterns**: Guidance for correct glob pattern syntax
- **Validation Errors**: Specific feedback on parameter validation failures
- **Security Violations**: Safe error messages for unauthorized access attempts

## ๐Ÿงช Testing

Run the comprehensive test suite:

```bash
# Run all tests
cargo test

# Run specific test categories
cargo test read_files
cargo test search_files
cargo test get_file_info

# Run with coverage
cargo tarpaulin --out html
```

## ๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

### Development Setup

1. Clone the repository
2. Install Rust (1.70+ required)
3. Run `cargo build`
4. Run `cargo test`

### Code Style

This project follows SOLID principles and Domain-Driven Design:

- **Clean Architecture**: Separation of concerns with clear layers
- **Dependency Injection**: Testable and maintainable code
- **Comprehensive Testing**: Unit and integration tests
- **Error Handling**: Robust error types and handling

Run `cargo fmt` and `cargo clippy` before submitting.

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ™ Acknowledgments

- Built with [rmcp]https://crates.io/crates/rmcp - Rust MCP implementation
- File operations powered by [tokio]https://crates.io/crates/tokio
- Pattern matching via [globset]https://crates.io/crates/globset
- MIME type detection using [mime_guess]https://crates.io/crates/mime_guess

## ๐Ÿ“ž Support

- ๐Ÿ“– [Documentation]https://docs.rs/mcp-server-filesystem
- ๐Ÿ› [Issue Tracker]https://github.com/sabry-awad97/rust-mcp-servers/issues
- ๐Ÿ’ฌ [Discussions]https://github.com/sabry-awad97/rust-mcp-servers/discussions

---

<div align="center">
  <strong>Made with โค๏ธ for secure filesystem operations in the MCP ecosystem</strong>
</div>