ssh-mcp-rs 4.1.1

SeSSHion: lightweight SSH MCP server for LLM agents
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
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use rmcp::ErrorData as McpError;
use rmcp::model::{CallToolResult, ContentBlock};
use tokio::io::{AsyncReadExt, AsyncSeekExt};
use tracing::{debug, error, warn};

use crate::background::OutputStreamer;
use crate::background::marker::read_background_markers_from_channel;
use crate::background::response::{
    BackgroundTimeoutSnapshot, background_json_err, background_json_ok, background_json_timeout,
};
use crate::background::wrapper::{build_background_wrapper_script, remote_job_log_path};
use crate::ssh::command::read_local_log_tail;
use crate::ssh::sanitize::wrap_in_posix_shell;
use crate::ssh::{CommandOutput, sanitize_command, wrap_sudo_command};
use crate::tools::DEFAULT_CHECK_PROCESS_TAIL_LINES;

use super::SshMcpServer;
use super::validation::validate_background_log_path;

pub(super) enum BackgroundPrivilege<'a> {
    Normal,
    Sudo { password: Option<&'a str> },
}

struct TimeoutRecoverySnapshot {
    state: String,
    still_running: bool,
    exit_code: Option<u32>,
    state_reason: Option<String>,
    elapsed_time: String,
    log_exists: bool,
    log_tail: String,
    tail_lines_used: usize,
}

impl SshMcpServer {
    pub(super) async fn execute_detachable_foreground_impl(
        &self,
        command_for_exec: &str,
        command_for_registry: &str,
        timeout: Duration,
    ) -> std::result::Result<CallToolResult, McpError> {
        let job_id = super::make_job_id();

        let (final_log_path_buf, final_log_path) = match self.default_local_log_path(&job_id) {
            Ok(v) => v,
            Err(e) => {
                return Ok(CallToolResult::error(vec![ContentBlock::text(format!(
                    "Error: {e}"
                ))]));
            }
        };
        if let Err(e) = self.ensure_local_log_file(&final_log_path_buf).await {
            return Ok(CallToolResult::error(vec![ContentBlock::text(format!(
                "Error: {e}"
            ))]));
        }

        let remote_log_path = remote_job_log_path(&job_id);
        let wrapper = build_background_wrapper_script(&job_id, command_for_exec, &remote_log_path);

        let permit = match self.connection.acquire_command_slot_raw().await {
            Ok(p) => p,
            Err(e) => {
                return Ok(CallToolResult::error(vec![ContentBlock::text(format!(
                    "Error: Failed to acquire command slot: {e}"
                ))]));
            }
        };

        let wrapped_wrapper = wrap_in_posix_shell(&wrapper, false);
        let mut channel = match self
            .open_background_wrapper_channel_with_retry(wrapped_wrapper.as_str())
            .await
        {
            Ok(ch) => ch,
            Err(e) => {
                return Ok(CallToolResult::error(vec![ContentBlock::text(format!(
                    "Error: {e}"
                ))]));
            }
        };

        let (markers, initial_stdout) = match read_background_markers_from_channel(
            &mut channel,
            &job_id,
            &remote_log_path,
            super::BACKGROUND_START_TIMEOUT,
        )
        .await
        {
            Ok(v) => v,
            Err(e) => {
                return Ok(CallToolResult::error(vec![ContentBlock::text(format!(
                    "Error: {e}"
                ))]));
            }
        };

        self.register_running_job(
            &job_id,
            markers.pid,
            final_log_path_buf.clone(),
            command_for_registry,
        )
        .await;

        let streamer = OutputStreamer::new(
            job_id.clone(),
            final_log_path_buf.clone(),
            Arc::clone(&self.job_registry),
            self.spooler.clone(),
        );

        // The permit is only needed for opening the channel and reading markers
        // (both completed above). Streaming reads from an already-owned channel
        // and writes to a local file — it does not touch the SSH session or the
        // semaphore. Releasing the permit here prevents long-running detachable
        // commands from starving the command-slot pool and blocking check_process.
        drop(permit);
        let join =
            tokio::spawn(async move { streamer.stream_channel(channel, initial_stdout).await });

        let completed = tokio::time::timeout(timeout, join).await;
        let join_exit_code: Option<i32> = match completed {
            Ok(joined) => match joined {
                Ok(Ok(code)) => code,
                Ok(Err(e)) => {
                    error!(job_id = ?job_id, error = ?e, "streaming failed");
                    return Ok(CallToolResult::error(vec![ContentBlock::text(format!(
                        "Error: streaming failed: {e}"
                    ))]));
                }
                Err(e) => {
                    error!(job_id = ?job_id, error = ?e, "streaming task join failed");
                    return Ok(CallToolResult::error(vec![ContentBlock::text(format!(
                        "Error: streaming task join failed: {e}"
                    ))]));
                }
            },
            Err(_) => {
                let snapshot = self
                    .build_timeout_recovery_snapshot(&job_id, markers.pid, &final_log_path_buf)
                    .await;
                return Ok(background_json_timeout(
                    &job_id,
                    markers.pid,
                    &final_log_path,
                    &BackgroundTimeoutSnapshot {
                        state: &snapshot.state,
                        still_running: snapshot.still_running,
                        exit_code: snapshot.exit_code,
                        state_reason: snapshot.state_reason.as_deref(),
                        elapsed_time: &snapshot.elapsed_time,
                        log_exists: snapshot.log_exists,
                        log_tail: &snapshot.log_tail,
                        tail_lines_used: snapshot.tail_lines_used,
                    },
                ));
            }
        };

        let registry_exit_code: Option<i32> = match self.job_registry.get(&job_id).await {
            Some(job) => {
                let job_guard = job.lock().await;
                job_guard.exit_code
            }
            None => None,
        };

        let exit_code_u32 = registry_exit_code
            .or(join_exit_code)
            .and_then(|code| u32::try_from(code).ok())
            .unwrap_or(255)
            .min(255);

        let mut file = match tokio::fs::File::open(&final_log_path_buf).await {
            Ok(f) => f,
            Err(e) => {
                return Ok(CallToolResult::error(vec![ContentBlock::text(format!(
                    "Error: failed to read local log: {e}"
                ))]));
            }
        };

        let max_bytes = self.config.max_output_tokens.map(|t| t.saturating_mul(4));

        const TAIL_BYTES: u64 = 512;

        let stdout = match max_bytes {
            Some(limit) => {
                let meta = file.metadata().await.map_err(|e| {
                    McpError::internal_error(format!("failed to stat local log: {e}"), None)
                })?;
                let file_len = meta.len();

                if file_len <= limit as u64 {
                    let mut buf = Vec::new();
                    file.read_to_end(&mut buf).await.map_err(|e| {
                        McpError::internal_error(format!("failed to read local log: {e}"), None)
                    })?;
                    String::from_utf8_lossy(&buf).to_string()
                } else {
                    let mut head = vec![0u8; limit];
                    let mut read_total = 0usize;
                    while read_total < limit {
                        let n = file.read(&mut head[read_total..]).await.map_err(|e| {
                            McpError::internal_error(format!("failed to read local log: {e}"), None)
                        })?;
                        if n == 0 {
                            break;
                        }
                        read_total = read_total.saturating_add(n);
                    }
                    head.truncate(read_total);

                    let tail_len = std::cmp::min(TAIL_BYTES, file_len);
                    file.seek(std::io::SeekFrom::Start(file_len.saturating_sub(tail_len)))
                        .await
                        .map_err(|e| {
                            McpError::internal_error(format!("failed to seek local log: {e}"), None)
                        })?;

                    let mut tail = vec![0u8; tail_len as usize];
                    let mut tail_read = 0usize;
                    while tail_read < tail.len() {
                        let n = file.read(&mut tail[tail_read..]).await.map_err(|e| {
                            McpError::internal_error(
                                format!("failed to read local log tail: {e}"),
                                None,
                            )
                        })?;
                        if n == 0 {
                            break;
                        }
                        tail_read = tail_read.saturating_add(n);
                    }
                    tail.truncate(tail_read);

                    let total_tokens = (file_len as usize).saturating_div(4);
                    let mut out = String::from_utf8_lossy(&head).to_string();
                    out.push_str(&format!(
                        "\n[Output truncated: {} tokens total]",
                        total_tokens
                    ));
                    out.push_str(
                        "\n[Tip: Use 'head -n 100' for first lines, 'tail -n 100' for last lines]",
                    );
                    out.push_str("\n[Tip: For large output use SFTP/SCP tools to download files]");
                    if !tail.is_empty() {
                        out.push('\n');
                        out.push_str(&String::from_utf8_lossy(&tail));
                    }
                    out
                }
            }
            None => {
                let mut buf = Vec::new();
                file.read_to_end(&mut buf).await.map_err(|e| {
                    McpError::internal_error(format!("failed to read local log: {e}"), None)
                })?;
                String::from_utf8_lossy(&buf).to_string()
            }
        };

        let output = CommandOutput {
            stdout,
            stderr: String::new(),
            exit_code: Some(exit_code_u32),
            ..Default::default()
        };
        Ok(Self::calltool_from_command_output(output))
    }

    async fn build_timeout_recovery_snapshot(
        &self,
        job_id: &str,
        pid: u32,
        final_log_path_buf: &PathBuf,
    ) -> TimeoutRecoverySnapshot {
        let tail_lines_used = DEFAULT_CHECK_PROCESS_TAIL_LINES;

        match self
            .connection
            .check_process(
                job_id,
                tail_lines_used,
                self.job_registry.as_ref(),
                &self.spooler,
            )
            .await
        {
            Ok(status) => TimeoutRecoverySnapshot {
                state: status.state,
                still_running: status.running,
                exit_code: status.exit_code,
                state_reason: status.state_reason,
                elapsed_time: status.elapsed_time,
                log_exists: status.log_exists,
                log_tail: status.log_tail,
                tail_lines_used,
            },
            Err(e) => {
                warn!(
                    job_id = ?job_id,
                    pid,
                    error = ?e,
                    "Failed to gather timeout recovery snapshot via check_process path"
                );

                let mut still_running = true;
                let mut state = crate::background::JobStatus::Running.as_str().to_string();
                let mut exit_code = None;
                let mut state_reason = None;
                let mut elapsed_time = String::new();

                if let Some(job) = self.job_registry.get(job_id).await {
                    let job_guard = job.lock().await;
                    still_running =
                        matches!(job_guard.status, crate::background::JobStatus::Running);
                    state = job_guard.status.as_str().to_string();
                    exit_code = if matches!(
                        job_guard.status,
                        crate::background::JobStatus::Completed
                            | crate::background::JobStatus::Failed
                    ) {
                        job_guard
                            .exit_code
                            .and_then(|code| u32::try_from(code).ok())
                    } else {
                        None
                    };
                    state_reason = job_guard.state_reason.clone();
                    elapsed_time = job_guard.elapsed_time();
                }

                if !still_running && exit_code.is_none() {
                    state = crate::background::JobStatus::StateLost.as_str().to_string();
                    state_reason.get_or_insert_with(|| {
                        "timeout_recovery_missing_terminal_exit_code".to_string()
                    });
                }

                let log_exists = match tokio::fs::symlink_metadata(final_log_path_buf).await {
                    Ok(meta) => !meta.file_type().is_symlink() && meta.is_file(),
                    Err(e) if e.kind() == std::io::ErrorKind::NotFound => false,
                    Err(log_err) => {
                        warn!(
                            job_id = ?job_id,
                            pid,
                            error = ?log_err,
                            "Failed to stat local log for timeout recovery snapshot"
                        );
                        false
                    }
                };

                let log_tail = match read_local_log_tail(
                    final_log_path_buf.as_path(),
                    tail_lines_used,
                )
                .await
                {
                    Ok(log_tail) => log_tail,
                    Err(log_err) => {
                        warn!(
                            job_id = ?job_id,
                            pid,
                            error = ?log_err,
                            "Failed to read local log tail for timeout recovery snapshot"
                        );
                        String::new()
                    }
                };

                TimeoutRecoverySnapshot {
                    state,
                    still_running,
                    exit_code: if still_running { None } else { exit_code },
                    state_reason,
                    elapsed_time,
                    log_exists,
                    log_tail,
                    tail_lines_used,
                }
            }
        }
    }

    async fn try_open_and_exec_background_wrapper(
        &self,
        wrapped_wrapper: &str,
    ) -> std::result::Result<russh::Channel<russh::client::Msg>, String> {
        let channel = self
            .connection
            .open_channel()
            .await
            .map_err(|e| format!("failed to open background channel: {e}"))?;

        channel
            .exec(true, wrapped_wrapper)
            .await
            .map_err(|e| format!("failed to send background exec request: {e}"))?;

        Ok(channel)
    }

    pub(super) async fn open_background_wrapper_channel_with_retry(
        &self,
        wrapped_wrapper: &str,
    ) -> std::result::Result<russh::Channel<russh::client::Msg>, String> {
        match self
            .try_open_and_exec_background_wrapper(wrapped_wrapper)
            .await
        {
            Ok(channel) => Ok(channel),
            Err(first_err) => {
                warn!(
                    error = ?first_err,
                    "Background wrapper pre-exec failed, reconnecting once"
                );

                if let Err(reconnect_err) = self.connection.reconnect().await {
                    return Err(format!(
                        "background pre-exec failed ({first_err}); reconnect failed: {reconnect_err}"
                    ));
                }

                self.try_open_and_exec_background_wrapper(wrapped_wrapper)
                    .await
                    .map_err(|retry_err| {
                        format!(
                            "background pre-exec failed ({first_err}); retry failed: {retry_err}"
                        )
                    })
            }
        }
    }

    pub(super) async fn execute_background_impl(
        &self,
        command: &str,
        log_path: Option<&str>,
        privilege: BackgroundPrivilege<'_>,
    ) -> std::result::Result<CallToolResult, McpError> {
        let job_id = super::make_job_id();
        let remote_log_path = remote_job_log_path(&job_id);

        let (final_log_path_buf, final_log_path) = match log_path {
            Some(p) => (PathBuf::from(p), p.to_string()),
            None => match self.default_local_log_path(&job_id) {
                Ok(v) => v,
                Err(e) => {
                    return Ok(background_json_err(&e, ""));
                }
            },
        };

        if let Some(p) = log_path
            && let Err(e) = validate_background_log_path(self.spooler.base_dir(), p)
        {
            return Ok(background_json_err(&e, ""));
        }

        if let Err(e) = self.ensure_local_log_file(&final_log_path_buf).await {
            return Ok(background_json_err(&e.to_string(), ""));
        }

        let sanitized = match sanitize_command(command, self.max_chars) {
            Ok(cmd) => cmd,
            Err(e) => {
                return Ok(background_json_err(&e.to_string(), ""));
            }
        };

        // Ensure connection is established
        if let Err(e) = self.connection.ensure_connected().await {
            return Ok(background_json_err(&e.to_string(), ""));
        }

        let (command_for_exec, command_for_registry, attempt_su_elevation, log_msg) =
            match privilege {
                BackgroundPrivilege::Normal => (
                    sanitized.clone(),
                    sanitized.clone(),
                    true,
                    "streaming failed for background job",
                ),
                BackgroundPrivilege::Sudo { password } => {
                    let wrapped_command = wrap_sudo_command(&sanitized, password);
                    debug!(
                        "Wrapped sudo command (password hidden): sudo -n sh -c '...' or printf '...' | sudo ..."
                    );
                    (
                        wrapped_command,
                        format!("sudo {sanitized}"),
                        false,
                        "streaming failed for background sudo job",
                    )
                }
            };

        // If su elevation is configured and available, ensure we're elevated (best-effort)
        if attempt_su_elevation
            && self.connection.get_su_password().is_some()
            && let Err(e) = self.connection.ensure_elevated().await
        {
            debug!(error = ?e, "Elevation failed, will run as normal user");
        }

        let wrapper = build_background_wrapper_script(&job_id, &command_for_exec, &remote_log_path);

        let permit = match self.connection.acquire_command_slot_raw().await {
            Ok(p) => p,
            Err(e) => {
                return Ok(background_json_err(
                    &format!("Failed to acquire command slot: {e}"),
                    "",
                ));
            }
        };

        let wrapped_wrapper = wrap_in_posix_shell(&wrapper, false);
        let mut channel = match self
            .open_background_wrapper_channel_with_retry(wrapped_wrapper.as_str())
            .await
        {
            Ok(ch) => ch,
            Err(e) => {
                return Ok(background_json_err(&e, ""));
            }
        };

        let (markers, initial_stdout) = match read_background_markers_from_channel(
            &mut channel,
            &job_id,
            &remote_log_path,
            super::BACKGROUND_START_TIMEOUT,
        )
        .await
        {
            Ok(v) => v,
            Err(e) => {
                return Ok(background_json_err(&e, ""));
            }
        };

        self.register_running_job(
            &job_id,
            markers.pid,
            final_log_path_buf.clone(),
            &command_for_registry,
        )
        .await;

        let streamer = OutputStreamer::new(
            job_id.clone(),
            final_log_path_buf.clone(),
            Arc::clone(&self.job_registry),
            self.spooler.clone(),
        );

        let job_id_for_log = job_id.clone();

        // The permit is only needed for opening the channel and reading markers
        // (both completed above). Streaming reads from an already-owned channel
        // and writes to a local file — it does not touch the SSH session or the
        // semaphore. Releasing the permit here prevents long-running background
        // jobs from starving the command-slot pool and blocking check_process.
        drop(permit);
        tokio::spawn(async move {
            if let Err(e) = streamer.stream_channel(channel, initial_stdout).await {
                error!(job_id = ?job_id_for_log, error = ?e, "{log_msg}");
            }
        });

        Ok(background_json_ok(&job_id, markers.pid, &final_log_path))
    }
}