use clap::Parser;
use log::{debug, error, info, LevelFilter};
use mcp_attr::server::serve_stdio;
use mcp_gmailcal::{cli::{Cli, Commands}, oauth, setup_logging, GmailServer};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env::set_var("RUST_LOG", "debug");
let cli = Cli::parse();
let is_read_only = std::env::var("CLAUDE_DESKTOP").is_ok()
|| std::env::var("CLAUDE_AI").is_ok()
|| cli.memory_only;
if is_read_only {
env::set_var("MCP_READ_ONLY", "1");
println!("Running in read-only mode with in-memory logging");
}
match cli.command {
Some(Commands::Auth) => {
println!("Starting OAuth authentication flow...");
if let Err(e) = oauth::run_oauth_flow().await {
eprintln!("Authentication failed: {}", e);
std::process::exit(1);
}
return Ok(());
}
Some(Commands::Test) => {
println!("Testing Gmail credentials...");
match oauth::test_credentials().await {
Ok(result) => {
println!("{}\n", result);
println!("✅ Credentials are valid and working!");
}
Err(e) => {
eprintln!("❌ Credential test failed: {}", e);
eprintln!("\nRun 'cargo run -- auth' to refresh your credentials.");
std::process::exit(1);
}
}
return Ok(());
}
Some(Commands::Server) | None => {
}
}
let log_file = if is_read_only {
setup_logging(LevelFilter::Debug, Some("memory"))?
} else {
setup_logging(LevelFilter::Trace, None)?
};
info!("Gmail MCP Server starting...");
info!("Logs will be saved to {}", log_file);
debug!("Debug logging enabled");
debug!("Creating GmailServer instance");
let server = GmailServer::new();
info!("Starting MCP server with stdio interface");
let result = serve_stdio(server).await;
if let Err(ref e) = result {
error!("Error running MCP server: {}", e);
} else {
info!("MCP server completed successfully");
}
debug!("Exiting application");
result.map_err(|e| e.into())
}