codetree 0.1.1

AI-native version control system for autonomous agents
Documentation
//! CodeTree MCP Server
//!
//! Model Context Protocol server for CodeTree
//! Allows AI agents to interact with CodeTree's VCS automation capabilities

use codetree::error::{Context, Result};
use codetree::{mcp::CodetreeServer, BranchManager, Config, VcsManager};
use clap::Parser;
use rmcp::service::ServiceExt;
use std::path::PathBuf;
use std::sync::Arc;

#[derive(Parser, Debug)]
#[command(name = "codetree-mcp")]
#[command(about = "CodeTree Model Context Protocol (MCP) Server")]
#[command(version)]
#[command(author = "Ying Kit WONG")]
struct Cli {
    /// Path to codetree directory
    #[arg(long, default_value = ".codetree")]
    codetree_dir: String,

    /// Enable verbose logging
    #[arg(short, long)]
    verbose: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    if cli.verbose {
        eprintln!("[DEBUG] CodeTree MCP Server v{} starting...", env!("CARGO_PKG_VERSION"));
    }

    // Get current directory
    let current_dir = std::env::current_dir()
        .context("Failed to get current directory")?;

    // Load configuration
    let codetree_dir = if std::path::Path::new(&cli.codetree_dir).is_absolute() {
        PathBuf::from(&cli.codetree_dir)
    } else {
        current_dir.join(&cli.codetree_dir)
    };

    let config_path = codetree_dir.join("config.toml");
    let codetree_config = if config_path.exists() {
        Config::load(&config_path).await?
    } else {
        eprintln!("[WARN]  Config file not found, using defaults");
        Config::default()
    };

    // Initialize managers
    let branches_path = codetree_dir.join("branches.json");
    let branch_manager = Arc::new(tokio::sync::RwLock::new(
        BranchManager::new(branches_path).await?,
    ));

    let git_manager = Arc::new(VcsManager::new(current_dir.clone())?);
    // Create MCP server handler
    let server_handler = CodetreeServer::new(
        branch_manager,
        git_manager,
        codetree_config,
    );

    // Create stdio transport
    let (stdin, stdout) = (tokio::io::stdin(), tokio::io::stdout());
    let transport = (stdin, stdout);

    if cli.verbose {
        eprintln!("[DEBUG] Server initialized, listening for requests on stdio...");
    }

    // Start MCP server
    let server = server_handler.serve(transport).await?;

    if cli.verbose {
        eprintln!("[DEBUG] Server handshake complete. Waiting for requests...");
    }

    // Wait for server shutdown
    let shutdown_reason = server.waiting().await?;

    if cli.verbose {
        eprintln!("[DEBUG] Server shutting down: {:?}", shutdown_reason);
    }

    Ok(())
}