|
| 1 | +//! Basic Router Test - demonstrates BasicToolsRouter and router combination |
| 2 | +//! |
| 3 | +//! This tests the Week 1 implementation of BasicToolsRouter with 3 core tools |
| 4 | +//! and demonstrates the router combination pattern for future expansion. |
| 5 | +
|
| 6 | +use codeprism_mcp::{tools::basic::BasicToolsRouter, CodePrismMcpServer}; |
| 7 | +use rmcp::{ |
| 8 | + handler::server::router::tool::ToolRouter, model::*, tool_handler, Error as McpError, |
| 9 | + ServerHandler, |
| 10 | +}; |
| 11 | +use std::sync::Arc; |
| 12 | +use tokio::sync::RwLock; |
| 13 | + |
| 14 | +/// Combined server demonstrating modular router architecture |
| 15 | +#[derive(Clone)] |
| 16 | +pub struct CombinedTestServer { |
| 17 | + core_server: Arc<RwLock<CodePrismMcpServer>>, |
| 18 | + combined_router: ToolRouter<CombinedTestServer>, |
| 19 | +} |
| 20 | + |
| 21 | +impl CombinedTestServer { |
| 22 | + pub fn new() -> Result<Self, McpError> { |
| 23 | + let core_server = Arc::new(RwLock::new( |
| 24 | + CodePrismMcpServer::new().map_err(|e| McpError::internal_error(e.to_string()))?, |
| 25 | + )); |
| 26 | + |
| 27 | + // Create category routers - currently just BasicToolsRouter |
| 28 | + // In Week 2, we'll add: + AnalysisToolsRouter::analysis_tools_router() |
| 29 | + let combined_router = BasicToolsRouter::basic_tools_router(); |
| 30 | + |
| 31 | + Ok(Self { |
| 32 | + core_server, |
| 33 | + combined_router, |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + pub async fn initialize_repository<P: AsRef<std::path::Path>>( |
| 38 | + &mut self, |
| 39 | + path: P, |
| 40 | + ) -> Result<(), McpError> { |
| 41 | + let path = path.as_ref().to_path_buf(); |
| 42 | + { |
| 43 | + let mut server = self.core_server.write().await; |
| 44 | + server |
| 45 | + .initialize_with_repository(&path) |
| 46 | + .await |
| 47 | + .map_err(|e| McpError::internal_error(e.to_string()))?; |
| 48 | + } |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[tool_handler] |
| 54 | +impl ServerHandler for CombinedTestServer { |
| 55 | + fn get_info(&self) -> ServerInfo { |
| 56 | + ServerInfo { |
| 57 | + protocol_version: ProtocolVersion::V_2024_11_05, |
| 58 | + capabilities: ServerCapabilities::builder().enable_tools().build(), |
| 59 | + server_info: Implementation::from_build_env(), |
| 60 | + instructions: Some("CodePrism MCP Server - Week 1 BasicToolsRouter Test - modular architecture with category-based routers".to_string()), |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#[tokio::main] |
| 66 | +async fn main() -> anyhow::Result<()> { |
| 67 | + println!("🧪 Testing Week 1: BasicToolsRouter + Router Combination"); |
| 68 | + |
| 69 | + // Create combined server with BasicToolsRouter |
| 70 | + let mut server = CombinedTestServer::new()?; |
| 71 | + println!("✅ Combined server with BasicToolsRouter created"); |
| 72 | + |
| 73 | + // Test repository initialization (optional) |
| 74 | + if let Ok(current_dir) = std::env::current_dir() { |
| 75 | + if let Err(e) = server.initialize_repository(¤t_dir).await { |
| 76 | + println!("⚠️ Repository initialization failed: {}", e); |
| 77 | + println!(" Continuing with tests (some tools may show 'no repository loaded')"); |
| 78 | + } else { |
| 79 | + println!("✅ Repository initialized: {}", current_dir.display()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Test that we can access the BasicToolsRouter through the combined router |
| 84 | + let basic_router = BasicToolsRouter::new(server.core_server.clone()); |
| 85 | + |
| 86 | + // Test 1: repository_stats |
| 87 | + println!("\n🔧 Testing repository_stats tool:"); |
| 88 | + match basic_router.repository_stats().await { |
| 89 | + Ok(result) => { |
| 90 | + println!("✅ repository_stats successful"); |
| 91 | + if let Some(content) = result.content.first() { |
| 92 | + println!(" Result: {}", content.text().unwrap_or("No text content")); |
| 93 | + } |
| 94 | + } |
| 95 | + Err(e) => println!("❌ repository_stats failed: {}", e), |
| 96 | + } |
| 97 | + |
| 98 | + // Test 2: search_symbols |
| 99 | + println!("\n🔍 Testing search_symbols tool:"); |
| 100 | + let search_args = serde_json::json!({ |
| 101 | + "pattern": "test" |
| 102 | + }); |
| 103 | + match basic_router |
| 104 | + .search_symbols(rmcp::handler::server::tool::Parameters(search_args)) |
| 105 | + .await |
| 106 | + { |
| 107 | + Ok(result) => { |
| 108 | + println!("✅ search_symbols successful"); |
| 109 | + if let Some(content) = result.content.first() { |
| 110 | + println!(" Result: {}", content.text().unwrap_or("No text content")); |
| 111 | + } |
| 112 | + } |
| 113 | + Err(e) => println!("❌ search_symbols failed: {}", e), |
| 114 | + } |
| 115 | + |
| 116 | + // Test 3: find_files |
| 117 | + println!("\n📁 Testing find_files tool:"); |
| 118 | + let files_args = serde_json::json!({ |
| 119 | + "pattern": "*.rs" |
| 120 | + }); |
| 121 | + match basic_router |
| 122 | + .find_files(rmcp::handler::server::tool::Parameters(files_args)) |
| 123 | + .await |
| 124 | + { |
| 125 | + Ok(result) => { |
| 126 | + println!("✅ find_files successful"); |
| 127 | + if let Some(content) = result.content.first() { |
| 128 | + println!( |
| 129 | + " Result preview: {}...", |
| 130 | + content |
| 131 | + .text() |
| 132 | + .unwrap_or("No text content") |
| 133 | + .chars() |
| 134 | + .take(200) |
| 135 | + .collect::<String>() |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | + Err(e) => println!("❌ find_files failed: {}", e), |
| 140 | + } |
| 141 | + |
| 142 | + println!("\n🎉 Week 1 BasicToolsRouter test completed!"); |
| 143 | + println!("✅ 3 core tools implemented: repository_stats, search_symbols, find_files"); |
| 144 | + println!("✅ RMCP router combination pattern established"); |
| 145 | + println!("✅ Foundation ready for Week 2: Core Category Migration"); |
| 146 | + |
| 147 | + Ok(()) |
| 148 | +} |
0 commit comments