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
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
//! MCP Server implementation
//!
//! This module provides the main MCP server that integrates SSH connection
//! management with the `shell` and `sudo_shell` tools.

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use std::time::{SystemTime, UNIX_EPOCH};

use rmcp::{
    ErrorData as McpError,
    handler::server::ServerHandler,
    model::*,
    service::{RequestContext, RoleServer},
};
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, warn};

use crate::background::job::NewRunningJob;
use crate::background::transfer::{SharedTransferJob, TransferJobRegistry};
use crate::background::{JobRegistry, JobState, LocalLogSpooler, SharedJobState};
use crate::config::Config;
use crate::error::{Result, SshMcpError};
#[cfg(unix)]
use crate::platform::O_NOFOLLOW_FLAG;
use crate::server::handlers::file_edit_common::{FileEditFaultInjection, FileEditPrivilege};
#[cfg(test)]
use crate::server::validation::validate_background_log_path;
use crate::ssh::{
    CommandOutput, SshConfig, SshConnectionManager, sanitize_command, wrap_sudo_command,
};
use crate::tools::ApplyPatchParams;
use crate::transfer::{
    TransferEngine, TransferEventSink, TransferParams, TransferResponse, TransferRunContext,
    TransferSshOptions,
};

mod args;
mod exec;
mod handlers;
mod testing;
mod tools;
mod validation;

const BACKGROUND_START_TIMEOUT: Duration = Duration::from_secs(20);

const JOB_COMPLETED_RETENTION: Duration = Duration::from_secs(60 * 60);

static JOB_COUNTER: AtomicU64 = AtomicU64::new(0);

fn make_job_id() -> String {
    let counter = JOB_COUNTER.fetch_add(1, Ordering::Relaxed);
    let epoch_ms = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    format!("{}-{}", epoch_ms, counter)
}

/// SeSSHion SSH MCP server
///
/// The main server implementation that provides MCP tools for remote SSH
/// command execution.
#[derive(Clone)]
pub struct SshMcpServer {
    /// Server configuration
    config: Config,

    /// SSH connection manager
    connection: Arc<SshConnectionManager>,

    /// Command execution timeout
    timeout: Duration,

    /// Maximum command length
    max_chars: Option<usize>,

    spooler: Arc<LocalLogSpooler>,
    job_registry: Arc<JobRegistry>,

    transfer_job_registry: Arc<TransferJobRegistry>,
    transfer_tasks: Arc<Mutex<Vec<JoinHandle<()>>>>,
    transfer_shutdown: CancellationToken,

    transfer: TransferEngine,
}

struct TransferTerminalGuard {
    job: SharedTransferJob,
    fallback: Option<TransferResponse>,
}

impl TransferTerminalGuard {
    fn new(job: SharedTransferJob, fallback: TransferResponse) -> Self {
        Self {
            job,
            fallback: Some(fallback),
        }
    }

    fn finish(mut self, response: &TransferResponse) {
        if let Ok(mut job) = self.job.lock() {
            job.finish(response);
        }
        self.fallback = None;
    }
}

impl Drop for TransferTerminalGuard {
    fn drop(&mut self) {
        let Some(fallback) = self.fallback.take() else {
            return;
        };
        if let Ok(mut job) = self.job.lock()
            && !job.is_terminal()
        {
            job.finish(&fallback);
        }
    }
}

impl SshMcpServer {
    /// Create a new SeSSHion server
    ///
    /// This sets up the SSH connection manager based on the provided configuration.
    /// Connection is not established until a tool is actually used.
    pub async fn new(config: Config) -> Result<Self> {
        Self::new_with_spool_dir(config, None).await
    }

    /// Create a new SeSSHion server with an optional local spool directory.
    pub async fn new_with_spool_dir(config: Config, spool_dir: Option<PathBuf>) -> Result<Self> {
        let local_root = std::env::current_dir()?;

        let spooler = Arc::new(resolve_local_spooler(spool_dir)?);
        spooler.ensure_dir().await.map_err(|e| {
            SshMcpError::Config(format!(
                "failed to initialize local log spool dir {}: {e}",
                spooler.base_dir().display()
            ))
        })?;
        let job_registry = Arc::new(JobRegistry::new(JOB_COMPLETED_RETENTION));
        let transfer_job_registry = Arc::new(TransferJobRegistry::new(JOB_COMPLETED_RETENTION));

        // Build SSH configuration
        let mut ssh_config = SshConfig::new(&config.host, &config.user).with_port(config.port);

        // Add authentication
        if let Some(ref password) = config.password {
            ssh_config = ssh_config.with_password(password);
        }

        if let Some(ref key_path) = config.key {
            // Read the key file
            let key_content = tokio::fs::read_to_string(key_path)
                .await
                .map_err(SshMcpError::Io)?;
            ssh_config = ssh_config.with_private_key(&key_content);
        }

        // Add elevation passwords if provided
        if let Some(ref su_password) = config.su_password {
            ssh_config = ssh_config.with_su_password(su_password);
        }

        if let Some(ref sudo_password) = config.sudo_password {
            ssh_config = ssh_config.with_sudo_password(sudo_password);
        }

        // Add keepalive settings for human-like connection persistence
        ssh_config = ssh_config
            .with_keepalive_interval(config.keepalive_interval)
            .with_keepalive_max(config.keepalive_max);

        // Add reconnect and health probe settings
        ssh_config = ssh_config
            .with_reconnect_retries(config.reconnect_retries)
            .with_reconnect_backoff_ms(config.reconnect_backoff_ms)
            .with_health_probe_timeout_ms(config.health_probe_timeout_ms);

        // Add host key verification settings
        ssh_config = ssh_config
            .with_host_key_checking(config.strict_host_key_checking)
            .with_known_hosts(config.known_hosts.clone());

        // Add output token limit for OOM protection
        ssh_config = ssh_config.with_max_output_tokens(config.max_output_tokens);

        // Create connection manager
        let connection = Arc::new(SshConnectionManager::new(ssh_config).await);

        let timeout = Duration::from_millis(config.timeout_ms);
        let max_chars = config.max_chars;

        Ok(Self {
            config,
            connection,
            timeout,
            max_chars,
            spooler,
            job_registry,
            transfer_job_registry,
            transfer_tasks: Arc::new(Mutex::new(Vec::new())),
            transfer_shutdown: CancellationToken::new(),
            transfer: TransferEngine::new(local_root),
        })
    }

    fn connection_id(&self) -> String {
        format!(
            "{}@{}:{}",
            self.config.user, self.config.host, self.config.port
        )
    }

    fn default_local_log_path(
        &self,
        job_id: &str,
    ) -> std::result::Result<(PathBuf, String), String> {
        let path = self
            .spooler
            .log_path_for(job_id)
            .map_err(|e| format!("failed to generate local log path for job_id='{job_id}': {e}"))?;
        let path_str = path.to_string_lossy().to_string();
        Ok((path, path_str))
    }

    async fn ensure_local_log_file(&self, log_path: &Path) -> std::result::Result<(), SshMcpError> {
        self.spooler.ensure_dir().await.map_err(|e| {
            SshMcpError::Config(format!(
                "failed to ensure local log spool dir {}: {e}",
                self.spooler.base_dir().display()
            ))
        })?;

        if log_path.parent() != Some(self.spooler.base_dir()) {
            return Err(SshMcpError::InvalidParams(format!(
                "log_path must be directly under {}",
                self.spooler.base_dir().display()
            )));
        }

        match tokio::fs::symlink_metadata(log_path).await {
            Ok(meta) => {
                let ft = meta.file_type();
                if ft.is_symlink() {
                    return Err(SshMcpError::invalid_params(
                        "log_path is a symlink (refusing to follow it)",
                    ));
                }
                if !ft.is_file() {
                    return Err(SshMcpError::invalid_params(
                        "log_path exists but is not a regular file",
                    ));
                }
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
            Err(e) => return Err(SshMcpError::Io(e)),
        }

        let mut opts = tokio::fs::OpenOptions::new();
        opts.write(true).create(true).truncate(true);

        #[cfg(unix)]
        {
            opts.custom_flags(O_NOFOLLOW_FLAG);
        }

        let file = match opts.open(log_path).await {
            Ok(f) => f,
            Err(e) => {
                if let Ok(meta) = tokio::fs::symlink_metadata(log_path).await
                    && meta.file_type().is_symlink()
                {
                    return Err(SshMcpError::invalid_params(
                        "log_path is a symlink (refusing to follow it)",
                    ));
                }
                return Err(SshMcpError::Io(e));
            }
        };

        file.sync_all().await.map_err(SshMcpError::Io)
    }

    async fn register_running_job(
        &self,
        job_id: &str,
        pid: u32,
        log_path: PathBuf,
        command: &str,
    ) -> SharedJobState {
        let job = Arc::new(Mutex::new(JobState::new_running(NewRunningJob {
            job_id: job_id.to_string(),
            pid,
            log_path,
            command: command.to_string(),
            connection_id: self.connection_id(),
        })));

        self.job_registry
            .insert(job_id.to_string(), Arc::clone(&job))
            .await;

        let persisted = {
            let guard = job.lock().await;
            guard.clone()
        };
        if let Err(e) = self.spooler.persist_job_state(&persisted).await {
            warn!(job_id = ?job_id, error = ?e, "failed to persist running job state");
        }

        job
    }

    /// Get a reference to the SSH connection manager
    pub fn connection(&self) -> &Arc<SshConnectionManager> {
        &self.connection
    }

    /// Close the server and cleanup resources
    pub async fn shutdown(&self) {
        info!("Shutting down SeSSHion...");
        self.transfer_shutdown.cancel();
        let tasks = {
            let mut tasks = self.transfer_tasks.lock().await;
            std::mem::take(&mut *tasks)
        };
        let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
        for mut task in tasks {
            if tokio::time::timeout_at(deadline, &mut task).await.is_err() {
                task.abort();
                let _ = task.await;
            }
        }
        self.connection.close().await;
    }

    /// Execute a command (used by shell tool)
    async fn execute_command_with_timeout(
        &self,
        command: &str,
        timeout: Duration,
    ) -> std::result::Result<CallToolResult, McpError> {
        debug!(
            "shell tool called: cmd_len={}, background=false, sudo=false, timeout_ms={}",
            command.len(),
            timeout.as_millis()
        );

        // Sanitize the command
        let sanitized = match self.sanitize_or_tool_error(command) {
            Ok(cmd) => cmd,
            Err(result) => return Ok(result),
        };

        // Foreground execution is detachable-by-design:
        // - Start the command on a dedicated SSH channel
        // - Stream remote stdout/stderr into a local spool file
        // - If timeout elapses, return JSON with job_id/pid/log_path while the stream continues

        let requires_elevation = self.connection.get_su_password().is_some();
        if requires_elevation {
            if let Err(e) = self.connection.ensure_connected().await {
                error!(error = ?e, "Failed to ensure SSH connection");
                return Ok(CallToolResult::error(vec![ContentBlock::text(
                    e.to_string(),
                )]));
            }

            if let Err(e) = self.connection.ensure_elevated().await {
                debug!(error = ?e, "Elevation failed, will run as normal user");
            }
        }

        // Ensure connection is established for detached foreground execution path.
        if !requires_elevation && let Err(e) = self.connection.ensure_connected().await {
            error!(error = ?e, "Failed to ensure SSH connection");
            return Ok(CallToolResult::error(vec![ContentBlock::text(
                e.to_string(),
            )]));
        }

        self.execute_detachable_foreground_impl(&sanitized, &sanitized, timeout)
            .await
    }

    async fn execute_command(
        &self,
        command: &str,
    ) -> std::result::Result<CallToolResult, McpError> {
        self.execute_command_with_timeout(command, self.timeout)
            .await
    }

    async fn execute_background_command(
        &self,
        command: &str,
        log_path: Option<&str>,
    ) -> std::result::Result<CallToolResult, McpError> {
        self.execute_background_impl(command, log_path, exec::BackgroundPrivilege::Normal)
            .await
    }

    /// Execute a command with sudo (used by sudo_shell tool)
    async fn execute_sudo_command_with_timeout(
        &self,
        command: &str,
        timeout: Duration,
    ) -> std::result::Result<CallToolResult, McpError> {
        debug!(
            "sudo_shell tool called: cmd_len={}, background=false, sudo=true, timeout_ms={}",
            command.len(),
            timeout.as_millis()
        );

        // Sanitize the command
        let sanitized = match self.sanitize_or_tool_error(command) {
            Ok(cmd) => cmd,
            Err(result) => return Ok(result),
        };

        // Wrap the command with sudo
        let sudo_password = self.connection.get_sudo_password();
        let wrapped_command = wrap_sudo_command(&sanitized, sudo_password);
        debug!(
            "Wrapped sudo command (password hidden): sudo -n sh -c '...' or printf '...' | sudo ..."
        );

        if let Err(e) = self.connection.ensure_connected().await {
            error!(error = ?e, "Failed to ensure SSH connection");
            return Ok(CallToolResult::error(vec![ContentBlock::text(
                e.to_string(),
            )]));
        }

        self.execute_detachable_foreground_impl(
            &wrapped_command,
            &format!("sudo {sanitized}"),
            timeout,
        )
        .await
    }

    async fn execute_sudo_command(
        &self,
        command: &str,
    ) -> std::result::Result<CallToolResult, McpError> {
        self.execute_sudo_command_with_timeout(command, self.timeout)
            .await
    }

    async fn execute_background_sudo_command(
        &self,
        command: &str,
        log_path: Option<&str>,
    ) -> std::result::Result<CallToolResult, McpError> {
        let sudo_password = self.connection.get_sudo_password();
        self.execute_background_impl(
            command,
            log_path,
            exec::BackgroundPrivilege::Sudo {
                password: sudo_password,
            },
        )
        .await
    }

    fn sanitize_or_tool_error(&self, command: &str) -> std::result::Result<String, CallToolResult> {
        sanitize_command(command, self.max_chars).map_err(|e| {
            error!(error = ?e, "Command sanitization failed");
            CallToolResult::error(vec![ContentBlock::text(format!("Error: {}", e))])
        })
    }

    fn calltool_from_command_output(output: CommandOutput) -> CallToolResult {
        // Combine stdout and stderr for the response
        let mut result_text = output.stdout;
        if !output.stderr.is_empty() {
            if !result_text.is_empty() {
                result_text.push_str("\n--- stderr ---\n");
            }
            result_text.push_str(&output.stderr);
        }

        // Check for error exit code.
        // exit_code=None means the SSH channel was torn down without delivering
        // an exit status or exit signal — treat as error, not success.
        if output.exit_code.map(|code| code != 0).unwrap_or(true) {
            CallToolResult::error(vec![ContentBlock::text(result_text)])
        } else {
            CallToolResult::success(vec![ContentBlock::text(result_text)])
        }
    }

    /// Build shell tool definition (compact)
    fn shell_tool() -> Tool {
        tools::shell_tool()
    }

    /// Build sudo_shell tool definition (compact)
    fn sudo_shell_tool() -> Tool {
        tools::sudo_shell_tool()
    }

    /// Build transfer tool definition (compact)
    fn transfer_tool() -> Tool {
        tools::transfer_tool()
    }

    /// Build check_process tool definition
    fn check_process_tool() -> Tool {
        tools::check_process_tool()
    }

    /// Build apply_patch tool definition
    fn apply_patch_tool() -> Tool {
        tools::apply_patch_tool()
    }

    /// Build sudo_apply_patch tool definition
    fn sudo_apply_patch_tool() -> Tool {
        tools::sudo_apply_patch_tool()
    }

    /// Resolve timeout duration from optional milliseconds, falling back to server default.
    fn resolve_timeout(&self, timeout_ms: Option<u64>) -> Duration {
        timeout_ms
            .map(Duration::from_millis)
            .unwrap_or(self.timeout)
    }

    /// Parse tool parameters from JSON with standardized error handling.
    fn parse_tool_params<T: serde::de::DeserializeOwned>(
        &self,
        args: serde_json::Map<String, serde_json::Value>,
        tool_name: &str,
    ) -> std::result::Result<T, McpError> {
        serde_json::from_value(serde_json::Value::Object(args))
            .map_err(|e| McpError::invalid_params(format!("invalid {tool_name} params: {e}"), None))
    }

    async fn run_transfer_response(
        &self,
        params: TransferParams,
        cancellation: CancellationToken,
        progress: Option<TransferEventSink>,
    ) -> TransferResponse {
        let timeout = self.resolve_timeout(params.timeout_ms);
        let key_path = self.config.key.clone();

        self.transfer
            .run_controlled(
                &self.connection,
                params,
                TransferRunContext {
                    timeout,
                    ssh: TransferSshOptions {
                        host: self.config.host.clone(),
                        port: self.config.port,
                        user: self.config.user.clone(),
                        key_path,
                        host_key_checking: self.config.strict_host_key_checking,
                        known_hosts: self.config.known_hosts.clone(),
                    },
                },
                cancellation,
                progress,
            )
            .await
    }

    /// Execute a foreground transfer and serialize its response.
    async fn execute_transfer(
        &self,
        params: TransferParams,
        verbose: bool,
        cancellation: CancellationToken,
    ) -> std::result::Result<CallToolResult, McpError> {
        let resp = self.run_transfer_response(params, cancellation, None).await;
        let body = resp
            .to_json(verbose)
            .unwrap_or_else(|_| "{\"ok\":false,\"error\":\"serialization_error\"}".to_string());
        Ok(CallToolResult::success(vec![ContentBlock::text(body)]))
    }

    async fn execute_background_transfer(
        &self,
        params: TransferParams,
    ) -> std::result::Result<CallToolResult, McpError> {
        let job_id = format!("transfer-{}", make_job_id());
        let job = self.transfer_job_registry.register(job_id.clone());
        let progress_job = Arc::clone(&job);
        let progress = TransferEventSink::new(move |event| {
            if let Ok(mut job) = progress_job.lock() {
                job.apply_event(event);
            }
        });

        let server = self.clone();
        let task_params = params.clone();
        let fallback = TransferResponse::error(
            params,
            self.transfer.local_root(),
            "background transfer stopped unexpectedly",
        );
        let body = serde_json::json!({
            "ok": true,
            "background": true,
            "job_id": job_id,
            "job_type": "transfer",
            "state": "running",
            "phase": "queued",
        });

        let mut tasks = self.transfer_tasks.lock().await;
        tasks.retain(|task| !task.is_finished());
        let task = tokio::spawn(async move {
            tokio::task::yield_now().await;
            let terminal = TransferTerminalGuard::new(job, fallback);
            let response = server
                .run_transfer_response(
                    task_params,
                    server.transfer_shutdown.child_token(),
                    Some(progress),
                )
                .await;
            terminal.finish(&response);
        });
        tasks.push(task);
        drop(tasks);

        Ok(CallToolResult::success(vec![ContentBlock::text(
            body.to_string(),
        )]))
    }
}

fn resolve_local_spooler(spool_dir: Option<PathBuf>) -> Result<LocalLogSpooler> {
    match spool_dir {
        Some(path) if !path.is_absolute() => Err(SshMcpError::Config(format!(
            "spool directory must be absolute: {}",
            path.display()
        ))),
        Some(path) => Ok(LocalLogSpooler::new(path)),
        None => Ok(LocalLogSpooler::new_default()),
    }
}

fn server_implementation() -> Implementation {
    Implementation::new("ssh-mcp", env!("CARGO_PKG_VERSION"))
        .with_title("SeSSHion")
        .with_description(env!("CARGO_PKG_DESCRIPTION"))
        .with_website_url("https://github.com/0FL01/SeSSHion")
}

impl ServerHandler for SshMcpServer {
    /// Return server information
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
            .with_protocol_version(ProtocolVersion::LATEST)
            .with_server_info(server_implementation())
            .with_instructions(format!(
                "SeSSHion v{} - SSH MCP server for {}@{}:{}",
                env!("CARGO_PKG_VERSION"),
                self.config.user,
                self.config.host,
                self.config.port,
            ))
    }

    /// List available tools
    async fn list_tools(
        &self,
        _request: Option<PaginatedRequestParams>,
        _context: RequestContext<RoleServer>,
    ) -> std::result::Result<ListToolsResult, McpError> {
        debug!("list_tools called");

        let mut tools = vec![Self::shell_tool()];

        // Docs/expected order: shell, optional sudo tools, check_process, transfer, apply_patch.
        if !self.config.disable_sudo {
            tools.push(Self::sudo_shell_tool());
            tools.push(Self::sudo_apply_patch_tool());
        }
        tools.push(Self::check_process_tool());
        tools.push(Self::transfer_tool());
        tools.push(Self::apply_patch_tool());

        Ok(ListToolsResult {
            tools,
            next_cursor: None,
            meta: Default::default(),
        })
    }

    /// Call a tool
    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> std::result::Result<CallToolResult, McpError> {
        let tool_name: &str = request.name.as_ref();
        debug!("call_tool called: {:?}", tool_name);

        let args = request.arguments.unwrap_or_default();

        // Route to the appropriate tool
        match tool_name {
            "shell" => {
                let parsed = self.parse_common_tool_args(&args)?;
                let timeout = self.resolve_timeout(parsed.timeout_ms);

                if parsed.background {
                    self.execute_background_command(&parsed.command, parsed.log_path.as_deref())
                        .await
                } else {
                    self.execute_command_with_timeout(&parsed.command, timeout)
                        .await
                }
            }
            "sudo_shell" => {
                if self.config.disable_sudo {
                    return Err(McpError::invalid_params(
                        "sudo_shell tool is disabled",
                        None,
                    ));
                }

                let parsed = self.parse_common_tool_args(&args)?;
                let timeout = self.resolve_timeout(parsed.timeout_ms);

                if parsed.background {
                    self.execute_background_sudo_command(
                        &parsed.command,
                        parsed.log_path.as_deref(),
                    )
                    .await
                } else {
                    self.execute_sudo_command_with_timeout(&parsed.command, timeout)
                        .await
                }
            }
            "transfer" => {
                let params: TransferParams = self.parse_tool_params(args, "transfer")?;
                let verbose = params.verbose;
                if params.background {
                    self.execute_background_transfer(params).await
                } else {
                    self.execute_transfer(params, verbose, context.ct.clone())
                        .await
                }
            }
            "check_process" => {
                let params: args::CheckProcessToolArgs =
                    self.parse_tool_params(args, "check_process")?;
                self.execute_check_process(params.check, params.wait_for, context.ct.cancelled())
                    .await
            }
            "apply_patch" => {
                let params: ApplyPatchParams = self.parse_tool_params(args, "apply_patch")?;
                self.execute_apply_patch(
                    params,
                    FileEditFaultInjection::None,
                    FileEditPrivilege::User,
                )
                .await
            }
            "sudo_apply_patch" => {
                if self.config.disable_sudo {
                    return Err(McpError::invalid_params(
                        "sudo_apply_patch tool is disabled",
                        None,
                    ));
                }

                let params: ApplyPatchParams = self.parse_tool_params(args, "sudo_apply_patch")?;
                self.execute_apply_patch(
                    params,
                    FileEditFaultInjection::None,
                    FileEditPrivilege::Sudo,
                )
                .await
            }
            _ => Err(McpError::invalid_params(
                format!("Unknown tool: {}", tool_name),
                None,
            )),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::background::response::{
        BACKGROUND_JSON_SNIPPET_LIMIT_CHARS, background_json_err, background_json_timeout,
    };
    use crate::background::wrapper::{build_background_wrapper_script, remote_job_log_path};

    fn extract_text_from_result(result: &CallToolResult) -> String {
        result
            .content
            .iter()
            .filter_map(|c| c.as_text().map(|text| text.text.clone()))
            .collect::<Vec<_>>()
            .join("\n")
    }

    #[test]
    fn test_server_info() {
        let implementation = server_implementation();

        assert_eq!(implementation.name, "ssh-mcp");
        assert_eq!(implementation.title.as_deref(), Some("SeSSHion"));
        assert_eq!(implementation.version, env!("CARGO_PKG_VERSION"));
        assert_eq!(
            implementation.website_url.as_deref(),
            Some("https://github.com/0FL01/SeSSHion")
        );
        assert_eq!(
            implementation.description.as_deref(),
            Some(env!("CARGO_PKG_DESCRIPTION"))
        );
    }

    #[test]
    fn test_resolve_local_spooler_rejects_relative_override() {
        let error = resolve_local_spooler(Some(PathBuf::from("relative/spool")))
            .expect_err("relative spool directory must be rejected");

        assert!(matches!(
            error,
            SshMcpError::Config(message) if message.contains("must be absolute")
        ));
    }

    #[test]
    fn test_shell_tool_definition() {
        let tool = SshMcpServer::shell_tool();
        assert_eq!(tool.name.as_ref(), "shell");
        assert!(tool.description.is_some());
    }

    #[test]
    fn test_sudo_shell_tool_definition() {
        let tool = SshMcpServer::sudo_shell_tool();
        assert_eq!(tool.name.as_ref(), "sudo_shell");
        assert!(tool.description.is_some());
    }

    #[test]
    fn test_apply_patch_tool_definition() {
        let tool = SshMcpServer::apply_patch_tool();
        assert_eq!(tool.name.as_ref(), "apply_patch");
        assert!(tool.description.is_some());
    }

    #[test]
    fn test_sudo_apply_patch_tool_definition() {
        let tool = SshMcpServer::sudo_apply_patch_tool();
        assert_eq!(tool.name.as_ref(), "sudo_apply_patch");
        assert!(tool.description.is_some());
    }

    #[test]
    fn test_build_background_wrapper_escapes_single_quotes_in_user_command() {
        let remote_log = remote_job_log_path("job-1");
        let script = build_background_wrapper_script("job-1", "echo 'hello world'", &remote_log);
        assert!(script.contains("exec sh -c 'set +m; echo '\"'\"'hello world'\"'\"''"));
    }

    #[test]
    fn test_build_background_wrapper_is_busybox_friendly() {
        let remote_log = remote_job_log_path("job-1");
        let script = build_background_wrapper_script("job-1", "echo test", &remote_log);
        assert!(!script.contains("dirname --"));
        assert!(!script.contains("mkdir -p --"));
        assert!(!script.contains("sh -lc"));
        assert!(script.contains("exec sh -c"));
        assert!(!script.contains("nohup"));
    }

    #[test]
    fn test_background_wrapper_emits_markers_and_exec() {
        let remote_log = remote_job_log_path("job-1");
        let script = build_background_wrapper_script("job-1", "echo test", &remote_log);
        assert!(script.contains("__SSH_MCP_JOB_ID=job-1"));
        assert!(script.contains("__SSH_MCP_PID=$$"));
        assert!(script.contains("__SSH_MCP_LOG=$LOG"));
        assert!(script.contains("exec sh -c"));
    }

    #[test]
    fn test_background_wrapper_does_not_redirect_remote_output() {
        let remote_log = remote_job_log_path("job-1");
        let script = build_background_wrapper_script("job-1", "echo test", &remote_log);
        assert!(!script.contains(">$LOG"));
        assert!(!script.contains("2>&1"));
        assert!(!script.contains("$EXIT"));
        assert!(!script.contains("nohup"));
    }

    #[test]
    fn test_validate_background_log_path_rejects_leading_dash() {
        let err =
            validate_background_log_path(Path::new("/tmp/ssh-mcp"), "-not-a-path").unwrap_err();
        assert!(err.contains("start with '-'") || err.contains("start with"));
    }

    #[test]
    fn test_validate_background_log_path_rejects_newlines() {
        assert!(
            validate_background_log_path(Path::new("/tmp/ssh-mcp"), "/tmp/x\nrm -rf /").is_err()
        );
        assert!(
            validate_background_log_path(Path::new("/tmp/ssh-mcp"), "/tmp/x\rrm -rf /").is_err()
        );
    }

    #[test]
    fn test_background_json_err_omits_unregistered_job_fields() {
        let long_error = "e".repeat(BACKGROUND_JSON_SNIPPET_LIMIT_CHARS + 10);
        let long_stderr = "s".repeat(BACKGROUND_JSON_SNIPPET_LIMIT_CHARS + 10);

        let result = background_json_err(&long_error, &long_stderr);
        let text = extract_text_from_result(&result);

        let value: serde_json::Value =
            serde_json::from_str(text.trim()).expect("background_json_err should return JSON");

        assert_eq!(value.get("ok").and_then(|v| v.as_bool()), Some(false));
        assert_eq!(
            value.get("background").and_then(|v| v.as_bool()),
            Some(true)
        );
        assert_eq!(value.get("truncated").and_then(|v| v.as_bool()), Some(true));
        assert!(value.get("job_id").is_none());
        assert!(value.get("log_path").is_none());
        assert!(value.get("hint").is_none());

        let fields = value
            .get("truncated_fields")
            .expect("expected truncated_fields");
        assert_eq!(fields.get("error").and_then(|v| v.as_bool()), Some(true));
        assert_eq!(fields.get("stderr").and_then(|v| v.as_bool()), Some(true));

        let error_snippet = value
            .get("error")
            .and_then(|v| v.as_str())
            .expect("expected error field");
        assert_eq!(
            error_snippet.chars().count(),
            BACKGROUND_JSON_SNIPPET_LIMIT_CHARS
        );
        let stderr_snippet = value
            .get("stderr")
            .and_then(|v| v.as_str())
            .expect("expected stderr field");
        assert_eq!(
            stderr_snippet.chars().count(),
            BACKGROUND_JSON_SNIPPET_LIMIT_CHARS
        );
    }

    #[test]
    fn test_background_json_timeout_hint_contains_pid_and_check_process_tool() {
        let result = background_json_timeout(
            "job-42",
            4242,
            "/tmp/ssh-mcp/local.log",
            &crate::background::response::BackgroundTimeoutSnapshot {
                state: "running",
                still_running: true,
                exit_code: None,
                state_reason: None,
                elapsed_time: "00:01",
                log_exists: true,
                log_tail: "tail line",
                tail_lines_used: 50,
            },
        );
        let text = extract_text_from_result(&result);

        let value: serde_json::Value =
            serde_json::from_str(text.trim()).expect("background_json_timeout should return JSON");

        assert_eq!(value.get("ok").and_then(|v| v.as_bool()), Some(false));
        assert_eq!(value.get("timeout").and_then(|v| v.as_bool()), Some(true));
        assert_eq!(
            value.get("background").and_then(|v| v.as_bool()),
            Some(true)
        );
        assert_eq!(
            value.get("still_running").and_then(|v| v.as_bool()),
            Some(true)
        );
        assert_eq!(value.get("state").and_then(|v| v.as_str()), Some("running"));
        assert_eq!(
            value.get("tail_lines_used").and_then(|v| v.as_u64()),
            Some(50)
        );
        assert_eq!(
            value.get("elapsed_time").and_then(|v| v.as_str()),
            Some("00:01")
        );
        assert_eq!(
            value.get("log_tail").and_then(|v| v.as_str()),
            Some("tail line")
        );

        let hint = value
            .get("hint")
            .and_then(|v| v.as_str())
            .expect("expected hint field");

        // Hint should contain the actual job_id value
        assert!(
            hint.contains("job_id=job-42"),
            "hint should contain the actual job_id value; got: '{hint}'"
        );
        // Hint should mention the check_process tool
        assert!(
            hint.contains("check_process"),
            "hint should mention check_process tool; got: '{hint}'"
        );
        // Hint should warn against restarting
        assert!(
            hint.contains("DO NOT restart"),
            "hint should warn against restarting; got: '{hint}'"
        );
        // Hint should use TIMEOUT_RECOVERY prefix
        assert!(
            hint.contains("TIMEOUT_RECOVERY"),
            "hint should start with TIMEOUT_RECOVERY; got: '{hint}'"
        );
        assert!(
            hint.contains("MCP client deadlines may be shorter than timeout_ms"),
            "hint should distinguish the client deadline from timeout_ms; got: '{hint}'"
        );
        assert!(
            hint.contains("background=true"),
            "hint should recommend explicit background mode; got: '{hint}'"
        );
        // Hint should NOT contain old placeholders
        assert!(
            !hint.contains("<pid>"),
            "hint should not contain <pid> placeholder; got: '{hint}'"
        );
        assert!(
            !hint.contains("<log_path>"),
            "hint should not contain <log_path> placeholder; got: '{hint}'"
        );
    }
}