mcp_tools/lib.rs
1//! # MCP Tools - Unified MCP Tools Crate
2//!
3//! A comprehensive collection of MCP (Model Context Protocol) servers and clients
4//! built on top of the powerful CoderLib library. This crate provides standalone
5//! MCP servers that can be used with any MCP-compatible client (Claude Desktop, etc.)
6//! and clients for testing and development.
7//!
8//! ## Features
9//!
10//! ### MCP Servers
11//! - **File Operations Server** - File system operations with security
12//! - **Git Tools Server** - Git repository management and analysis
13//! - **Code Analysis Server** - Language-aware code analysis and refactoring
14//! - **Web Tools Server** - Web scraping and HTTP operations
15//! - **System Tools Server** - System information and process management
16//!
17//! ### MCP Clients
18//! - **CLI Client** - Command-line MCP client for testing
19//! - **Desktop Client** - GUI client for interactive use
20//!
21//! ## Quick Start
22//!
23//! ### Running a File Operations Server
24//! ```bash
25//! cargo run --bin mcp-file-server -- --port 3000
26//! ```
27//!
28//! ### Using the CLI Client
29//! ```bash
30//! cargo run --bin mcp-cli -- connect ws://localhost:3000
31//! ```
32//!
33//! ## Architecture
34//!
35//! ```text
36//! ┌─────────────────────────────────────────────────────────────┐
37//! │ MCP Tools Crate │
38//! ├─────────────────────────────────────────────────────────────┤
39//! │ Servers │ Clients │ Examples │
40//! │ ├─ file-ops │ ├─ cli │ ├─ basic-client │
41//! │ ├─ git-tools │ ├─ desktop │ ├─ advanced-server │
42//! │ ├─ code-analysis │ └─ ... │ └─ ... │
43//! │ ├─ web-tools │ │ │
44//! │ └─ system-tools │ │ │
45//! ├─────────────────────────────────────────────────────────────┤
46//! │ CoderLib │
47//! │ ┌─ Core ─┐ ┌─ Tools ─┐ ┌─ MCP ─┐ ┌─ Permission ─┐ │
48//! │ │ Session│ │ File │ │ SDK │ │ Security │ │
49//! │ │ LLM │ │ Git │ │ Proto│ │ Validation │ │
50//! │ │ Storage│ │ Code │ │ Tools│ │ Session Mgmt │ │
51//! │ └────────┘ └────────┘ └──────┘ └──────────────┘ │
52//! └─────────────────────────────────────────────────────────────┘
53//! ```
54
55pub mod clients;
56pub mod common;
57pub mod config;
58pub mod servers;
59pub mod utils;
60
61// TODO: Re-export core types for convenience when coderlib is available
62// pub use coderlib::{
63// CoderLib, CoderLibConfig, Session, Tool, ToolResponse, ToolError,
64// Permission, PermissionService, PermissionRequest, PermissionResponse,
65// Provider, Model, Storage, Message
66// };
67
68// Re-export RMCP types for MCP protocol
69pub use rmcp::{model::*, Error as McpError};
70
71// Common types used across servers and clients
72pub use common::*;
73
74/// MCP Tools version
75pub const VERSION: &str = env!("CARGO_PKG_VERSION");
76
77/// Default MCP server configuration
78pub const DEFAULT_HOST: &str = "127.0.0.1";
79pub const DEFAULT_PORT: u16 = 3000;
80
81/// Error types for MCP Tools
82#[derive(Debug, thiserror::Error)]
83pub enum McpToolsError {
84 #[error("CoderLib error: {0}")]
85 CoderLib(String),
86
87 #[error("MCP SDK error: {0}")]
88 McpSdk(String),
89
90 #[error("Configuration error: {0}")]
91 Config(String),
92
93 #[error("Initialization error: {0}")]
94 InitializationError(String),
95
96 #[error("IO error: {0}")]
97 Io(#[from] std::io::Error),
98
99 #[error("Serialization error: {0}")]
100 Serialization(#[from] serde_json::Error),
101
102 #[error("Git error: {0}")]
103 Git(#[from] git2::Error),
104
105 #[error("Network error: {0}")]
106 Network(#[from] reqwest::Error),
107
108 #[error("Server error: {0}")]
109 Server(String),
110
111 #[error("Client error: {0}")]
112 Client(String),
113}
114
115/// Result type for MCP Tools operations
116pub type Result<T> = std::result::Result<T, McpToolsError>;
117
118/// Initialize logging for MCP Tools
119pub fn init_logging() -> Result<()> {
120 tracing_subscriber::fmt()
121 .with_env_filter(
122 tracing_subscriber::EnvFilter::try_from_default_env()
123 .unwrap_or_else(|_| "mcp_tools=info,coderlib=info".into()),
124 )
125 .init();
126 Ok(())
127}
128
129/// Get MCP Tools information
130pub fn info() -> McpToolsInfo {
131 McpToolsInfo {
132 name: "MCP Tools".to_string(),
133 version: VERSION.to_string(),
134 description: "Unified MCP Tools Crate powered by CoderLib".to_string(),
135 coderlib_version: "0.1.0".to_string(), // TODO: Get from coderlib when available
136 features: get_enabled_features(),
137 }
138}
139
140/// Information about MCP Tools
141#[derive(Debug, serde::Serialize, serde::Deserialize)]
142pub struct McpToolsInfo {
143 pub name: String,
144 pub version: String,
145 pub description: String,
146 pub coderlib_version: String,
147 pub features: Vec<String>,
148}
149
150/// Get list of enabled features
151fn get_enabled_features() -> Vec<String> {
152 let mut features = Vec::new();
153
154 #[cfg(feature = "file-server")]
155 features.push("file-server".to_string());
156
157 #[cfg(feature = "git-server")]
158 features.push("git-server".to_string());
159
160 #[cfg(feature = "code-server")]
161 features.push("code-server".to_string());
162
163 #[cfg(feature = "web-server")]
164 features.push("web-server".to_string());
165
166 #[cfg(feature = "system-server")]
167 features.push("system-server".to_string());
168
169 #[cfg(feature = "cli-client")]
170 features.push("cli-client".to_string());
171
172 #[cfg(feature = "desktop-client")]
173 features.push("desktop-client".to_string());
174
175 features
176}
177
178#[cfg(test)]
179mod tests {
180 use super::*;
181
182 #[test]
183 fn test_version() {
184 assert!(!VERSION.is_empty());
185 }
186
187 #[test]
188 fn test_info() {
189 let info = info();
190 assert_eq!(info.name, "MCP Tools");
191 assert!(!info.version.is_empty());
192 assert!(!info.description.is_empty());
193 }
194
195 #[test]
196 fn test_features() {
197 let features = get_enabled_features();
198 // Should have at least one feature enabled in tests
199 assert!(!features.is_empty());
200 }
201}