ssh-mcp-rs 2.1.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
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::Duration;

use tokio::process::Command;

use crate::error::{Result, SshMcpError};
use crate::ssh::{HostKeyCheckMode, SshConnectionManager, escape_for_shell};

use super::process;
use super::skeleton;
use super::types::{
    RsyncOptions, TransferCounts, TransferKind, TransferOperation, TransferStaging,
};

// Staging/marker helpers live in `super::staging`.

#[derive(Debug, Clone)]
pub struct RsyncEndpoint {
    pub host: String,
    pub port: u16,
    pub user: String,
    pub key_path: Option<PathBuf>,
    pub host_key_checking: HostKeyCheckMode,
    pub known_hosts: Option<PathBuf>,
}

#[derive(Debug, Clone)]
pub struct RsyncTransferArgs<'a> {
    pub conn: &'a SshConnectionManager,
    pub remote_home: &'a str,
    pub local_root: &'a Path,
    pub id: u64,
    pub timeout: Duration,
    pub operation: TransferOperation,
    pub kind: TransferKind,
    pub local_path: &'a Path,
    pub remote_path: &'a str,
    pub overwrite: bool,
    pub rsync_options: RsyncOptions,
}

pub async fn run_transfer(
    endpoint: RsyncEndpoint,
    args: RsyncTransferArgs<'_>,
) -> std::result::Result<(TransferStaging, TransferCounts), super::TransportAttemptError> {
    // Check local rsync availability first
    if let Err(e) = check_local_rsync().await {
        return Err(super::TransportAttemptError::Unsupported {
            transport: super::TransferTransport::Rsync,
            reason: format!("local rsync not available: {e}"),
        });
    }

    // Check remote rsync availability via SSH
    match check_remote_rsync(args.conn, args.timeout).await {
        Ok(true) => {}
        Ok(false) => {
            return Err(super::TransportAttemptError::Unsupported {
                transport: super::TransferTransport::Rsync,
                reason: "rsync not found on remote host".to_string(),
            });
        }
        Err(e) => {
            return Err(super::TransportAttemptError::Other(e));
        }
    }

    skeleton::dispatch_transfer(skeleton::DispatchTransferArgs {
        operation: args.operation,
        kind: args.kind,
        endpoint,
        args,
        put_file,
        get_file,
        put_dir,
        get_dir,
    })
    .await
}

async fn check_local_rsync() -> Result<()> {
    match Command::new("rsync").arg("--version").output().await {
        Ok(output) if output.status.success() => Ok(()),
        Ok(_) => Err(SshMcpError::connection("rsync --version failed")),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            Err(SshMcpError::connection("rsync binary not found"))
        }
        Err(e) => Err(SshMcpError::Io(e)),
    }
}

async fn check_remote_rsync(conn: &SshConnectionManager, timeout: Duration) -> Result<bool> {
    let cmd = r#"sh -c 'command -v rsync'"#;
    let out = conn.exec_command(cmd, timeout).await?;
    Ok(out.exit_code == Some(0) && !out.stdout.trim().is_empty())
}

#[cfg(unix)]
fn null_known_hosts_path() -> &'static str {
    "/dev/null"
}

#[cfg(windows)]
fn null_known_hosts_path() -> &'static str {
    "NUL"
}

fn build_ssh_options(endpoint: &RsyncEndpoint) -> String {
    let mut opts = vec![
        "-o".to_string(),
        "BatchMode=yes".to_string(),
        "-o".to_string(),
        format!(
            "StrictHostKeyChecking={}",
            endpoint.host_key_checking.as_openssh_value()
        ),
    ];

    match endpoint.host_key_checking {
        HostKeyCheckMode::No => {
            opts.push("-o".to_string());
            opts.push(format!("UserKnownHostsFile={}", null_known_hosts_path()));
        }
        HostKeyCheckMode::Yes | HostKeyCheckMode::AcceptNew => {
            if let Some(path) = &endpoint.known_hosts {
                let path_str = path.display().to_string();
                opts.push("-o".to_string());
                opts.push(format!(
                    "UserKnownHostsFile='{}'",
                    escape_for_shell(&path_str)
                ));
            }
        }
    }

    opts.push("-o".to_string());
    opts.push("LogLevel=ERROR".to_string());

    if endpoint.port != 22 {
        opts.push("-p".to_string());
        opts.push(endpoint.port.to_string());
    }

    if let Some(ref key) = endpoint.key_path {
        opts.push("-i".to_string());
        let key_str = key.display().to_string();
        let escaped = escape_for_shell(&key_str);
        // rsync -e passes a single command string; ensure key_path stays a single token.
        opts.push(format!("'{}'", escaped));
    }

    opts.join(" ")
}

fn rsync_remote_spec(endpoint: &RsyncEndpoint, remote_path: &str) -> String {
    format!("{}@{}:{}", endpoint.user, endpoint.host, remote_path)
}

async fn run_rsync(
    endpoint: &RsyncEndpoint,
    rsync_options: &RsyncOptions,
    src: &str,
    dst: &str,
    timeout_duration: Duration,
) -> std::result::Result<TransferCounts, super::TransportAttemptError> {
    let ssh_opts = build_ssh_options(endpoint);
    let mut cmd = Command::new("rsync");

    cmd.arg("--archive")
        .arg("--checksum")
        .arg("--inplace")
        .arg("--partial")
        .arg("--stats");

    if rsync_options.compress {
        cmd.arg("--compress");
    }

    if rsync_options.delete {
        cmd.arg("--delete");
    }

    cmd.arg("-e")
        .arg(format!("ssh {ssh_opts}"))
        .arg(src)
        .arg(dst);

    cmd.env("LC_ALL", "C");
    cmd.stdin(Stdio::null());
    cmd.stdout(Stdio::piped());
    cmd.stderr(Stdio::piped());

    let child = cmd.spawn().map_err(classify_spawn_error)?;
    let captured = process::wait_child_with_timeout(child, timeout_duration).await?;

    let stdout = String::from_utf8_lossy(&captured.stdout).to_string();
    let stderr = String::from_utf8_lossy(&captured.stderr).to_string();

    if !captured.status.success() {
        return Err(classify_rsync_failure(captured.status.code(), &stderr));
    }

    Ok(parse_rsync_stats(&stdout))
}

fn parse_rsync_stats(stdout: &str) -> TransferCounts {
    let mut files = 0u64;
    let mut bytes = 0u64;
    let mut found_transferred_files = false;

    for line in stdout.lines() {
        if let Some(rest) = line.strip_prefix("Number of regular files transferred: ") {
            // Prefer this count as it represents actual files (not including directories)
            if let Ok(n) = rest.parse::<u64>() {
                files = n;
                found_transferred_files = true;
            }
        } else if let Some(rest) = line.strip_prefix("Number of files: ") {
            // Format: "Number of files: 10 (reg: 8, dir: 2)"
            // Only use this as fallback if we haven't found "regular files transferred"
            if !found_transferred_files
                && let Some(num_str) = rest.split_whitespace().next()
                && let Ok(n) = num_str.parse::<u64>()
            {
                files = n;
            }
        } else if let Some(rest) = line.strip_prefix("Total transferred file size: ") {
            // Format: "Total transferred file size: 1,234,567 bytes"
            let cleaned: String = rest.chars().filter(|c| c.is_ascii_digit()).collect();
            if let Ok(n) = cleaned.parse::<u64>() {
                bytes = n;
            }
        }
    }

    TransferCounts {
        bytes,
        files,
        directories: 0,
    }
}

fn classify_spawn_error(err: std::io::Error) -> super::TransportAttemptError {
    process::classify_spawn_error_with_reason(
        err,
        super::TransferTransport::Rsync,
        "missing local rsync binary".to_string(),
    )
}

fn classify_rsync_failure(exit_code: Option<i32>, stderr: &str) -> super::TransportAttemptError {
    let stderr_lower = stderr.to_lowercase();

    // Check for rsync not found on remote
    if stderr_lower.contains("rsync: not found")
        || stderr_lower.contains("rsync: command not found")
        || stderr_lower.contains("could not find rsync")
    {
        return super::TransportAttemptError::Unsupported {
            transport: super::TransferTransport::Rsync,
            reason: "rsync not found on remote host".to_string(),
        };
    }

    // Check for SSH connection issues
    if stderr_lower.contains("connection refused")
        || stderr_lower.contains("connection timed out")
        || stderr_lower.contains("no route to host")
        || stderr_lower.contains("network is unreachable")
    {
        return super::TransportAttemptError::Other(SshMcpError::connection(format!(
            "rsync failed: network error; stderr={}",
            stderr.trim()
        )));
    }

    // Check for permission denied
    if stderr_lower.contains("permission denied") || stderr_lower.contains("access denied") {
        return super::TransportAttemptError::Other(SshMcpError::connection(format!(
            "rsync failed: permission denied; stderr={}",
            stderr.trim()
        )));
    }

    super::TransportAttemptError::Other(SshMcpError::connection(format!(
        "rsync failed: exit_code={exit_code:?}; stderr={}",
        stderr.trim()
    )))
}

// Remote staging helpers are implemented in `super::staging`.

async fn put_file(
    endpoint: RsyncEndpoint,
    args: RsyncTransferArgs<'_>,
) -> std::result::Result<(TransferStaging, TransferCounts), super::TransportAttemptError> {
    let RsyncTransferArgs {
        conn,
        remote_home,
        local_root: _,
        id,
        timeout,
        operation: _,
        kind: _,
        local_path,
        remote_path,
        overwrite,
        rsync_options,
    } = args;

    let local_path_str = local_path.display().to_string();
    let remote_path = remote_path.to_string();

    skeleton::put_file_with_remote_staging(
        skeleton::PutFileWithRemoteStagingArgs {
            conn,
            remote_home,
            remote_path,
            overwrite,
            id,
            timeout,
            local_path,
        },
        move |stage_path| async move {
            let remote = rsync_remote_spec(&endpoint, &stage_path);
            run_rsync(&endpoint, &rsync_options, &local_path_str, &remote, timeout)
                .await
                .map(|_| ())
        },
    )
    .await
}

async fn get_file(
    endpoint: RsyncEndpoint,
    args: RsyncTransferArgs<'_>,
) -> std::result::Result<(TransferStaging, TransferCounts), super::TransportAttemptError> {
    let RsyncTransferArgs {
        conn: _,
        remote_home: _,
        local_root,
        id,
        timeout,
        operation: _,
        kind: _,
        local_path,
        remote_path,
        overwrite,
        rsync_options,
    } = args;

    let remote = rsync_remote_spec(&endpoint, remote_path);

    skeleton::get_file_with_local_staging(
        skeleton::GetFileWithLocalStagingArgs {
            local_root,
            local_path,
            remote_path,
            overwrite,
            id,
        },
        move |tmp_path| async move {
            run_rsync(&endpoint, &rsync_options, &remote, &tmp_path, timeout)
                .await
                .map(|_| ())
        },
    )
    .await
}

async fn count_local_dir_no_symlinks(root: &Path) -> Result<TransferCounts> {
    super::walk::count_dir_no_symlinks(root).await
}

async fn put_dir(
    endpoint: RsyncEndpoint,
    args: RsyncTransferArgs<'_>,
) -> std::result::Result<(TransferStaging, TransferCounts), super::TransportAttemptError> {
    let RsyncTransferArgs {
        conn,
        remote_home,
        id,
        timeout,
        local_path,
        remote_path,
        overwrite,
        rsync_options,
        ..
    } = args;

    let counts = count_local_dir_no_symlinks(local_path)
        .await
        .map_err(super::TransportAttemptError::Other)?;

    let remote_path = remote_path.to_string();

    skeleton::put_dir_with_remote_staging(
        skeleton::PutDirWithRemoteStagingArgs {
            conn,
            remote_home,
            remote_path,
            overwrite,
            id,
            timeout,
            counts,
        },
        move |stage_path| async move {
            let local_dot = format!("{}/.", local_path.display());
            let remote = rsync_remote_spec(&endpoint, &stage_path);
            run_rsync(&endpoint, &rsync_options, &local_dot, &remote, timeout)
                .await
                .map(|_| ())
        },
    )
    .await
}

async fn get_dir(
    endpoint: RsyncEndpoint,
    args: RsyncTransferArgs<'_>,
) -> std::result::Result<(TransferStaging, TransferCounts), super::TransportAttemptError> {
    let RsyncTransferArgs {
        conn,
        remote_home: _,
        local_root,
        id,
        timeout,
        operation: _,
        kind: _,
        local_path,
        remote_path,
        overwrite,
        rsync_options,
    } = args;

    let remote_dot = format!("{}/.", remote_path);
    let remote = rsync_remote_spec(&endpoint, &remote_dot);

    skeleton::get_dir_with_local_staging(
        skeleton::GetDirWithLocalStagingArgs {
            conn,
            local_root,
            local_path,
            remote_path,
            overwrite,
            id,
            timeout,
        },
        move |extract_target| async move {
            run_rsync(&endpoint, &rsync_options, &remote, &extract_target, timeout)
                .await
                .map(|_| ())
        },
    )
    .await
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ssh::escape_for_shell;

    #[test]
    fn test_parse_rsync_stats() {
        let output = r#"Number of files: 10 (reg: 8, dir: 2)
Number of created files: 10 (reg: 8, dir: 2)
Number of deleted files: 0
Number of regular files transferred: 8
Total file size: 1,234,567 bytes
Total transferred file size: 1,234,567 bytes
Literal data: 1,234,567 bytes
Matched data: 0 bytes
File list size: 0
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 1,235,890
Total bytes received: 172"#;

        let counts = parse_rsync_stats(output);
        assert_eq!(counts.files, 8);
        assert_eq!(counts.bytes, 1234567);
    }

    #[test]
    fn test_rsync_remote_spec() {
        let endpoint = RsyncEndpoint {
            host: "example.com".to_string(),
            port: 22,
            user: "alice".to_string(),
            key_path: None,
            host_key_checking: HostKeyCheckMode::No,
            known_hosts: None,
        };
        let spec = rsync_remote_spec(&endpoint, "/path/to/file.txt");
        assert_eq!(spec, "alice@example.com:/path/to/file.txt");
    }

    #[test]
    fn test_build_ssh_options() {
        let key_path = if cfg!(windows) {
            PathBuf::from(r"C:\Users\Alice\My Keys\id_rsa")
        } else {
            PathBuf::from("/home/alice/my keys/id_rsa")
        };

        let endpoint = RsyncEndpoint {
            host: "example.com".to_string(),
            port: 2222,
            user: "alice".to_string(),
            key_path: Some(key_path.clone()),
            host_key_checking: HostKeyCheckMode::No,
            known_hosts: None,
        };
        let opts = build_ssh_options(&endpoint);
        assert!(opts.contains("-p 2222"));

        let key_str = key_path.display().to_string();
        assert!(opts.contains(&format!("-i '{}'", escape_for_shell(&key_str))));
        assert!(opts.contains("BatchMode=yes"));

        let null_hosts = if cfg!(windows) { "NUL" } else { "/dev/null" };
        assert!(opts.contains(&format!("UserKnownHostsFile={null_hosts}")));
    }

    #[test]
    fn test_build_ssh_options_accept_new_known_hosts() {
        let endpoint = RsyncEndpoint {
            host: "example.com".to_string(),
            port: 22,
            user: "alice".to_string(),
            key_path: None,
            host_key_checking: HostKeyCheckMode::AcceptNew,
            known_hosts: Some(PathBuf::from("/tmp/my known_hosts")),
        };
        let opts = build_ssh_options(&endpoint);
        assert!(opts.contains("StrictHostKeyChecking=accept-new"));
        assert!(opts.contains("UserKnownHostsFile='/tmp/my known_hosts'"));
    }
}