mcp-tools 0.1.0

Rust MCP tools library
Documentation
//! Tool Execution Example
//!
//! This example demonstrates how to execute tools programmatically
//! using the MCP servers.

use mcp_tools::{
    common::{McpContent, McpServerBase, McpToolRequest, ServerConfig},
    servers::{CodeAnalysisServer, GitToolsServer},
    Result,
};
use std::collections::HashMap;
use uuid::Uuid;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize logging
    tracing_subscriber::fmt().with_env_filter("info").init();

    println!("MCP Tools - Tool Execution Example");

    // Create server configuration
    let config = ServerConfig {
        name: "example-server".to_string(),
        description: "Example server for tool execution".to_string(),
        version: "1.0.0".to_string(),
        host: "127.0.0.1".to_string(),
        port: 8080,
        max_connections: 10,
        request_timeout_secs: 60,
        log_requests: true,
        server_config: HashMap::new(),
    };

    // Example 1: Git Tools
    println!("\n=== Git Tools Example ===");
    let mut git_server = GitToolsServer::new(config.clone()).await?;
    git_server.initialize().await?;

    // Create a git status request
    let mut git_args = HashMap::new();
    git_args.insert(
        "repo_path".to_string(),
        serde_json::Value::String(".".to_string()),
    );

    let git_request = McpToolRequest {
        id: Uuid::new_v4(),
        tool: "git_status".to_string(),
        arguments: serde_json::to_value(git_args)?,
        session_id: "example-session".to_string(),
        metadata: HashMap::new(),
    };

    match git_server.handle_tool_request(git_request).await {
        Ok(response) => {
            println!("Git status response:");
            for content in &response.content {
                match content {
                    McpContent::Text { text } => println!("{}", text),
                    _ => println!("Non-text content received"),
                }
            }
        }
        Err(e) => println!("Git status error: {}", e),
    }

    git_server.shutdown().await?;

    // Example 2: Code Analysis
    println!("\n=== Code Analysis Example ===");
    let mut code_server = CodeAnalysisServer::new(config.clone()).await?;
    code_server.initialize().await?;

    // Create a language detection request
    let mut code_args = HashMap::new();
    code_args.insert(
        "file_path".to_string(),
        serde_json::Value::String("main.rs".to_string()),
    );
    code_args.insert(
        "content".to_string(),
        serde_json::Value::String("fn main() {\n    println!(\"Hello, world!\");\n}".to_string()),
    );

    let code_request = McpToolRequest {
        id: Uuid::new_v4(),
        tool: "detect_language".to_string(),
        arguments: serde_json::to_value(code_args)?,
        session_id: "example-session".to_string(),
        metadata: HashMap::new(),
    };

    match code_server.handle_tool_request(code_request).await {
        Ok(response) => {
            println!("Language detection response:");
            for content in &response.content {
                match content {
                    McpContent::Text { text } => println!("{}", text),
                    _ => println!("Non-text content received"),
                }
            }
        }
        Err(e) => println!("Language detection error: {}", e),
    }

    code_server.shutdown().await?;

    // Example 3: Complexity Analysis
    println!("\n=== Complexity Analysis Example ===");
    let mut analysis_server = CodeAnalysisServer::new(config).await?;
    analysis_server.initialize().await?;

    let mut complexity_args = HashMap::new();
    complexity_args.insert(
        "content".to_string(),
        serde_json::Value::String(
            r#"
        fn fibonacci(n: u32) -> u32 {
            if n <= 1 {
                return n;
            }
            fibonacci(n - 1) + fibonacci(n - 2)
        }
        
        fn main() {
            for i in 0..10 {
                println!("fib({}) = {}", i, fibonacci(i));
            }
        }
        "#
            .to_string(),
        ),
    );
    complexity_args.insert(
        "language".to_string(),
        serde_json::Value::String("rust".to_string()),
    );

    let complexity_request = McpToolRequest {
        id: Uuid::new_v4(),
        tool: "complexity_analysis".to_string(),
        arguments: serde_json::to_value(complexity_args)?,
        session_id: "example-session".to_string(),
        metadata: HashMap::new(),
    };

    match analysis_server
        .handle_tool_request(complexity_request)
        .await
    {
        Ok(response) => {
            println!("Complexity analysis response:");
            for content in &response.content {
                match content {
                    McpContent::Text { text } => println!("{}", text),
                    _ => println!("Non-text content received"),
                }
            }

            if !response.metadata.is_empty() {
                println!("\nMetadata:");
                for (key, value) in &response.metadata {
                    println!("  {}: {}", key, value);
                }
            }
        }
        Err(e) => println!("Complexity analysis error: {}", e),
    }

    analysis_server.shutdown().await?;

    println!("\n=== Example Complete ===");
    println!("This example showed how to:");
    println!("1. Create and initialize MCP servers");
    println!("2. Execute tools programmatically");
    println!("3. Handle responses and metadata");
    println!("4. Properly shutdown servers");

    Ok(())
}