#[cfg(feature = "mcp")]
mod params;
#[cfg(feature = "mcp")]
mod schemas;
#[cfg(feature = "mcp")]
mod tools;
#[cfg(feature = "mcp")]
mod symbols_tools;
#[cfg(feature = "mcp")]
pub use params::*;
#[cfg(feature = "mcp")]
use rmcp::{
ServerHandler,
handler::server::tool::ToolRouter,
handler::server::wrapper::{Json, Parameters},
service::serve_server,
tool, tool_router,
transport::io::stdio,
};
#[cfg(feature = "mcp")]
use crate::types::{FileInfo, SearchResult};
#[cfg(feature = "mcp")]
#[derive(Debug, Clone)]
pub struct CodeSearchMcpService {
tool_router: ToolRouter<Self>,
}
#[cfg(feature = "mcp")]
#[tool_router]
impl CodeSearchMcpService {
pub fn new() -> Self {
Self {
tool_router: Self::tool_router(),
}
}
#[tool(
description = "Search for text patterns in code files with advanced options like fuzzy matching, regex, and filtering"
)]
pub async fn search_code(
&self,
params: Parameters<SearchCodeParams>,
) -> Json<Vec<SearchResult>> {
tools::search_code_tool(params).await
}
#[tool(
description = "List all searchable files in a directory with optional filtering by extensions"
)]
pub async fn list_files(&self, params: Parameters<ListFilesParams>) -> Json<Vec<FileInfo>> {
tools::list_files_tool(params).await
}
#[tool(
description = "Analyze codebase metrics and statistics. Returns JSON with file counts, line counts, and code patterns"
)]
pub async fn analyze_codebase(
&self,
params: Parameters<AnalyzeCodebaseParams>,
) -> Json<serde_json::Value> {
tools::analyze_codebase_tool(params).await
}
#[tool(
description = "Detect code complexity issues. Returns files with high cyclomatic or cognitive complexity"
)]
pub async fn detect_complexity(
&self,
params: Parameters<ComplexityParams>,
) -> Json<serde_json::Value> {
tools::detect_complexity_tool(params).await
}
#[tool(description = "Detect duplicate code blocks. Returns pairs of similar code sections")]
pub async fn detect_duplicates(
&self,
params: Parameters<DuplicatesParams>,
) -> Json<serde_json::Value> {
tools::detect_duplicates_tool(params).await
}
#[tool(description = "Detect dead code including unused functions, variables, and imports")]
pub async fn detect_deadcode(
&self,
params: Parameters<DeadcodeParams>,
) -> Json<serde_json::Value> {
tools::detect_deadcode_tool(params).await
}
#[tool(description = "Detect circular dependencies between modules")]
pub async fn detect_circular(
&self,
params: Parameters<CircularParams>,
) -> Json<serde_json::Value> {
tools::detect_circular_tool(params).await
}
#[tool(
description = "Find symbol definition, references, and callers. Structure-aware search for functions, classes, and identifiers"
)]
pub async fn find_symbol(
&self,
params: Parameters<FindSymbolParams>,
) -> Json<serde_json::Value> {
tools::find_symbol_tool(params).await
}
#[tool(
description = "Get codebase health score (0-100) from dead code, duplicates, and complexity. CI-friendly"
)]
pub async fn get_health(&self, params: Parameters<GetHealthParams>) -> Json<serde_json::Value> {
tools::get_health_tool(params).await
}
#[tool(
description = "Advanced symbol search with context, signatures, and relationships. Search for functions, classes, methods, variables with detailed metadata"
)]
pub async fn search_symbols(
&self,
params: Parameters<symbols_tools::SearchSymbolsParams>,
) -> Json<Vec<crate::symbols::SymbolSearchResult>> {
symbols_tools::search_symbols_tool(params).await
}
#[tool(
description = "Get comprehensive details about a specific symbol including context, documentation, and relationships"
)]
pub async fn get_symbol_details(
&self,
params: Parameters<symbols_tools::GetSymbolDetailsParams>,
) -> Json<serde_json::Value> {
symbols_tools::get_symbol_details_tool(params).await
}
#[tool(
description = "Find relationships between symbols (inheritance, calls, references, etc.). Understand how code elements are connected"
)]
pub async fn find_symbol_relationships(
&self,
params: Parameters<symbols_tools::FindSymbolRelationshipsParams>,
) -> Json<serde_json::Value> {
symbols_tools::find_symbol_relationships_tool(params).await
}
#[tool(
description = "Build or update the symbol index for fast search and analysis. Incremental indexing with automatic change detection"
)]
pub async fn build_symbol_index(
&self,
params: Parameters<symbols_tools::BuildSymbolIndexParams>,
) -> Json<serde_json::Value> {
symbols_tools::build_symbol_index_tool(params).await
}
#[tool(
description = "Get statistics about the symbol index including symbol counts by type and language"
)]
pub async fn get_index_stats(
&self,
params: Parameters<symbols_tools::GetIndexStatsParams>,
) -> Json<serde_json::Value> {
symbols_tools::get_index_stats_tool(params).await
}
#[tool(
description = "Find inheritance and implementation hierarchies for classes, interfaces, and types"
)]
pub async fn find_symbol_hierarchy(
&self,
params: Parameters<symbols_tools::FindSymbolHierarchyParams>,
) -> Json<serde_json::Value> {
symbols_tools::find_symbol_hierarchy_tool(params).await
}
}
#[cfg(feature = "mcp")]
impl ServerHandler for CodeSearchMcpService {
}
#[cfg(feature = "mcp")]
pub async fn start_mcp_server() -> Result<(), Box<dyn std::error::Error>> {
let service = CodeSearchMcpService::new();
let transport = stdio();
serve_server(service, transport).await?;
Ok(())
}