ssh-mcp-rs 3.0.0

MCP server exposing SSH control for Linux systems via Model Context Protocol
Documentation
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File Backup Manager - Implementation Reference

> **Use Case**: Safe file removal with automatic backups for MCP tools  > **Features**: Local and remote backups, UUID-based sessions, atomic moves, XML serialization

---

## Overview

This implementation provides safe file removal operations by automatically creating backups before deletion. Supports both local filesystem and remote SSH/SFTP targets.

### Key Features

| Feature | Description |
|---------|-------------|
| **Atomic Backup** | Uses `rename()` for instant move without copy |
| **UUID Sessions** | Unique backup identifiers for batch operations |
| **Dual Support** | Both local and remote (SSH/SFTP) backups |
| **XML Serialization** | Structured backup metadata for audit trails |
| **Path Preservation** | Original path stored for potential rollback |

---

## Dependencies

```toml
[dependencies]
uuid = { version = "1.0", features = ["v4"] }
serde = { version = "1.0", features = ["derive"] }
```

---

## Core Implementation

### 1. Backup Manager Structure

```rust
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use uuid::Uuid;
use anyhow::{anyhow, Result};

/// Manages file backups for safe removal operations
/// 
/// # Example
/// ```
/// // Backup before removal
/// let backup_path = FileBackupManager::move_to_backup("/etc/nginx/nginx.conf")?;
/// println!("Backed up to: {}", backup_path);
/// 
/// // Later: restore if needed
/// std::fs::rename(&backup_path, "/etc/nginx/nginx.conf")?;
/// ```
pub struct FileBackupManager {
    /// Base directory for local backups
    local_backup_root: PathBuf,
    /// Session ID for grouping related backups
    session_id: String,
    /// Tracks original -> backup mappings
    backup_map: HashMap<String, String>,
}

impl FileBackupManager {
    /// Create new backup manager with unique session
    pub fn new(local_backup_root: PathBuf) -> Self {
        Self {
            local_backup_root,
            session_id: Uuid::new_v4().to_string(),
            backup_map: HashMap::new(),
        }
    }
    
    /// Create with explicit session ID (for restore operations)
    pub fn with_session(local_backup_root: PathBuf, session_id: String) -> Self {
        Self {
            local_backup_root,
            session_id,
            backup_map: HashMap::new(),
        }
    }
    
    /// Get backup directory path for current session
    pub fn session_backup_dir(&self) -> PathBuf {
        self.local_backup_root
            .join("backups")
            .join(&self.session_id)
    }
}
```

---

### 2. Local File Backup

```rust
impl FileBackupManager {
    /// Move local file or directory to backup location
    /// 
    /// # Arguments
    /// * `path` - Path to file or directory to backup
    ///
    /// # Returns
    /// Path to backup location
    ///
    /// # Errors
    /// * Path does not exist
    /// * Cannot create backup directory
    /// * Move operation fails
    pub fn backup_local(&mut self,
        path: &str,
    ) -> Result<String> {
        let path_obj = Path::new(path);
        
        // Verify path exists
        if !path_obj.exists() {
            return Err(anyhow!("Path does not exist: {}", path));
        }
        
        // Create backup directory
        let backup_dir = self.session_backup_dir();
        std::fs::create_dir_all(&backup_dir)
            .map_err(|e| anyhow!("Failed to create backup directory: {}", e))?;
        
        // Get item name (preserve original name)
        let item_name = path_obj
            .file_name()
            .and_then(|n| n.to_str())
            .ok_or_else(|| anyhow!("Invalid path name"))?;
        
        let backup_path = backup_dir.join(item_name);
        
        // Atomic move (instant, no copy for same filesystem)
        std::fs::rename(path_obj, &backup_path)
            .map_err(|e| anyhow!("Failed to move to backup: {}", e))?;
        
        let backup_path_str = backup_path.to_string_lossy().to_string();
        
        // Track mapping
        self.backup_map.insert(
            path.to_string(),
            backup_path_str.clone(),
        );
        
        Ok(backup_path_str)
    }
    
    /// Restore file from backup
    pub fn restore_local(&self,
        original_path: &str,
        backup_path: &str,
    ) -> Result<()> {
        let backup = Path::new(backup_path);
        
        if !backup.exists() {
            return Err(anyhow!("Backup not found: {}", backup_path));
        }
        
        // Ensure parent directory exists
        if let Some(parent) = Path::new(original_path).parent() {
            std::fs::create_dir_all(parent)?;
        }
        
        // Restore via atomic move
        std::fs::rename(backup, original_path)
            .map_err(|e| anyhow!("Failed to restore from backup: {}", e))?;
        
        Ok(())
    }
}
```

---

### 3. Remote File Backup (SSH/SFTP)

```rust
/// Remote connection abstraction (simplified)
pub trait RemoteConnection: Send + Sync {
    async fn rename(&self,
        from: &str,
        to: &str,
    ) -> Result<()>;
    
    async fn create_dir_all(&self,
        path: &str,
    ) -> Result<()>;
    
    async fn exists(&self,
        path: &str,
    ) -> bool;
}

impl FileBackupManager {
    /// Move remote file or directory to backup location
    /// 
    /// # Arguments
    /// * `conn` - Remote connection (SSH/SFTP)
    /// * `path` - Remote path to backup
    /// * `remote_backup_root` - Base backup directory on remote host
    pub async fn backup_remote(
        &mut self,
        conn: &Arc<dyn RemoteConnection>,
        path: &str,
        remote_backup_root: &str,
    ) -> Result<String> {
        // Verify path exists
        if !conn.exists(path).await {
            return Err(anyhow!("Remote path does not exist: {}", path));
        }
        
        // Create backup directory on remote
        let backup_dir = format!(
            "{}/backups/{}",
            remote_backup_root,
            self.session_id
        );
        
        conn.create_dir_all(&backup_dir).await
            .map_err(|e| anyhow!("Failed to create remote backup dir: {}", e))?;
        
        // Get item name
        let item_name = Path::new(path)
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown");
        
        let backup_path = format!("{}/{}", backup_dir, item_name);
        
        // Atomic move on remote
        conn.rename(path, &backup_path).await
            .map_err(|e| anyhow!("Failed to move remote to backup: {}", e))?;
        
        // Track mapping
        self.backup_map.insert(
            path.to_string(),
            backup_path.clone(),
        );
        
        Ok(backup_path)
    }
    
    /// Restore remote file from backup
    pub async fn restore_remote(
        &self,
        conn: &Arc<dyn RemoteConnection>,
        original_path: &str,
        backup_path: &str,
    ) -> Result<()> {
        if !conn.exists(backup_path).await {
            return Err(anyhow!("Remote backup not found: {}", backup_path));
        }
        
        // Ensure parent exists
        if let Some(parent) = Path::new(original_path).parent() {
            let parent_str = parent.to_string_lossy();
            if !parent_str.is_empty() && parent_str != "/" {
                let _ = conn.create_dir_all(&parent_str).await;
            }
        }
        
        // Restore
        conn.rename(backup_path, original_path).await
            .map_err(|e| anyhow!("Failed to restore remote backup: {}", e))?;
        
        Ok(())
    }
}
```

---

### 4. Backup Serialization (XML)

```rust
impl FileBackupManager {
    /// Serialize backup mappings to XML
    /// 
    /// Format:
    /// ```xml
    /// <file_backups>
    ///   <file
    ///       original_path="/etc/nginx/nginx.conf"
    ///       backup_path="/backups/session-uuid/nginx.conf"
    ///       location="remote"
    ///   />
    /// </file_backups>
    /// ```
    pub fn to_xml(&self,
        location: &str,  // "local" or "remote"
    ) -> String {
        let mut xml = String::from("\n<file_backups>");
        
        for (original, backup) in &self.backup_map {
            xml.push_str(&format!(
                r#"\n  <file
    original_path="{}"
    backup_path="{}"
    location="{}"
  />"#,
                Self::escape_xml(original),
                Self::escape_xml(backup),
                location
            ));
        }
        
        xml.push_str("\n</file_backups>");
        xml
    }
    
    /// Escape XML special characters
    fn escape_xml(text: &str) -> String {
        text
            .replace('&', "&amp;")
            .replace('<', "&lt;")
            .replace('>', "&gt;")
            .replace('"', "&quot;")
            .replace('\'', "&apos;")
    }
    
    /// Parse XML back to backup map (for restore)
    pub fn from_xml(xml: &str) -> Result<HashMap<String, String>> {
        // Simple XML parsing - in production use proper XML library
        let mut map = HashMap::new();
        
        // Extract file elements
        for line in xml.lines() {
            let line = line.trim();
            if line.starts_with("<file ") {
                let original = Self::extract_attr(line, "original_path");
                let backup = Self::extract_attr(line, "backup_path");
                if let (Some(orig), Some(back)) = (original, backup) {
                    map.insert(orig, back);
                }
            }
        }
        
        Ok(map)
    }
    
    fn extract_attr(line: &str, attr: &str) -> Option<String> {
        let pattern = format!(r#"{}=""#, attr);
        line.find(&pattern).and_then(|start| {
            let value_start = start + pattern.len();
            line[value_start..].find('"').map(|end| {
                line[value_start..value_start + end].to_string()
            })
        })
    }
}
```

---

## Batch Operations

### Transaction-style Backup

```rust
/// Transaction for batch file operations with rollback support
pub struct BackupTransaction {
    manager: FileBackupManager,
    committed: bool,
}

impl BackupTransaction {
    /// Start new transaction
    pub fn new(backup_root: PathBuf) -> Self {
        Self {
            manager: FileBackupManager::new(backup_root),
            committed: false,
        }
    }
    
    /// Backup multiple paths atomically
    pub fn backup_batch(&mut self,
        paths: &[&str],
    ) -> Result<Vec<String>> {
        let mut backup_paths = Vec::new();
        
        for path in paths {
            match self.manager.backup_local(path) {
                Ok(backup) => backup_paths.push(backup),
                Err(e) => {
                    // Rollback on failure
                    self.rollback()?;
                    return Err(anyhow!(
                        "Backup failed for '{}': {}. Rolled back {} backups.",
                        path, e, backup_paths.len()
                    ));
                }
            }
        }
        
        Ok(backup_paths)
    }
    
    /// Commit transaction (prevent auto-rollback)
    pub fn commit(mut self) {
        self.committed = true;
    }
    
    /// Rollback all backups in this transaction
    pub fn rollback(&self) -> Result<()> {
        for (original, backup) in &self.manager.backup_map {
            if let Err(e) = self.manager.restore_local(original, backup) {
                eprintln!("Warning: Failed to restore {}: {}", original, e);
            }
        }
        Ok(())
    }
}

impl Drop for BackupTransaction {
    fn drop(&mut self) {
        if !self.committed && !self.manager.backup_map.is_empty() {
            println!("Auto-rolling back {} backups...", self.manager.backup_map.len());
            let _ = self.rollback();
        }
    }
}
```

---

## MCP Tool Integration

### Remove Tool with Backup

```rust
use mcp_sdk::types::{Tool, CallToolResult, Content};
use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Debug, Deserialize, JsonSchema)]
pub struct RemoveParams {
    /// Path to remove
    pub path: String,
    /// Recursive removal for directories
    #[serde(default)]
    pub recursive: bool,
    /// Skip backup (dangerous!)
    #[serde(default)]
    pub no_backup: bool,
}

pub fn remove_tool() -> Tool {
    Tool {
        name: "remove".to_string(),
        description: Some(
            "Remove file or directory with automatic backup. \
             Backups can be restored using the restore tool.".to_string()
        ),
        input_schema: serde_json::json!({
            "type": "object",
            "properties": {
                "path": { "type": "string" },
                "recursive": { "type": "boolean", "default": false },
                "no_backup": { "type": "boolean", "default": false }
            },
            "required": ["path"]
        }),
    }
}

pub async fn handle_remove(
    params: RemoveParams,
    backup_root: PathBuf,
) -> CallToolResult {
    let path = Path::new(&params.path);
    
    // Verify path exists
    if !path.exists() {
        return CallToolResult::error(vec![
            Content::text(format!("Path does not exist: {}", params.path))
        ]);
    }
    
    // Check if directory
    let is_dir = path.is_dir();
    if is_dir && !params.recursive {
        return CallToolResult::error(vec![
            Content::text(
                "Path is a directory. Use recursive=true to remove.".to_string()
            )
        ]);
    }
    
    // Create backup unless skipped
    let backup_info = if !params.no_backup {
        let mut manager = FileBackupManager::new(backup_root);
        match manager.backup_local(&params.path) {
            Ok(backup_path) => {
                Some((backup_path, manager.to_xml("local")))
            }
            Err(e) => {
                return CallToolResult::error(vec![
                    Content::text(format!("Failed to create backup: {}", e))
                ]);
            }
        }
    } else {
        None
    };
    
    // Perform removal
    let result = if is_dir {
        std::fs::remove_dir_all(&params.path)
    } else {
        std::fs::remove_file(&params.path)
    };
    
    if let Err(e) = result {
        return CallToolResult::error(vec![
            Content::text(format!("Failed to remove: {}", e))
        ]);
    }
    
    // Build success response
    let mut response = format!("Successfully removed: {}", params.path);
    
    if let Some((backup_path, xml)) = backup_info {
        response.push_str(&format!(
            "\n\nBackup created at: {}\n\nBackup metadata:\n```xml\n{}\n```",
            backup_path,
            xml
        ));
    }
    
    CallToolResult::success(vec![Content::text(response)])
}
```

### Restore Tool

```rust
#[derive(Debug, Deserialize, JsonSchema)]
pub struct RestoreParams {
    /// Original path where file should be restored
    pub original_path: String,
    /// Backup path (from previous remove operation)
    pub backup_path: String,
}

pub fn restore_tool() -> Tool {
    Tool {
        name: "restore".to_string(),
        description: Some(
            "Restore file from backup created by remove tool".to_string()
        ),
        input_schema: serde_json::json!({
            "type": "object",
            "properties": {
                "original_path": { "type": "string" },
                "backup_path": { "type": "string" }
            },
            "required": ["original_path", "backup_path"]
        }),
    }
}

pub fn handle_restore(params: RestoreParams) -> CallToolResult {
    let manager = FileBackupManager::new(PathBuf::from("/tmp")); // root not used for restore
    
    match manager.restore_local(&params.original_path,
        &params.backup_path,
    ) {
        Ok(_) => CallToolResult::success(vec![
            Content::text(format!(
                "Restored {} from {}",
                params.original_path,
                params.backup_path
            ))
        ]),
        Err(e) => CallToolResult::error(vec![
            Content::text(format!("Failed to restore: {}", e))
        ]),
    }
}
```

---

## Testing

```rust
#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    
    #[test]
    fn test_local_backup_and_restore() {
        let temp_dir = TempDir::new().unwrap();
        let backup_root = temp_dir.path().join("backups");
        
        // Create test file
        let test_file = temp_dir.path().join("test.txt");
        std::fs::write(&test_file, "original content").unwrap();
        
        // Backup
        let mut manager = FileBackupManager::new(backup_root.clone());
        let backup_path = manager.backup_local(
            test_file.to_str().unwrap()
        ).unwrap();
        
        // Verify file moved to backup
        assert!(!test_file.exists());
        assert!(Path::new(&backup_path).exists());
        
        // Restore
        manager.restore_local(
            test_file.to_str().unwrap(),
            &backup_path,
        ).unwrap();
        
        // Verify restored
        assert!(test_file.exists());
        assert!(!Path::new(&backup_path).exists());
        
        let content = std::fs::read_to_string(&test_file).unwrap();
        assert_eq!(content, "original content");
    }
    
    #[test]
    fn test_xml_serialization() {
        let temp_dir = TempDir::new().unwrap();
        let mut manager = FileBackupManager::new(temp_dir.path().to_path_buf());
        
        // Create mock backup
        manager.backup_map.insert(
            "/etc/config.conf".to_string(),
            "/backups/uuid/config.conf".to_string(),
        );
        
        let xml = manager.to_xml("local");
        
        assert!(xml.contains("<file_backups>"));
        assert!(xml.contains("original_path="));
        assert!(xml.contains("/etc/config.conf"));
        assert!(xml.contains("location=\"local\""));
        
        // Parse back
        let map = FileBackupManager::from_xml(&xml).unwrap();
        assert_eq!(map.len(), 1);
        assert_eq!(
            map.get("/etc/config.conf"),
            Some(&"/backups/uuid/config.conf".to_string())
        );
    }
    
    #[test]
    fn test_transaction_rollback() {
        let temp_dir = TempDir::new().unwrap();
        let backup_root = temp_dir.path().join("backups");
        
        // Create files
        let file1 = temp_dir.path().join("file1.txt");
        let file2 = temp_dir.path().join("file2.txt");
        std::fs::write(&file1, "content1").unwrap();
        std::fs::write(&file2, "content2").unwrap();
        
        // Start transaction and backup first file
        {
            let mut tx = BackupTransaction::new(backup_root.clone());
            tx.backup_batch(&[file1.to_str().unwrap()]).unwrap();
            
            // Transaction dropped without commit - should auto-rollback
        }
        
        // Verify file1 restored
        assert!(file1.exists());
    }
    
    #[test]
    fn test_xml_escaping() {
        let input = "Special <chars> \"quoted\" & 'apostrophe'";
        let escaped = FileBackupManager::escape_xml(input);
        
        assert_eq!(
            escaped,
            "Special &lt;chars&gt; &quot;quoted&quot; &amp; &apos;apostrophe&apos;"
        );
    }
}
```

---

## Directory Structure

```
~/.mcp/backups/
└── {session-uuid}/
    ├── file1.conf          # backed up file
    ├── file2.conf
    └── directory/          # backed up directory (moved atomically)
        └── contents...
```

---

## Integration Checklist

1. **Dependencies**: Add `uuid` and create backup root directory
2. **Storage**: Decide on backup location (`~/.mcp/backups/` or `/var/backups/`)
3. **Retention**: Implement cleanup for old backups (cron/scheduled task)
4. **Permissions**: Ensure backup directory has proper permissions (700)
5. **Monitoring**: Track backup sizes to prevent disk fill

---

## External Libraries

- [uuid]https://github.com/uuid-rs/uuid - UUID generation

---

*Adapt this reference to your MCP server architecture.*