mcp-tools 0.1.0

Rust MCP tools library
Documentation
//! MCP Server implementations

pub mod code_analysis;
pub mod file_operations;
pub mod git_tools;
pub mod system_tools;
pub mod web_tools;

// Re-export server types
pub use code_analysis::CodeAnalysisServer;
pub use file_operations::FileOperationsServer;
pub use git_tools::GitToolsServer;
pub use system_tools::SystemToolsServer;
pub use web_tools::WebToolsServer;

use crate::common::{McpServerBase, ServerConfig};
use crate::{McpToolsError, Result};

/// Server type enumeration
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ServerType {
    FileOperations,
    GitTools,
    CodeAnalysis,
    WebTools,
    SystemTools,
}

impl std::str::FromStr for ServerType {
    type Err = McpToolsError;

    fn from_str(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "file" | "file-operations" | "files" => Ok(Self::FileOperations),
            "git" | "git-tools" => Ok(Self::GitTools),
            "code" | "code-analysis" => Ok(Self::CodeAnalysis),
            "web" | "web-tools" => Ok(Self::WebTools),
            "system" | "system-tools" => Ok(Self::SystemTools),
            _ => Err(McpToolsError::Config(format!("Unknown server type: {}", s))),
        }
    }
}

impl std::fmt::Display for ServerType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::FileOperations => write!(f, "file-operations"),
            Self::GitTools => write!(f, "git-tools"),
            Self::CodeAnalysis => write!(f, "code-analysis"),
            Self::WebTools => write!(f, "web-tools"),
            Self::SystemTools => write!(f, "system-tools"),
        }
    }
}

/// Create a server instance based on type
pub async fn create_server(
    server_type: ServerType,
    config: ServerConfig,
) -> Result<Box<dyn McpServerBase>> {
    match server_type {
        ServerType::FileOperations => {
            let server = FileOperationsServer::new(config).await?;
            Ok(Box::new(server))
        }
        ServerType::GitTools => {
            let server = GitToolsServer::new(config).await?;
            Ok(Box::new(server))
        }
        ServerType::CodeAnalysis => {
            let server = CodeAnalysisServer::new(config).await?;
            Ok(Box::new(server))
        }
        ServerType::WebTools => {
            let server = WebToolsServer::new(config).await?;
            Ok(Box::new(server))
        }
        ServerType::SystemTools => {
            let server = SystemToolsServer::new(config).await?;
            Ok(Box::new(server))
        }
    }
}

/// Get default configuration for a server type
pub fn get_default_config(server_type: ServerType) -> ServerConfig {
    let mut config = ServerConfig::default();

    match server_type {
        ServerType::FileOperations => {
            config.name = "File Operations MCP Server".to_string();
            config.description = "File system operations with security validation".to_string();
            config.port = 3001;
        }
        ServerType::GitTools => {
            config.name = "Git Tools MCP Server".to_string();
            config.description = "Git repository management and analysis".to_string();
            config.port = 3002;
        }
        ServerType::CodeAnalysis => {
            config.name = "Code Analysis MCP Server".to_string();
            config.description = "Language-aware code analysis and refactoring".to_string();
            config.port = 3003;
        }
        ServerType::WebTools => {
            config.name = "Web Tools MCP Server".to_string();
            config.description = "Web scraping and HTTP operations".to_string();
            config.port = 3004;
        }
        ServerType::SystemTools => {
            config.name = "System Tools MCP Server".to_string();
            config.description = "System information and process management".to_string();
            config.port = 3005;
        }
    }

    config
}

/// Get all available server types
pub fn get_available_servers() -> Vec<ServerType> {
    vec![
        ServerType::FileOperations,
        ServerType::GitTools,
        ServerType::CodeAnalysis,
        ServerType::WebTools,
        ServerType::SystemTools,
    ]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_server_type_parsing() {
        assert_eq!(
            "file".parse::<ServerType>().unwrap(),
            ServerType::FileOperations
        );
        assert_eq!(
            "git-tools".parse::<ServerType>().unwrap(),
            ServerType::GitTools
        );
        assert_eq!(
            "code".parse::<ServerType>().unwrap(),
            ServerType::CodeAnalysis
        );
        assert_eq!("web".parse::<ServerType>().unwrap(), ServerType::WebTools);
        assert_eq!(
            "system".parse::<ServerType>().unwrap(),
            ServerType::SystemTools
        );

        assert!("invalid".parse::<ServerType>().is_err());
    }

    #[test]
    fn test_server_type_display() {
        assert_eq!(ServerType::FileOperations.to_string(), "file-operations");
        assert_eq!(ServerType::GitTools.to_string(), "git-tools");
        assert_eq!(ServerType::CodeAnalysis.to_string(), "code-analysis");
        assert_eq!(ServerType::WebTools.to_string(), "web-tools");
        assert_eq!(ServerType::SystemTools.to_string(), "system-tools");
    }

    #[test]
    fn test_default_configs() {
        let file_config = get_default_config(ServerType::FileOperations);
        assert_eq!(file_config.port, 3001);
        assert!(file_config.name.contains("File Operations"));

        let git_config = get_default_config(ServerType::GitTools);
        assert_eq!(git_config.port, 3002);
        assert!(git_config.name.contains("Git Tools"));
    }

    #[test]
    fn test_available_servers() {
        let servers = get_available_servers();
        assert_eq!(servers.len(), 5);
        assert!(servers.contains(&ServerType::FileOperations));
        assert!(servers.contains(&ServerType::GitTools));
    }
}