use anyhow::Result;
use clap::Parser;
use rmcp::{ServiceExt, transport::io::stdio};
use std::path::PathBuf;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use torc::mcp_server::server::TorcMcpServer;
#[derive(Parser, Debug)]
#[command(name = "torc-mcp-server")]
#[command(version, about, long_about = None)]
struct Args {
#[arg(
long,
env = "TORC_API_URL",
default_value = "http://localhost:8080/torc-service/v1"
)]
api_url: String,
#[arg(long, env = "TORC_OUTPUT_DIR", default_value = "torc_output")]
output_dir: PathBuf,
#[arg(long, env = "TORC_PASSWORD")]
password: Option<String>,
#[arg(long, env = "TORC_TLS_CA_CERT")]
tls_ca_cert: Option<String>,
#[arg(long, env = "TORC_TLS_INSECURE")]
tls_insecure: bool,
#[arg(long, env = "TORC_DOCS_DIR")]
docs_dir: Option<PathBuf>,
#[arg(long, env = "TORC_EXAMPLES_DIR")]
examples_dir: Option<PathBuf>,
}
fn main() -> Result<()> {
let args = Args::parse();
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "torc_mcp_server=info".into()),
)
.with(tracing_subscriber::fmt::layer().with_writer(std::io::stderr))
.init();
tracing::info!("Starting Torc MCP Server");
tracing::info!("API URL: {}", args.api_url);
tracing::info!("Output directory: {}", args.output_dir.display());
let tls = torc::client::apis::configuration::TlsConfig {
ca_cert_path: args.tls_ca_cert.as_ref().map(std::path::PathBuf::from),
insecure: args.tls_insecure,
};
let server = if args.password.is_some() {
let username = torc::get_username();
TorcMcpServer::with_auth_and_tls(
args.api_url,
args.output_dir,
Some(username),
args.password,
tls,
)
} else {
TorcMcpServer::new_with_tls(args.api_url, args.output_dir, tls)
}
.with_docs_dir(args.docs_dir)
.with_examples_dir(args.examples_dir);
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()?;
runtime.block_on(async_main(server))
}
async fn async_main(server: TorcMcpServer) -> Result<()> {
let service = server.serve(stdio()).await?;
tracing::info!("MCP server running");
service.waiting().await?;
Ok(())
}