Skip to main content

mcp_stdio_proxy/client/
cli.rs

1// MCP-Proxy CLI 实现
2//
3// CLI 主入口,负责命令分发到各个实现模块
4
5use anyhow::{Result, bail};
6
7// 导出 CLI 特定类型
8// 注意: ConvertArgs 等通用参数类型已由 client/mod.rs 统一导出
9pub use crate::client::support::args::{Cli, Commands};
10
11/// 运行 CLI 主逻辑
12///
13/// 根据命令类型分发到对应的实现模块:
14/// - Convert -> cli_impl/convert_cmd.rs
15/// - Check/Detect -> cli_impl/check.rs
16/// - Health -> cli_impl/health.rs
17/// - Proxy -> proxy_server.rs
18pub async fn run_cli(cli: Cli) -> Result<()> {
19    match cli.command {
20        Some(Commands::Convert(args)) => {
21            crate::client::cli_impl::run_convert_command(args, cli.verbose, cli.quiet).await
22        }
23        Some(Commands::Check(args)) => {
24            crate::client::cli_impl::run_check_command(args, cli.verbose, cli.quiet).await
25        }
26        Some(Commands::Detect(args)) => {
27            crate::client::cli_impl::run_detect_command(args, cli.verbose, cli.quiet).await
28        }
29        Some(Commands::Health(args)) => {
30            crate::client::cli_impl::run_health_command(args, cli.quiet).await
31        }
32        Some(Commands::Proxy(args)) => {
33            super::proxy_server::run_proxy_command(args, cli.verbose, cli.quiet).await
34        }
35        None => {
36            // 直接 URL 模式(向后兼容)
37            if let Some(url) = cli.url {
38                let args = crate::client::support::args::ConvertArgs {
39                    url: Some(url),
40                    config: None,
41                    config_file: None,
42                    name: None,
43                    protocol: None,
44                    auth: None,
45                    header: vec![],
46                    retries: 0, // 无限重试
47                    allow_tools: None,
48                    deny_tools: None,
49                    ping_interval: 30, // 默认 30 秒 ping 一次
50                    ping_timeout: 10,  // 默认 10 秒超时
51                    logging: crate::client::support::LoggingArgs {
52                        diagnostic: true,    // 默认启用诊断模式
53                        log_dir: None,       // 默认无日志目录(将在 init_logging 中自动设置)
54                        log_file: None,      // 默认无日志文件
55                        otlp_endpoint: None, // 默认不启用 OTLP 追踪
56                        service_name: "mcp-proxy".to_string(),
57                    },
58                };
59                crate::client::cli_impl::run_convert_command(args, cli.verbose, cli.quiet).await
60            } else {
61                bail!("请提供 URL 或使用子命令")
62            }
63        }
64    }
65}