file-organizer-mcp 3.1.3 → 3.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/ARCHITECTURE.md +588 -582
  2. package/CHANGELOG.md +12 -0
  3. package/CONTRIBUTING.md +314 -312
  4. package/MIGRATION.md +365 -358
  5. package/README.md +43 -48
  6. package/bin/file-organizer-mcp.mjs +141 -0
  7. package/bin/file-organizer-setup.mjs +76 -0
  8. package/dist/config.d.ts.map +1 -1
  9. package/dist/config.js +40 -7
  10. package/dist/config.js.map +1 -1
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +189 -18
  13. package/dist/index.js.map +1 -1
  14. package/dist/services/auto-organize.service.d.ts +17 -3
  15. package/dist/services/auto-organize.service.d.ts.map +1 -1
  16. package/dist/services/auto-organize.service.js +57 -13
  17. package/dist/services/auto-organize.service.js.map +1 -1
  18. package/dist/services/metadata.service.d.ts.map +1 -1
  19. package/dist/services/metadata.service.js +0 -2
  20. package/dist/services/metadata.service.js.map +1 -1
  21. package/dist/services/renaming.service.js +1 -1
  22. package/dist/services/renaming.service.js.map +1 -1
  23. package/dist/tools/duplicate-management.js +17 -17
  24. package/dist/tools/file-analysis.js +2 -2
  25. package/dist/tools/file-categorization.js +2 -2
  26. package/dist/tools/file-duplicates.js +7 -7
  27. package/dist/tools/file-listing.js +6 -6
  28. package/dist/tools/file-management.js +2 -2
  29. package/dist/tools/file-organization.js +13 -13
  30. package/dist/tools/file-scanning.js +7 -7
  31. package/dist/tools/organization-preview.js +15 -15
  32. package/dist/tools/rollback.js +6 -6
  33. package/dist/tools/watch.tool.js +13 -13
  34. package/dist/tui/client-detector.d.ts +49 -0
  35. package/dist/tui/client-detector.d.ts.map +1 -0
  36. package/dist/tui/client-detector.js +515 -0
  37. package/dist/tui/client-detector.js.map +1 -0
  38. package/dist/tui/index.d.ts +2 -2
  39. package/dist/tui/index.js +14 -3
  40. package/dist/tui/index.js.map +1 -1
  41. package/dist/tui/setup-wizard.d.ts +14 -0
  42. package/dist/tui/setup-wizard.d.ts.map +1 -1
  43. package/dist/tui/setup-wizard.js +253 -129
  44. package/dist/tui/setup-wizard.js.map +1 -1
  45. package/dist/utils/diagnostics.d.ts +22 -0
  46. package/dist/utils/diagnostics.d.ts.map +1 -0
  47. package/dist/utils/diagnostics.js +551 -0
  48. package/dist/utils/diagnostics.js.map +1 -0
  49. package/dist/utils/error-handler.js +10 -10
  50. package/package.json +98 -95
  51. package/scripts/postinstall.cjs +55 -0
  52. package/scripts/prepare.cjs +65 -0
  53. package/scripts/postinstall.js +0 -38
package/ARCHITECTURE.md CHANGED
@@ -1,582 +1,588 @@
1
- # Architecture Documentation
2
-
3
- ## 🏗️ System Overview
4
-
5
- File Organizer MCP is a security-hardened Model Context Protocol (MCP) server that provides intelligent file organization capabilities to Large Language Models (LLMs). The architecture follows a layered service pattern with comprehensive security validation at every level.
6
-
7
- ### Core Principles
8
-
9
- 1. **Security First** - Multi-layer validation and sanitization
10
- 2. **Service Isolation** - Clear separation of concerns
11
- 3. **Type Safety** - Strict TypeScript with Zod validation
12
- 4. **Testability** - Dependency injection and modular design
13
- 5. **Performance** - Streaming operations and resource limits
14
-
15
- ## 📐 Architecture Layers
16
-
17
- ```
18
- ┌─────────────────────────────────────────────────────────────┐
19
- │ MCP Client (LLM) │
20
- └─────────────────────────┬───────────────────────────────────┘
21
- │ JSON-RPC 2.0
22
- ┌─────────────────────────▼───────────────────────────────────┐
23
- │ MCP Server Layer │
24
- │ (server.ts - Protocol Handler) │
25
- └─────────────────────────┬───────────────────────────────────┘
26
-
27
- ┌─────────────────────────▼───────────────────────────────────┐
28
- │ Tools Layer │
29
- │ ┌──────────┬──────────┬───────────┬─────────────┐ │
30
- │ │ Scan │ Organize │ Duplicate │ Categorize │ │
31
- │ │ Files │ Files │ Find │ Files │ │
32
- │ └──────────┴──────────┴───────────┴─────────────┘ │
33
- └─────────────────────────┬───────────────────────────────────┘
34
-
35
- ┌─────────────────────────▼───────────────────────────────────┐
36
- │ Services Layer │
37
- │ ┌────────────┬──────────────┬─────────────┬──────────┐ │
38
- │ │ Path │ Organizer │ Hash │ Scanner │ │
39
- │ │ Validator │ Service │ Calculator │ Service │ │
40
- │ └────────────┴──────────────┴─────────────┴──────────┘ │
41
- └─────────────────────────┬───────────────────────────────────┘
42
-
43
- ┌─────────────────────────▼───────────────────────────────────┐
44
- │ Utils Layer │
45
- │ ┌──────────┬──────────┬───────────┬──────────┐ │
46
- │ │ Logger │ Error │ File │ Format- │ │
47
- │ │ │ Handler │ Utils │ ters │ │
48
- │ └──────────┴──────────┴───────────┴──────────┘ │
49
- └─────────────────────────┬───────────────────────────────────┘
50
-
51
- ┌─────────────────────────▼───────────────────────────────────┐
52
- │ File System │
53
- └─────────────────────────────────────────────────────────────┘
54
- ```
55
-
56
- ## 🔐 Security Architecture
57
-
58
- ### 8-Layer Path Validation Pipeline
59
-
60
- Every file path goes through these validation layers:
61
-
62
- ```typescript
63
- Input Path
64
-
65
- 1. Type Validation (Zod Schema)
66
-
67
- 2. Null Byte & Basic Sanitization
68
-
69
- 3. Path Normalization & Windows Case Adjustment
70
-
71
- 4. Traversal Sequence Prevention (../)
72
-
73
- 5. Absolute Path Resolution
74
-
75
- 6. Security Check (Whitelist & Blacklist)
76
-
77
- 7. Symlink Resolution & Target Validation
78
-
79
- 8. Existence & Access Check
80
-
81
- Validated Path ✅
82
- ```
83
-
84
- **Implementation:** [`src/services/path-validator.service.ts`](file:///c:/Users/NewAdmin/Desktop/File-Organizer-MCP/src/services/path-validator.service.ts)
85
-
86
- ### Race Condition Mitigation (TOCTOU Protection)
87
-
88
- **Problem:** Time-of-Check-Time-of-Use vulnerabilities can occur when a file is checked, then used later, allowing an attacker to swap the file between operations.
89
-
90
- **Solution:** Multi-layered approach:
91
-
92
- ```typescript
93
- // Layer 1: File Descriptor Validation
94
- // Open file with O_NOFOLLOW to prevent symlink following
95
- const handle = await fs.open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
96
-
97
- // Layer 2: Atomic Copy Operations
98
- // Use COPYFILE_EXCL to ensure destination doesn't exist
99
- await fs.copyFile(source, dest, constants.COPYFILE_EXCL);
100
-
101
- // Layer 3: Safe Overwrites
102
- // Move existing file to backup before replacing
103
- const backupPath = path.join('.file-organizer-backups', `${Date.now()}_overwrite_${basename}`);
104
- await fs.rename(existingFile, backupPath);
105
- ```
106
-
107
- **Limitations:**
108
- - File deletion on Windows uses path-based locking (not file descriptor)
109
- - Symlinks are blocked for security but may limit legitimate use cases
110
-
111
- ### Resource Protection
112
-
113
- ```typescript
114
- // Security Constants
115
- const SECURITY_LIMITS = {
116
- MAX_FILE_SIZE: 100 * 1024 * 1024, // 100 MB
117
- MAX_FILES: 10000, // Per operation
118
- MAX_DEPTH: 10, // Directory recursion
119
- MAX_PATH_LENGTH: 4096 // Characters
120
- };
121
- ```
122
-
123
- ## 📦 Core Components
124
-
125
- ### 1. Server Layer (`server.ts`)
126
-
127
- **Responsibility:** MCP protocol handling and tool registration
128
-
129
- ```typescript
130
- // Key responsibilities:
131
- - Initialize MCP server
132
- - Register tools with schemas
133
- - Handle tool invocations
134
- - Manage error responses
135
- - Coordinate with services
136
- ```
137
-
138
- **Key Features:**
139
- - Automatic tool discovery from `tools/` directory
140
- - Structured response formatting (JSON/Markdown)
141
- - Centralized error handling
142
-
143
- ### 2. Tools Layer (`tools/`)
144
-
145
- **Responsibility:** MCP tool implementations (user-facing API)
146
-
147
- Each tool follows this pattern:
148
-
149
- ```typescript
150
- export const toolDefinition: ToolDefinition = {
151
- name: 'tool_name',
152
- description: 'What it does',
153
- inputSchema: ZodSchema // Validation
154
- };
155
-
156
- export async function handleTool(args: ToolArgs): Promise<ToolResponse> {
157
- // 1. Validate inputs (Zod)
158
- // 2. Validate security (PathValidator)
159
- // 3. Call service layer
160
- // 4. Format response
161
- // 5. Handle errors
162
- }
163
- ```
164
-
165
- **Available Tools:**
166
- 1. **List Files in Directory** (`file_organizer_list_files`)
167
- 2. **Scan Directory for Detailed Info** (`file_organizer_scan_directory`)
168
- 3. **Categorize Files by Type** (`file_organizer_categorize_files`)
169
- 4. **Find Largest Files** (`file_organizer_find_largest_files`)
170
- 5. **Find Duplicate Files** (`file_organizer_find_duplicate_files`)
171
- 6. **Preview File Organization Plan** (`file_organizer_preview_organization`)
172
- 7. **Get Available File Categories** (`file_organizer_get_categories`)
173
- 8. **Analyze Duplicate Files with Smart Recommendations** (`file_organizer_analyze_duplicates`)
174
- 9. **Organize Files** (`file_organizer_organize_files`)
175
- 10. **Set Custom Organization Rules** (`file_organizer_set_custom_rules`)
176
- 11. **Delete Duplicate Files** (`file_organizer_delete_duplicates`)
177
- 12. **Undo Last Organization Operation** (`file_organizer_undo_last_operation`)
178
-
179
- ### 3. Services Layer (`services/`)
180
-
181
- **Responsibility:** Core business logic
182
-
183
- #### PathValidatorService
184
- ```typescript
185
- class PathValidatorService {
186
- // Multi-layer path validation
187
- async validateStrictPath(inputPath: unknown, options?: ValidatePathOptions): Promise<string>
188
-
189
- // Symlink resolution
190
- async resolvePath(path: string): Promise<string>
191
-
192
- // Containment checking
193
- isPathWithinRoots(path: string, roots: string[]): boolean
194
- }
195
- ```
196
-
197
- #### OrganizerService
198
- ```typescript
199
- class OrganizerService {
200
- // Generate organization plan
201
- async generateOrganizationPlan(
202
- directory: string,
203
- files: FileWithSize[],
204
- strategy?: ConflictStrategy // 'rename' | 'skip' | 'overwrite' | 'overwrite_if_newer'
205
- ): Promise<OrganizationPlan>
206
-
207
- // Execute organization with safety guarantees
208
- async organize(
209
- directory: string,
210
- files: FileWithSize[],
211
- options?: OrganizeOptions
212
- ): Promise<OrganizeResult>
213
-
214
- // Safety mechanisms:
215
- // 1. File descriptor validation before move
216
- // 2. Atomic copy with COPYFILE_EXCL (race-safe)
217
- // 3. Automatic backup to .file-organizer-backups/ on overwrite
218
- // 4. Retry loop for race condition recovery
219
- }
220
- ```
221
-
222
- #### HashCalculatorService
223
- ```typescript
224
- class HashCalculatorService {
225
- // Streaming hash calculation
226
- async calculateHash(filePath: string): Promise<string>
227
-
228
- // Find duplicates (memory-safe)
229
- async findDuplicates(files: FileWithSize[]): Promise<DuplicateGroup[]>
230
- }
231
- ```
232
-
233
- #### FileScannerService
234
- ```typescript
235
- class FileScannerService {
236
- // Recursive directory scanning
237
- async getAllFiles(
238
- directory: string,
239
- options?: ScanOptions
240
- ): Promise<FileWithSize[]>
241
-
242
- // Size calculation
243
- async calculateDirectorySize(directory: string): Promise<number>
244
- }
245
- ```
246
-
247
- #### CategorizerService
248
- ```typescript
249
- class CategorizerService {
250
- // File categorization
251
- getCategory(fileName: string): CategoryName
252
-
253
- // Custom rules support
254
- setCustomRules(rules: CategoryRule[]): void
255
-
256
- // Category statistics
257
- categorizeFiles(files: FileWithSize[]): CategoryBreakdown
258
- }
259
- ```
260
-
261
- #### RollbackService
262
- ```typescript
263
- class RollbackService {
264
- // Create rollback manifest
265
- async createManifest(actions: OrganizeAction[]): Promise<string>
266
-
267
- // Execute rollback
268
- async rollback(manifestId: string): Promise<RollbackResult>
269
-
270
- // List available rollbacks
271
- async listRollbacks(): Promise<RollbackManifest[]>
272
- }
273
- ```
274
-
275
- ### 4. Utils Layer (`utils/`)
276
-
277
- **Responsibility:** Shared utility functions
278
-
279
- #### Logger
280
- ```typescript
281
- class Logger {
282
- // Structured JSON logging to stderr (MCP stdio protocol)
283
- debug(message: string, context?: Record<string, any>): void
284
- info(message: string, context?: Record<string, any>): void
285
- warn(message: string, context?: Record<string, any>): void
286
- error(message: string, error?: Error, context?: Record<string, any>): void
287
-
288
- // Features:
289
- // - ISO 8601 timestamps
290
- // - Configurable log levels (debug/info/warn/error)
291
- // - Error stack traces for error logs
292
- // - JSON format for machine parsing
293
- }
294
- ```
295
-
296
- - **error-handler.ts** - Centralized error handling
297
- - **file-utils.ts** - File system helpers
298
- - **formatters.ts** - Data formatting (bytes, dates, etc.)
299
-
300
- ### 5. Schemas Layer (`schemas/`)
301
-
302
- **Responsibility:** Input validation using Zod
303
-
304
- ```typescript
305
- // Example: Path validation
306
- export const PathSchema = z.string()
307
- .min(1, 'Path cannot be empty')
308
- .max(4096, 'Path exceeds maximum length')
309
- .refine(
310
- (path) => !path.includes('\0'),
311
- 'Path contains null bytes'
312
- );
313
- ```
314
-
315
- ## 🔄 Data Flow
316
-
317
- ### Example: Organize Files Operation
318
-
319
- ```
320
- 1. LLM Request
321
-
322
- 2. MCP Server (server.ts)
323
- - Parse JSON-RPC request
324
- - Route to organize tool
325
-
326
- 3. Tool Handler (tools/file-organization.ts)
327
- - Validate args with Zod
328
- - Call PathValidatorService
329
-
330
- 4. PathValidatorService
331
- - 7-layer validation
332
- - Return validated path
333
-
334
- 5. FileScannerService
335
- - Scan directory
336
- - Apply resource limits
337
-
338
- 6. CategorizerService
339
- - Categorize each file
340
-
341
- 7. OrganizerService
342
- - Generate organization plan
343
- - Check conflicts
344
- - Execute moves (if not dry run)
345
-
346
- 8. RollbackService
347
- - Create rollback manifest
348
-
349
- 9. Tool Handler
350
- - Format response (JSON/Markdown)
351
-
352
- 10. MCP Server
353
- - Send JSON-RPC response
354
-
355
- 11. LLM receives result
356
- ```
357
-
358
- ## 💾 File Organization
359
-
360
- ### Source Structure
361
-
362
- ```
363
- src/
364
- ├── server.ts # MCP server entry point
365
- ├── index.ts # Main entry point
366
- ├── types.ts # TypeScript type definitions
367
- ├── constants.ts # Application constants
368
- ├── config.ts # Configuration management
369
-
370
- ├── services/ # Core business logic
371
- ├── path-validator.service.ts
372
- ├── organizer.service.ts
373
- ├── hash-calculator.service.ts
374
- ├── file-scanner.service.ts
375
- ├── categorizer.service.ts
376
- └── rollback.service.ts
377
-
378
- ├── tools/ # MCP tool implementations
379
- │ ├── file-scanning.ts
380
- │ ├── file-organization.ts
381
- ├── file-duplicates.ts
382
- ├── file-categorization.ts
383
- │ └── ...
384
-
385
- ├── schemas/ # Zod validation schemas
386
- │ ├── security.schemas.ts
387
- │ ├── common.schemas.ts
388
- ├── scan.schemas.ts
389
- └── organize.schemas.ts
390
-
391
- └── utils/ # Shared utilities
392
- ├── logger.ts
393
- ├── error-handler.ts
394
- ├── file-utils.ts
395
- └── formatters.ts
396
- ```
397
-
398
- ## 🧪 Testing Architecture
399
-
400
- ### Test Structure
401
-
402
- ```
403
- tests/
404
- ├── unit/ # Unit tests for services
405
- │ ├── services/
406
- │ │ ├── path-validator.test.ts
407
- │ │ ├── organizer.test.ts
408
- │ │ └── ...
409
- │ └── utils/
410
-
411
- ├── integration/ # Integration tests for tools
412
- │ ├── tools/
413
- │ │ └── organize.test.ts
414
- │ └── edge-cases.test.ts
415
-
416
- └── performance/ # Performance benchmarks
417
- └── performance.test.ts
418
- ```
419
-
420
- ### Test Philosophy
421
-
422
- 1. **Unit Tests** - Test services in isolation
423
- 2. **Integration Tests** - Test complete workflows
424
- 3. **Security Tests** - Validate all security controls
425
- 4. **Performance Tests** - Ensure scalability
426
-
427
- ## 🚀 Performance Considerations
428
-
429
- ### Streaming Operations
430
-
431
- Large files are processed using streams to avoid memory exhaustion:
432
-
433
- ```typescript
434
- // Hash calculation uses streams
435
- const hash = crypto.createHash('sha256');
436
- const stream = createReadStream(filePath, { highWaterMark: 64 * 1024 });
437
- stream.on('data', (chunk) => hash.update(chunk));
438
- ```
439
-
440
- ### Resource Limits
441
-
442
- ```typescript
443
- // Enforced at multiple levels:
444
- - File size: Skip files > 100MB for hashing
445
- - File count: Max 10,000 files per operation
446
- - Recursion depth: Max 10 levels
447
- - Path length: Max 4,096 characters
448
- ```
449
-
450
- ### Caching Strategy
451
-
452
- - Category mappings cached in-memory
453
- - File stats cached during single operation
454
- - No persistent caching (stateless design)
455
-
456
- ## 🔧 Configuration
457
-
458
- ### Configuration System (`config.ts`)
459
-
460
- The server uses a platform-aware configuration system that combines hardcoded defaults with user customization.
461
-
462
- **Structure:**
463
- ```typescript
464
- export const CONFIG = {
465
- VERSION: '3.0.0',
466
-
467
- security: {
468
- enablePathValidation: true,
469
- allowCustomDirectories: true,
470
- logAccess: true,
471
- maxScanDepth: 10,
472
- maxFilesPerOperation: 10000
473
- },
474
-
475
- paths: {
476
- defaultAllowed: getDefaultAllowedDirs(), // Platform-aware safe directories
477
- customAllowed: loadCustomAllowedDirs(), // User-defined from config.json
478
- alwaysBlocked: getAlwaysBlockedPatterns() // System protection patterns
479
- }
480
- };
481
- ```
482
-
483
- **Default Allowed Directories:**
484
- - **Windows:** Desktop, Documents, Downloads, Pictures, Videos, Music, OneDrive, Projects
485
- - **macOS:** Desktop, Documents, Downloads, Movies, Music, Pictures, iCloud Drive, Projects
486
- - **Linux:** Desktop, Documents, Downloads, Music, Pictures, Videos, ~/dev, ~/workspace
487
-
488
- **User Configuration:**
489
- - **Windows:** `%APPDATA%\file-organizer-mcp\config.json`
490
- - **macOS:** `~/Library/Application Support/file-organizer-mcp/config.json`
491
- - **Linux:** `~/.config/file-organizer-mcp/config.json`
492
-
493
- **Config File Format:**
494
- ```json
495
- {
496
- "customAllowedDirectories": [
497
- "C:\\Users\\Name\\CustomFolder",
498
- "D:\\Projects"
499
- ],
500
- "settings": {
501
- "maxScanDepth": 10,
502
- "logAccess": true
503
- }
504
- }
505
- ```
506
-
507
- ## 🎯 Design Patterns
508
-
509
- ### Dependency Injection
510
-
511
- ```typescript
512
- // Services receive dependencies via constructor
513
- class OrganizerService {
514
- constructor(
515
- private categorizer: CategorizerService,
516
- private rollback?: RollbackService
517
- ) {}
518
- }
519
- ```
520
-
521
- ### Error Handling
522
-
523
- ```typescript
524
- // Centralized error handling
525
- try {
526
- const result = await operation();
527
- } catch (error) {
528
- return createErrorResponse(error);
529
- }
530
- ```
531
-
532
- ### Type Safety
533
-
534
- ```typescript
535
- // Strict types with Zod runtime validation
536
- const ArgsSchema = z.object({
537
- directory: PathSchema,
538
- dry_run: z.boolean().optional()
539
- });
540
-
541
- type Args = z.infer<typeof ArgsSchema>;
542
- ```
543
-
544
- ## 📊 Monitoring & Logging
545
-
546
- ### Structured Logging
547
-
548
- ```json
549
- {
550
- "timestamp": "2026-02-02T14:09:04.413Z",
551
- "level": "info",
552
- "message": "Created rollback manifest: uuid (6 actions)"
553
- }
554
- ```
555
-
556
- ### Log Levels
557
-
558
- - `error` - Critical failures
559
- - `warn` - Recoverable issues (e.g., skipped files)
560
- - `info` - Operation milestones
561
- - `debug` - Detailed diagnostics
562
-
563
- ## 🔮 Future Architecture Changes
564
-
565
- ### Planned Improvements
566
-
567
- 1. **Plugin System** - Allow custom categorization rules
568
- 2. **Database Layer** - Persistent state for large operations
569
- 3. **Queue System** - Background processing for large directories
570
- 4. **Metrics Collection** - Performance monitoring
571
- 5. **Multi-language Support** - i18n infrastructure
572
-
573
- ## 📚 References
574
-
575
- - [Model Context Protocol Spec](https://modelcontextprotocol.io)
576
- - [Zod Documentation](https://zod.dev)
577
- - [TypeScript Handbook](https://www.typescriptlang.org/docs)
578
-
579
- ---
580
-
581
- **Last Updated:** February 3, 2026
582
- **Version:** 3.0.0
1
+ # Architecture Documentation
2
+
3
+ ## 🏗️ System Overview
4
+
5
+ File Organizer MCP is a security-hardened Model Context Protocol (MCP) server that provides intelligent file organization capabilities to Large Language Models (LLMs). The architecture follows a layered service pattern with comprehensive security validation at every level.
6
+
7
+ ### Core Principles
8
+
9
+ 1. **Security First** - Multi-layer validation and sanitization
10
+ 2. **Service Isolation** - Clear separation of concerns
11
+ 3. **Type Safety** - Strict TypeScript with Zod validation
12
+ 4. **Testability** - Dependency injection and modular design
13
+ 5. **Performance** - Streaming operations and resource limits
14
+
15
+ ## 📐 Architecture Layers
16
+
17
+ ```
18
+ ┌─────────────────────────────────────────────────────────────┐
19
+ │ MCP Client (LLM) │
20
+ └─────────────────────────┬───────────────────────────────────┘
21
+ │ JSON-RPC 2.0
22
+ ┌─────────────────────────▼───────────────────────────────────┐
23
+ │ MCP Server Layer │
24
+ │ (server.ts - Protocol Handler) │
25
+ └─────────────────────────┬───────────────────────────────────┘
26
+
27
+ ┌─────────────────────────▼───────────────────────────────────┐
28
+ │ Tools Layer │
29
+ │ ┌──────────┬──────────┬───────────┬─────────────┐ │
30
+ │ │ Scan │ Organize │ Duplicate │ Categorize │ │
31
+ │ │ Files │ Files │ Find │ Files │ │
32
+ │ └──────────┴──────────┴───────────┴─────────────┘ │
33
+ └─────────────────────────┬───────────────────────────────────┘
34
+
35
+ ┌─────────────────────────▼───────────────────────────────────┐
36
+ │ Services Layer │
37
+ │ ┌────────────┬──────────────┬─────────────┬──────────┐ │
38
+ │ │ Path │ Organizer │ Hash │ Scanner │ │
39
+ │ │ Validator │ Service │ Calculator │ Service │ │
40
+ │ └────────────┴──────────────┴─────────────┴──────────┘ │
41
+ └─────────────────────────┬───────────────────────────────────┘
42
+
43
+ ┌─────────────────────────▼───────────────────────────────────┐
44
+ │ Utils Layer │
45
+ │ ┌──────────┬──────────┬───────────┬──────────┐ │
46
+ │ │ Logger │ Error │ File │ Format- │ │
47
+ │ │ │ Handler │ Utils │ ters │ │
48
+ │ └──────────┴──────────┴───────────┴──────────┘ │
49
+ └─────────────────────────┬───────────────────────────────────┘
50
+
51
+ ┌─────────────────────────▼───────────────────────────────────┐
52
+ │ File System │
53
+ └─────────────────────────────────────────────────────────────┘
54
+ ```
55
+
56
+ ## 🔐 Security Architecture
57
+
58
+ ### 8-Layer Path Validation Pipeline
59
+
60
+ Every file path goes through these validation layers:
61
+
62
+ ```typescript
63
+ Input Path
64
+
65
+ 1. Type Validation (Zod Schema)
66
+
67
+ 2. Null Byte & Basic Sanitization
68
+
69
+ 3. Path Normalization & Windows Case Adjustment
70
+
71
+ 4. Traversal Sequence Prevention (../)
72
+
73
+ 5. Absolute Path Resolution
74
+
75
+ 6. Security Check (Whitelist & Blacklist)
76
+
77
+ 7. Symlink Resolution & Target Validation
78
+
79
+ 8. Existence & Access Check
80
+
81
+ Validated Path ✅
82
+ ```
83
+
84
+ **Implementation:** [`src/services/path-validator.service.ts`](file:///c:/Users/NewAdmin/Desktop/File-Organizer-MCP/src/services/path-validator.service.ts)
85
+
86
+ ### Race Condition Mitigation (TOCTOU Protection)
87
+
88
+ **Problem:** Time-of-Check-Time-of-Use vulnerabilities can occur when a file is checked, then used later, allowing an attacker to swap the file between operations.
89
+
90
+ **Solution:** Multi-layered approach:
91
+
92
+ ```typescript
93
+ // Layer 1: File Descriptor Validation
94
+ // Open file with O_NOFOLLOW to prevent symlink following
95
+ const handle = await fs.open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
96
+
97
+ // Layer 2: Atomic Copy Operations
98
+ // Use COPYFILE_EXCL to ensure destination doesn't exist
99
+ await fs.copyFile(source, dest, constants.COPYFILE_EXCL);
100
+
101
+ // Layer 3: Safe Overwrites
102
+ // Move existing file to backup before replacing
103
+ const backupPath = path.join('.file-organizer-backups', `${Date.now()}_overwrite_${basename}`);
104
+ await fs.rename(existingFile, backupPath);
105
+ ```
106
+
107
+ **Limitations:**
108
+
109
+ - File deletion on Windows uses path-based locking (not file descriptor)
110
+ - Symlinks are blocked for security but may limit legitimate use cases
111
+
112
+ ### Resource Protection
113
+
114
+ ```typescript
115
+ // Security Constants
116
+ const SECURITY_LIMITS = {
117
+ MAX_FILE_SIZE: 100 * 1024 * 1024, // 100 MB
118
+ MAX_FILES: 10000, // Per operation
119
+ MAX_DEPTH: 10, // Directory recursion
120
+ MAX_PATH_LENGTH: 4096, // Characters
121
+ };
122
+ ```
123
+
124
+ ## 📦 Core Components
125
+
126
+ ### 1. Server Layer (`server.ts`)
127
+
128
+ **Responsibility:** MCP protocol handling and tool registration
129
+
130
+ ```typescript
131
+ // Key responsibilities:
132
+ - Initialize MCP server
133
+ - Register tools with schemas
134
+ - Handle tool invocations
135
+ - Manage error responses
136
+ - Coordinate with services
137
+ ```
138
+
139
+ **Key Features:**
140
+
141
+ - Automatic tool discovery from `tools/` directory
142
+ - Structured response formatting (JSON/Markdown)
143
+ - Centralized error handling
144
+
145
+ ### 2. Tools Layer (`tools/`)
146
+
147
+ **Responsibility:** MCP tool implementations (user-facing API)
148
+
149
+ Each tool follows this pattern:
150
+
151
+ ```typescript
152
+ export const toolDefinition: ToolDefinition = {
153
+ name: 'tool_name',
154
+ description: 'What it does',
155
+ inputSchema: ZodSchema, // Validation
156
+ };
157
+
158
+ export async function handleTool(args: ToolArgs): Promise<ToolResponse> {
159
+ // 1. Validate inputs (Zod)
160
+ // 2. Validate security (PathValidator)
161
+ // 3. Call service layer
162
+ // 4. Format response
163
+ // 5. Handle errors
164
+ }
165
+ ```
166
+
167
+ **Available Tools:**
168
+
169
+ 1. **List Files in Directory** (`file_organizer_list_files`)
170
+ 2. **Scan Directory for Detailed Info** (`file_organizer_scan_directory`)
171
+ 3. **Categorize Files by Type** (`file_organizer_categorize_files`)
172
+ 4. **Find Largest Files** (`file_organizer_find_largest_files`)
173
+ 5. **Find Duplicate Files** (`file_organizer_find_duplicate_files`)
174
+ 6. **Preview File Organization Plan** (`file_organizer_preview_organization`)
175
+ 7. **Get Available File Categories** (`file_organizer_get_categories`)
176
+ 8. **Analyze Duplicate Files with Smart Recommendations** (`file_organizer_analyze_duplicates`)
177
+ 9. **Organize Files** (`file_organizer_organize_files`)
178
+ 10. **Set Custom Organization Rules** (`file_organizer_set_custom_rules`)
179
+ 11. **Delete Duplicate Files** (`file_organizer_delete_duplicates`)
180
+ 12. **Undo Last Organization Operation** (`file_organizer_undo_last_operation`)
181
+
182
+ ### 3. Services Layer (`services/`)
183
+
184
+ **Responsibility:** Core business logic
185
+
186
+ #### PathValidatorService
187
+
188
+ ```typescript
189
+ class PathValidatorService {
190
+ // Multi-layer path validation
191
+ async validateStrictPath(inputPath: unknown, options?: ValidatePathOptions): Promise<string>;
192
+
193
+ // Symlink resolution
194
+ async resolvePath(path: string): Promise<string>;
195
+
196
+ // Containment checking
197
+ isPathWithinRoots(path: string, roots: string[]): boolean;
198
+ }
199
+ ```
200
+
201
+ #### OrganizerService
202
+
203
+ ```typescript
204
+ class OrganizerService {
205
+ // Generate organization plan
206
+ async generateOrganizationPlan(
207
+ directory: string,
208
+ files: FileWithSize[],
209
+ strategy?: ConflictStrategy // 'rename' | 'skip' | 'overwrite' | 'overwrite_if_newer'
210
+ ): Promise<OrganizationPlan>;
211
+
212
+ // Execute organization with safety guarantees
213
+ async organize(
214
+ directory: string,
215
+ files: FileWithSize[],
216
+ options?: OrganizeOptions
217
+ ): Promise<OrganizeResult>;
218
+
219
+ // Safety mechanisms:
220
+ // 1. File descriptor validation before move
221
+ // 2. Atomic copy with COPYFILE_EXCL (race-safe)
222
+ // 3. Automatic backup to .file-organizer-backups/ on overwrite
223
+ // 4. Retry loop for race condition recovery
224
+ }
225
+ ```
226
+
227
+ #### HashCalculatorService
228
+
229
+ ```typescript
230
+ class HashCalculatorService {
231
+ // Streaming hash calculation
232
+ async calculateHash(filePath: string): Promise<string>;
233
+
234
+ // Find duplicates (memory-safe)
235
+ async findDuplicates(files: FileWithSize[]): Promise<DuplicateGroup[]>;
236
+ }
237
+ ```
238
+
239
+ #### FileScannerService
240
+
241
+ ```typescript
242
+ class FileScannerService {
243
+ // Recursive directory scanning
244
+ async getAllFiles(directory: string, options?: ScanOptions): Promise<FileWithSize[]>;
245
+
246
+ // Size calculation
247
+ async calculateDirectorySize(directory: string): Promise<number>;
248
+ }
249
+ ```
250
+
251
+ #### CategorizerService
252
+
253
+ ```typescript
254
+ class CategorizerService {
255
+ // File categorization
256
+ getCategory(fileName: string): CategoryName;
257
+
258
+ // Custom rules support
259
+ setCustomRules(rules: CategoryRule[]): void;
260
+
261
+ // Category statistics
262
+ categorizeFiles(files: FileWithSize[]): CategoryBreakdown;
263
+ }
264
+ ```
265
+
266
+ #### RollbackService
267
+
268
+ ```typescript
269
+ class RollbackService {
270
+ // Create rollback manifest
271
+ async createManifest(actions: OrganizeAction[]): Promise<string>;
272
+
273
+ // Execute rollback
274
+ async rollback(manifestId: string): Promise<RollbackResult>;
275
+
276
+ // List available rollbacks
277
+ async listRollbacks(): Promise<RollbackManifest[]>;
278
+ }
279
+ ```
280
+
281
+ ### 4. Utils Layer (`utils/`)
282
+
283
+ **Responsibility:** Shared utility functions
284
+
285
+ #### Logger
286
+
287
+ ```typescript
288
+ class Logger {
289
+ // Structured JSON logging to stderr (MCP stdio protocol)
290
+ debug(message: string, context?: Record<string, any>): void;
291
+ info(message: string, context?: Record<string, any>): void;
292
+ warn(message: string, context?: Record<string, any>): void;
293
+ error(message: string, error?: Error, context?: Record<string, any>): void;
294
+
295
+ // Features:
296
+ // - ISO 8601 timestamps
297
+ // - Configurable log levels (debug/info/warn/error)
298
+ // - Error stack traces for error logs
299
+ // - JSON format for machine parsing
300
+ }
301
+ ```
302
+
303
+ - **error-handler.ts** - Centralized error handling
304
+ - **file-utils.ts** - File system helpers
305
+ - **formatters.ts** - Data formatting (bytes, dates, etc.)
306
+
307
+ ### 5. Schemas Layer (`schemas/`)
308
+
309
+ **Responsibility:** Input validation using Zod
310
+
311
+ ```typescript
312
+ // Example: Path validation
313
+ export const PathSchema = z
314
+ .string()
315
+ .min(1, 'Path cannot be empty')
316
+ .max(4096, 'Path exceeds maximum length')
317
+ .refine((path) => !path.includes('\0'), 'Path contains null bytes');
318
+ ```
319
+
320
+ ## 🔄 Data Flow
321
+
322
+ ### Example: Organize Files Operation
323
+
324
+ ```
325
+ 1. LLM Request
326
+
327
+ 2. MCP Server (server.ts)
328
+ - Parse JSON-RPC request
329
+ - Route to organize tool
330
+
331
+ 3. Tool Handler (tools/file-organization.ts)
332
+ - Validate args with Zod
333
+ - Call PathValidatorService
334
+
335
+ 4. PathValidatorService
336
+ - 7-layer validation
337
+ - Return validated path
338
+
339
+ 5. FileScannerService
340
+ - Scan directory
341
+ - Apply resource limits
342
+
343
+ 6. CategorizerService
344
+ - Categorize each file
345
+
346
+ 7. OrganizerService
347
+ - Generate organization plan
348
+ - Check conflicts
349
+ - Execute moves (if not dry run)
350
+
351
+ 8. RollbackService
352
+ - Create rollback manifest
353
+
354
+ 9. Tool Handler
355
+ - Format response (JSON/Markdown)
356
+
357
+ 10. MCP Server
358
+ - Send JSON-RPC response
359
+
360
+ 11. LLM receives result
361
+ ```
362
+
363
+ ## 💾 File Organization
364
+
365
+ ### Source Structure
366
+
367
+ ```
368
+ src/
369
+ ├── server.ts # MCP server entry point
370
+ ├── index.ts # Main entry point
371
+ ├── types.ts # TypeScript type definitions
372
+ ├── constants.ts # Application constants
373
+ ├── config.ts # Configuration management
374
+
375
+ ├── services/ # Core business logic
376
+ ├── path-validator.service.ts
377
+ ├── organizer.service.ts
378
+ ├── hash-calculator.service.ts
379
+ │ ├── file-scanner.service.ts
380
+ │ ├── categorizer.service.ts
381
+ └── rollback.service.ts
382
+
383
+ ├── tools/ # MCP tool implementations
384
+ ├── file-scanning.ts
385
+ ├── file-organization.ts
386
+ │ ├── file-duplicates.ts
387
+ │ ├── file-categorization.ts
388
+ └── ...
389
+
390
+ ├── schemas/ # Zod validation schemas
391
+ │ ├── security.schemas.ts
392
+ ├── common.schemas.ts
393
+ ├── scan.schemas.ts
394
+ │ └── organize.schemas.ts
395
+
396
+ └── utils/ # Shared utilities
397
+ ├── logger.ts
398
+ ├── error-handler.ts
399
+ ├── file-utils.ts
400
+ └── formatters.ts
401
+ ```
402
+
403
+ ## 🧪 Testing Architecture
404
+
405
+ ### Test Structure
406
+
407
+ ```
408
+ tests/
409
+ ├── unit/ # Unit tests for services
410
+ ├── services/
411
+ │ │ ├── path-validator.test.ts
412
+ ├── organizer.test.ts
413
+ │ │ └── ...
414
+ │ └── utils/
415
+
416
+ ├── integration/ # Integration tests for tools
417
+ │ ├── tools/
418
+ │ │ └── organize.test.ts
419
+ │ └── edge-cases.test.ts
420
+
421
+ └── performance/ # Performance benchmarks
422
+ └── performance.test.ts
423
+ ```
424
+
425
+ ### Test Philosophy
426
+
427
+ 1. **Unit Tests** - Test services in isolation
428
+ 2. **Integration Tests** - Test complete workflows
429
+ 3. **Security Tests** - Validate all security controls
430
+ 4. **Performance Tests** - Ensure scalability
431
+
432
+ ## 🚀 Performance Considerations
433
+
434
+ ### Streaming Operations
435
+
436
+ Large files are processed using streams to avoid memory exhaustion:
437
+
438
+ ```typescript
439
+ // Hash calculation uses streams
440
+ const hash = crypto.createHash('sha256');
441
+ const stream = createReadStream(filePath, { highWaterMark: 64 * 1024 });
442
+ stream.on('data', (chunk) => hash.update(chunk));
443
+ ```
444
+
445
+ ### Resource Limits
446
+
447
+ ```typescript
448
+ // Enforced at multiple levels:
449
+ - File size: Skip files > 100MB for hashing
450
+ - File count: Max 10,000 files per operation
451
+ - Recursion depth: Max 10 levels
452
+ - Path length: Max 4,096 characters
453
+ ```
454
+
455
+ ### Caching Strategy
456
+
457
+ - Category mappings cached in-memory
458
+ - File stats cached during single operation
459
+ - No persistent caching (stateless design)
460
+
461
+ ## 🔧 Configuration
462
+
463
+ ### Configuration System (`config.ts`)
464
+
465
+ The server uses a platform-aware configuration system that combines hardcoded defaults with user customization.
466
+
467
+ **Structure:**
468
+
469
+ ```typescript
470
+ export const CONFIG = {
471
+ VERSION: '3.0.0',
472
+
473
+ security: {
474
+ enablePathValidation: true,
475
+ allowCustomDirectories: true,
476
+ logAccess: true,
477
+ maxScanDepth: 10,
478
+ maxFilesPerOperation: 10000,
479
+ },
480
+
481
+ paths: {
482
+ defaultAllowed: getDefaultAllowedDirs(), // Platform-aware safe directories
483
+ customAllowed: loadCustomAllowedDirs(), // User-defined from config.json
484
+ alwaysBlocked: getAlwaysBlockedPatterns(), // System protection patterns
485
+ },
486
+ };
487
+ ```
488
+
489
+ **Default Allowed Directories:**
490
+
491
+ - **Windows:** Desktop, Documents, Downloads, Pictures, Videos, Music, OneDrive, Projects
492
+ - **macOS:** Desktop, Documents, Downloads, Movies, Music, Pictures, iCloud Drive, Projects
493
+ - **Linux:** Desktop, Documents, Downloads, Music, Pictures, Videos, ~/dev, ~/workspace
494
+
495
+ **User Configuration:**
496
+
497
+ - **Windows:** `%APPDATA%\file-organizer-mcp\config.json`
498
+ - **macOS:** `~/Library/Application Support/file-organizer-mcp/config.json`
499
+ - **Linux:** `~/.config/file-organizer-mcp/config.json`
500
+
501
+ **Config File Format:**
502
+
503
+ ```json
504
+ {
505
+ "customAllowedDirectories": ["C:\\Users\\Name\\CustomFolder", "D:\\Projects"],
506
+ "settings": {
507
+ "maxScanDepth": 10,
508
+ "logAccess": true
509
+ }
510
+ }
511
+ ```
512
+
513
+ ## 🎯 Design Patterns
514
+
515
+ ### Dependency Injection
516
+
517
+ ```typescript
518
+ // Services receive dependencies via constructor
519
+ class OrganizerService {
520
+ constructor(
521
+ private categorizer: CategorizerService,
522
+ private rollback?: RollbackService
523
+ ) {}
524
+ }
525
+ ```
526
+
527
+ ### Error Handling
528
+
529
+ ```typescript
530
+ // Centralized error handling
531
+ try {
532
+ const result = await operation();
533
+ } catch (error) {
534
+ return createErrorResponse(error);
535
+ }
536
+ ```
537
+
538
+ ### Type Safety
539
+
540
+ ```typescript
541
+ // Strict types with Zod runtime validation
542
+ const ArgsSchema = z.object({
543
+ directory: PathSchema,
544
+ dry_run: z.boolean().optional(),
545
+ });
546
+
547
+ type Args = z.infer<typeof ArgsSchema>;
548
+ ```
549
+
550
+ ## 📊 Monitoring & Logging
551
+
552
+ ### Structured Logging
553
+
554
+ ```json
555
+ {
556
+ "timestamp": "2026-02-02T14:09:04.413Z",
557
+ "level": "info",
558
+ "message": "Created rollback manifest: uuid (6 actions)"
559
+ }
560
+ ```
561
+
562
+ ### Log Levels
563
+
564
+ - `error` - Critical failures
565
+ - `warn` - Recoverable issues (e.g., skipped files)
566
+ - `info` - Operation milestones
567
+ - `debug` - Detailed diagnostics
568
+
569
+ ## 🔮 Future Architecture Changes
570
+
571
+ ### Planned Improvements
572
+
573
+ 1. **Plugin System** - Allow custom categorization rules
574
+ 2. **Database Layer** - Persistent state for large operations
575
+ 3. **Queue System** - Background processing for large directories
576
+ 4. **Metrics Collection** - Performance monitoring
577
+ 5. **Multi-language Support** - i18n infrastructure
578
+
579
+ ## 📚 References
580
+
581
+ - [Model Context Protocol Spec](https://modelcontextprotocol.io)
582
+ - [Zod Documentation](https://zod.dev)
583
+ - [TypeScript Handbook](https://www.typescriptlang.org/docs)
584
+
585
+ ---
586
+
587
+ **Last Updated:** February 3, 2026
588
+ **Version:** 3.0.0