turbomcp_cli/
cli.rs

1//! CLI argument parsing and configuration types - Enhanced version
2
3use clap::{Args, Parser, Subcommand, ValueEnum};
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6
7/// Main CLI application structure
8#[derive(Parser, Debug)]
9#[command(
10    name = "turbomcp-cli",
11    version,
12    about = "Comprehensive CLI for MCP servers - complete protocol support with rich UX",
13    long_about = "TurboMCP CLI provides comprehensive access to MCP (Model Context Protocol) servers.\n\
14                  Supports all MCP operations: tools, resources, prompts, completions, sampling, and more.\n\
15                  Multiple transports: stdio, HTTP SSE, WebSocket, TCP, Unix sockets."
16)]
17pub struct Cli {
18    /// Subcommand to run
19    #[command(subcommand)]
20    pub command: Commands,
21
22    /// Output format
23    #[arg(long, short = 'f', global = true, value_enum, default_value = "human")]
24    pub format: OutputFormat,
25
26    /// Enable verbose output
27    #[arg(long, short = 'v', global = true)]
28    pub verbose: bool,
29
30    /// Connection config name (from ~/.turbomcp/config.yaml)
31    #[arg(long, short = 'c', global = true)]
32    pub connection: Option<String>,
33
34    /// Disable colored output
35    #[arg(long, global = true)]
36    pub no_color: bool,
37}
38
39/// Available CLI subcommands - Complete MCP coverage
40#[derive(Subcommand, Debug)]
41pub enum Commands {
42    /// Tool operations
43    #[command(subcommand)]
44    Tools(ToolCommands),
45
46    /// Resource operations
47    #[command(subcommand)]
48    Resources(ResourceCommands),
49
50    /// Prompt operations
51    #[command(subcommand)]
52    Prompts(PromptCommands),
53
54    /// Completion operations
55    #[command(subcommand)]
56    Complete(CompletionCommands),
57
58    /// Server management
59    #[command(subcommand)]
60    Server(ServerCommands),
61
62    /// Sampling operations (advanced)
63    #[command(subcommand)]
64    Sample(SamplingCommands),
65
66    /// Interactive connection wizard
67    Connect(Connection),
68
69    /// Connection status
70    Status(Connection),
71}
72
73/// Tool-related commands
74#[derive(Subcommand, Debug)]
75pub enum ToolCommands {
76    /// List available tools
77    List {
78        #[command(flatten)]
79        conn: Connection,
80    },
81
82    /// Call a tool
83    Call {
84        #[command(flatten)]
85        conn: Connection,
86
87        /// Tool name
88        name: String,
89
90        /// Arguments as JSON object
91        #[arg(long, short = 'a', default_value = "{}")]
92        arguments: String,
93    },
94
95    /// Get tool schema
96    Schema {
97        #[command(flatten)]
98        conn: Connection,
99
100        /// Tool name (omit to get all schemas)
101        name: Option<String>,
102    },
103
104    /// Export all tool schemas
105    Export {
106        #[command(flatten)]
107        conn: Connection,
108
109        /// Output directory
110        #[arg(long, short = 'o')]
111        output: PathBuf,
112    },
113}
114
115/// Resource-related commands
116#[derive(Subcommand, Debug)]
117pub enum ResourceCommands {
118    /// List resources
119    List {
120        #[command(flatten)]
121        conn: Connection,
122    },
123
124    /// Read resource content
125    Read {
126        #[command(flatten)]
127        conn: Connection,
128
129        /// Resource URI
130        uri: String,
131    },
132
133    /// List resource templates
134    Templates {
135        #[command(flatten)]
136        conn: Connection,
137    },
138
139    /// Subscribe to resource updates
140    Subscribe {
141        #[command(flatten)]
142        conn: Connection,
143
144        /// Resource URI
145        uri: String,
146    },
147
148    /// Unsubscribe from resource updates
149    Unsubscribe {
150        #[command(flatten)]
151        conn: Connection,
152
153        /// Resource URI
154        uri: String,
155    },
156}
157
158/// Prompt-related commands
159#[derive(Subcommand, Debug)]
160pub enum PromptCommands {
161    /// List prompts
162    List {
163        #[command(flatten)]
164        conn: Connection,
165    },
166
167    /// Get prompt with arguments
168    Get {
169        #[command(flatten)]
170        conn: Connection,
171
172        /// Prompt name
173        name: String,
174
175        /// Arguments as JSON object
176        #[arg(long, short = 'a', default_value = "{}")]
177        arguments: String,
178    },
179
180    /// Get prompt schema
181    Schema {
182        #[command(flatten)]
183        conn: Connection,
184
185        /// Prompt name
186        name: String,
187    },
188}
189
190/// Completion commands
191#[derive(Subcommand, Debug)]
192pub enum CompletionCommands {
193    /// Get completions for a reference
194    Get {
195        #[command(flatten)]
196        conn: Connection,
197
198        /// Reference type (prompt, resource, etc.)
199        #[arg(value_enum)]
200        ref_type: RefType,
201
202        /// Reference value
203        ref_value: String,
204
205        /// Argument name (for prompt arguments)
206        #[arg(long)]
207        argument: Option<String>,
208    },
209}
210
211/// Server management commands
212#[derive(Subcommand, Debug)]
213pub enum ServerCommands {
214    /// Get server info
215    Info {
216        #[command(flatten)]
217        conn: Connection,
218    },
219
220    /// Ping server
221    Ping {
222        #[command(flatten)]
223        conn: Connection,
224    },
225
226    /// Set server log level
227    LogLevel {
228        #[command(flatten)]
229        conn: Connection,
230
231        /// Log level
232        #[arg(value_enum)]
233        level: LogLevel,
234    },
235
236    /// List roots
237    Roots {
238        #[command(flatten)]
239        conn: Connection,
240    },
241}
242
243/// Sampling commands (advanced)
244#[derive(Subcommand, Debug)]
245pub enum SamplingCommands {
246    /// Create a message sample
247    Create {
248        #[command(flatten)]
249        conn: Connection,
250
251        /// Messages as JSON array
252        messages: String,
253
254        /// Model preferences
255        #[arg(long)]
256        model_preferences: Option<String>,
257
258        /// System prompt
259        #[arg(long)]
260        system_prompt: Option<String>,
261
262        /// Max tokens
263        #[arg(long)]
264        max_tokens: Option<u32>,
265    },
266}
267
268/// Connection configuration
269#[derive(Args, Debug, Clone, Serialize, Deserialize)]
270pub struct Connection {
271    /// Transport protocol (auto-detected if not specified)
272    #[arg(long, value_enum)]
273    pub transport: Option<TransportKind>,
274
275    /// Server URL or command
276    #[arg(long, env = "MCP_URL", default_value = "http://localhost:8080/mcp")]
277    pub url: String,
278
279    /// Command for stdio transport (overrides --url)
280    #[arg(long, env = "MCP_COMMAND")]
281    pub command: Option<String>,
282
283    /// Bearer token or API key
284    #[arg(long, env = "MCP_AUTH")]
285    pub auth: Option<String>,
286
287    /// Connection timeout in seconds
288    #[arg(long, default_value = "30")]
289    pub timeout: u64,
290}
291
292/// Transport types - Extended
293#[derive(Debug, Clone, ValueEnum, PartialEq, Eq, Serialize, Deserialize)]
294#[serde(rename_all = "lowercase")]
295pub enum TransportKind {
296    /// Standard input/output
297    Stdio,
298    /// HTTP with Server-Sent Events
299    Http,
300    /// WebSocket
301    Ws,
302    /// TCP socket
303    Tcp,
304    /// Unix domain socket
305    Unix,
306}
307
308/// Output formats
309#[derive(Debug, Clone, ValueEnum, PartialEq, Eq)]
310pub enum OutputFormat {
311    /// Human-readable with colors
312    Human,
313    /// JSON output
314    Json,
315    /// YAML output
316    Yaml,
317    /// Table format
318    Table,
319    /// Compact JSON (no pretty print)
320    Compact,
321}
322
323/// Reference types for completions
324#[derive(Debug, Clone, ValueEnum, PartialEq, Eq)]
325pub enum RefType {
326    /// Prompt reference
327    Prompt,
328    /// Resource reference
329    Resource,
330}
331
332/// Log levels
333#[derive(Debug, Clone, ValueEnum, PartialEq, Eq)]
334pub enum LogLevel {
335    Debug,
336    Info,
337    Warning,
338    Error,
339}
340
341impl From<LogLevel> for turbomcp_protocol::types::LogLevel {
342    fn from(level: LogLevel) -> Self {
343        match level {
344            LogLevel::Debug => turbomcp_protocol::types::LogLevel::Debug,
345            LogLevel::Info => turbomcp_protocol::types::LogLevel::Info,
346            LogLevel::Warning => turbomcp_protocol::types::LogLevel::Warning,
347            LogLevel::Error => turbomcp_protocol::types::LogLevel::Error,
348        }
349    }
350}