Skip to main content

pty_mcp/app/
mod.rs

1mod context;
2mod local_sessions;
3mod ssh_connections;
4mod ssh_files;
5mod ssh_mounts;
6mod ssh_sessions;
7mod ssh_tunnels;
8mod support;
9pub mod types;
10
11use std::sync::Arc;
12
13use anyhow::Result;
14
15use context::AppContext;
16
17pub use types::{
18    SpawnSessionRequest, SshConnectRequest, SshConnectResult, SshDirectoryEntry,
19    SshDirectoryEntryType, SshDisconnectRequest, SshDisconnectResult, SshExecRequest,
20    SshListDirectoryResult, SshListResult, SshMkdirResult, SshMountRequest, SshReadFileResult,
21    SshRunRequest, SshRunResult, SshSessionSpawnRequest, SshTunnelCloseRequest,
22    SshTunnelCloseResult, SshTunnelOpenRequest, SshTunnelOpenResult, SshUnmountRequest,
23    SshUnmountResult, SshWriteFileResult,
24};
25
26use crate::{Config, buffer::BufferReadRequest, session::SessionId, ssh::SshCapabilityView};
27
28#[derive(Debug, Clone)]
29pub struct LocalSessionService {
30    context: Arc<AppContext>,
31}
32
33impl LocalSessionService {
34    fn new(context: Arc<AppContext>) -> Self {
35        Self { context }
36    }
37}
38
39#[derive(Debug, Clone)]
40pub struct SshService {
41    context: Arc<AppContext>,
42}
43
44impl SshService {
45    fn new(context: Arc<AppContext>) -> Self {
46        Self { context }
47    }
48}
49
50#[derive(Debug, Clone)]
51pub struct AppState {
52    context: Arc<AppContext>,
53    local: LocalSessionService,
54    ssh: SshService,
55}
56
57impl AppState {
58    pub fn new(config: Config) -> Self {
59        let context = Arc::new(AppContext::new(config));
60        let local = LocalSessionService::new(context.clone());
61        let ssh = SshService::new(context.clone());
62        Self {
63            context,
64            local,
65            ssh,
66        }
67    }
68
69    pub fn config(&self) -> &Config {
70        &self.context.config
71    }
72
73    pub fn ssh_capabilities(&self) -> &SshCapabilityView {
74        &self.context.ssh_capabilities
75    }
76
77    pub fn ssh_capability_probe(&self) -> &crate::ssh::SshCapabilityProbe {
78        &self.context.ssh_capability_probe
79    }
80
81    pub fn ssh_mount_feature_available(&self) -> bool {
82        self.ssh.mount_feature_available()
83    }
84
85    pub fn local(&self) -> &LocalSessionService {
86        &self.local
87    }
88
89    pub fn ssh(&self) -> &SshService {
90        &self.ssh
91    }
92
93    pub async fn shutdown(&self) -> Result<()> {
94        self.ssh.shutdown().await?;
95        self.local.shutdown().await
96    }
97
98    pub fn read_session(
99        &self,
100        session_id: &SessionId,
101        request: &BufferReadRequest,
102    ) -> Result<crate::buffer::BufferReadPage> {
103        self.local.read_session(session_id, request)
104    }
105}