cnctd_service_ssh/lib.rs
1//! cnctd-service-ssh - SSH command execution library
2//!
3//! This library provides secure SSH command execution capabilities,
4//! including interactive shell sessions for LLM agents.
5//!
6//! # Library Usage
7//!
8//! ```rust,no_run
9//! use cnctd_service_ssh::{SshService, SshRegisterArgs, SshExecArgs};
10//!
11//! #[tokio::main]
12//! async fn main() -> anyhow::Result<()> {
13//! let service = SshService::new();
14//!
15//! // Register a target
16//! service.register(SshRegisterArgs {
17//! id: "my-server".to_string(),
18//! host: "example.com".to_string(),
19//! user: "ubuntu".to_string(),
20//! port: 22,
21//! key_passphrase: None,
22//! known_hosts_path: "~/.ssh/known_hosts".to_string(),
23//! client_id: Some("my-app".to_string()),
24//! }).await?;
25//!
26//! // Execute a command
27//! let result = service.exec(SshExecArgs {
28//! id: "my-server".to_string(),
29//! command: "hostname".to_string(),
30//! timeout_secs: 30,
31//! context: Some("checking hostname".to_string()),
32//! }).await?;
33//!
34//! println!("Output: {}", result.stdout);
35//! Ok(())
36//! }
37//! ```
38//!
39//! # Interactive Shell Sessions
40//!
41//! ```rust,no_run
42//! use cnctd_service_ssh::sessions::{ShellSessionService, ShellSessionCreateArgs};
43//!
44//! #[tokio::main]
45//! async fn main() -> anyhow::Result<()> {
46//! let shell_service = ShellSessionService::global();
47//!
48//! // Create an interactive shell session
49//! let result = shell_service.create(ShellSessionCreateArgs {
50//! target_id: "my-server".to_string(),
51//! name: Some("dev-shell".to_string()),
52//! shell: None, // Use default login shell
53//! cols: 120,
54//! rows: 40,
55//! client_id: Some("my-app".to_string()),
56//! env: Default::default(),
57//! }).await?;
58//!
59//! println!("Session created: {}", result.session_id);
60//! Ok(())
61//! }
62//! ```
63//!
64//! # Tool Definitions
65//!
66//! To get tool schemas for LLM integration (avoiding hardcoded schemas):
67//!
68//! ```rust
69//! use cnctd_service_ssh::{get_tool_definitions, get_all_tool_definitions};
70//!
71//! // Get only SSH exec tools
72//! let ssh_tools = get_tool_definitions();
73//!
74//! // Get all tools including interactive shell sessions
75//! let all_tools = get_all_tool_definitions();
76//! ```
77//!
78//! # MCP Server
79//!
80//! Run as a standalone MCP server:
81//! ```bash
82//! cnctd-service-ssh
83//! ```
84
85pub mod operations;
86pub mod service_error;
87pub mod sessions;
88
89#[cfg(feature = "mcp")]
90pub mod mcp;
91
92// Re-export main types for convenience
93pub use operations::{
94 SshRegisterArgs,
95 SshExecArgs,
96 SshUnregisterArgs,
97 SshExecResult,
98 SshService,
99 ToolDefinition,
100 get_tool_definitions,
101 // Legacy global functions for backwards compatibility
102 ssh_register,
103 ssh_exec,
104 ssh_unregister,
105};
106
107pub use service_error::ServiceError;
108
109// Re-export sessions types
110pub use sessions::{
111 ShellSessionService,
112 types::{
113 ShellSessionCreateArgs,
114 ShellSessionWriteArgs,
115 ShellSessionReadArgs,
116 ShellSessionListArgs,
117 ShellSessionReconnectArgs,
118 ShellSessionResizeArgs,
119 ShellSessionCloseArgs,
120 ShellSessionCreateResult,
121 ShellSessionWriteResult,
122 ShellSessionReadResult,
123 ShellSessionListResult,
124 ShellSessionReconnectResult,
125 ShellSessionResizeResult,
126 ShellSessionCloseResult,
127 ShellSessionInfo,
128 ScreenState,
129 SessionState,
130 OutputFormat,
131 },
132 get_shell_session_tool_definitions,
133 // Global functions for MCP server
134 shell_session_create,
135 shell_session_write,
136 shell_session_read,
137 shell_session_list,
138 shell_session_reconnect,
139 shell_session_resize,
140 shell_session_close,
141};
142
143/// Get all tool definitions (SSH exec + interactive sessions)
144pub fn get_all_tool_definitions() -> Vec<ToolDefinition> {
145 let mut tools = get_tool_definitions();
146 tools.extend(get_shell_session_tool_definitions());
147 tools
148}