Skip to main content

nika_mcp/validation/
mod.rs

1//! MCP Validation Module
2//!
3//! Provides 3-layer validation for MCP tool parameters:
4//!
5//! 1. **Schema Discovery** (schema_cache.rs) - Cache schemas from list_tools()
6//! 2. **Pre-call Validation** (validator.rs) - Validate before calling
7//! 3. **Error Enhancement** (enhancer.rs) - Better error messages
8//!
9//! ## Usage
10//!
11//! ```rust,ignore
12//! use nika_mcp::validation::{ValidationConfig, McpValidator};
13//!
14//! let validator = McpValidator::new(ValidationConfig::default());
15//! validator.cache().populate("novanet", &tools)?;
16//!
17//! let result = validator.validate("novanet", "novanet_context", &params);
18//! if !result.is_valid {
19//!     for error in result.errors {
20//!         eprintln!("{}", error.message);
21//!     }
22//! }
23//! ```
24//!
25//! ## Error Codes
26//!
27//! - NIKA-107: McpValidationFailed - Pre-call validation found errors
28//! - NIKA-108: McpSchemaError - Failed to compile/parse tool schema
29
30pub mod enhancer;
31pub mod schema_cache;
32pub mod validator;
33
34// Re-exports
35pub use enhancer::ErrorEnhancer;
36pub use schema_cache::{CacheStats, CachedSchema, ToolSchemaCache};
37pub use validator::{McpValidator, ValidationError, ValidationErrorKind, ValidationResult};
38
39/// Configuration for MCP parameter validation
40#[derive(Debug, Clone)]
41pub struct ValidationConfig {
42    /// Enable pre-call validation (default: true)
43    pub pre_validate: bool,
44
45    /// Enable error enhancement (default: true)
46    pub enhance_errors: bool,
47
48    /// Max edit distance for "did you mean" suggestions (default: 3)
49    pub suggestion_distance: usize,
50}
51
52impl Default for ValidationConfig {
53    fn default() -> Self {
54        Self {
55            pre_validate: true,
56            enhance_errors: true,
57            suggestion_distance: 3,
58        }
59    }
60}
61
62// ============================================================================
63// TESTS
64// ============================================================================
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_validation_config_default() {
72        let config = ValidationConfig::default();
73        assert!(config.pre_validate);
74        assert!(config.enhance_errors);
75        assert_eq!(config.suggestion_distance, 3);
76    }
77
78    #[test]
79    fn test_validation_config_custom() {
80        let config = ValidationConfig {
81            pre_validate: false,
82            enhance_errors: true,
83            suggestion_distance: 5,
84        };
85        assert!(!config.pre_validate);
86        assert!(config.enhance_errors);
87        assert_eq!(config.suggestion_distance, 5);
88    }
89}