nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! MCP Validator Module (Layer 2)
//!
//! Pre-call validation of MCP tool parameters against cached schemas.
//!
//! ## Design
//!
//! - Uses cached schemas from ToolSchemaCache
//! - Validates parameters before calling MCP tool
//! - Returns detailed validation errors with suggestions
//!
//! ## Usage
//!
//! ```rust,ignore
//! use nika::mcp::validation::{McpValidator, ValidationConfig};
//!
//! let validator = McpValidator::new(ValidationConfig::default());
//! validator.cache().populate("novanet", &tools)?;
//!
//! let result = validator.validate("novanet", "novanet_context", &params);
//! if !result.is_valid {
//!     for error in result.errors {
//!         println!("{}", error.message);
//!     }
//! }
//! ```

use super::schema_cache::{CachedSchema, ToolSchemaCache};
use super::ValidationConfig;

/// Validation result with detailed errors
#[derive(Debug, Clone)]
pub struct ValidationResult {
    /// Whether validation passed
    pub is_valid: bool,

    /// List of validation errors (empty if valid)
    pub errors: Vec<ValidationError>,
}

/// Single validation error
#[derive(Debug, Clone)]
pub struct ValidationError {
    /// JSON path to the error (e.g., "/entity", "/locale")
    pub path: String,

    /// Error kind
    pub kind: ValidationErrorKind,

    /// Human-readable message
    pub message: String,
}

/// Validation error kinds
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationErrorKind {
    /// Required field is missing
    MissingRequired { field: String },

    /// Field type is wrong
    TypeMismatch { expected: String, actual: String },

    /// Unknown field (not in schema)
    UnknownField {
        field: String,
        suggestions: Vec<String>,
    },

    /// Value doesn't match pattern/format
    InvalidValue { reason: String },

    /// Enum value not in allowed list
    InvalidEnum { value: String, allowed: Vec<String> },
}

/// MCP parameter validator
pub struct McpValidator {
    cache: ToolSchemaCache,
    config: ValidationConfig,
}

impl McpValidator {
    /// Create a new validator with the given config
    pub fn new(config: ValidationConfig) -> Self {
        Self {
            cache: ToolSchemaCache::new(),
            config,
        }
    }

    /// Get reference to schema cache (for populating)
    pub fn cache(&self) -> &ToolSchemaCache {
        &self.cache
    }

    /// Get reference to validation config
    pub fn config(&self) -> &ValidationConfig {
        &self.config
    }

    /// Validate parameters against cached schema
    pub fn validate(
        &self,
        server: &str,
        tool: &str,
        params: &serde_json::Value,
    ) -> ValidationResult {
        // If validation disabled, always pass
        if !self.config.pre_validate {
            return ValidationResult {
                is_valid: true,
                errors: vec![],
            };
        }

        // Get cached schema
        let Some(schema_ref) = self.cache.get(server, tool) else {
            // No schema cached = can't validate, pass through
            tracing::debug!(
                server = %server,
                tool = %tool,
                "No cached schema, skipping validation"
            );
            return ValidationResult {
                is_valid: true,
                errors: vec![],
            };
        };

        let schema = schema_ref.value();
        let mut errors = Vec::new();

        // Run JSON Schema validation
        let validation = schema.validator.iter_errors(params);

        for error in validation {
            let path = error.instance_path.to_string();
            let kind = self.classify_error(&error, schema);
            let message = self.format_error(&error, schema);

            errors.push(ValidationError {
                path,
                kind,
                message,
            });
        }

        ValidationResult {
            is_valid: errors.is_empty(),
            errors,
        }
    }

    /// Classify validation error into a kind
    fn classify_error(
        &self,
        error: &jsonschema::ValidationError,
        schema: &CachedSchema,
    ) -> ValidationErrorKind {
        let error_kind = format!("{:?}", error.kind);
        let error_msg = error.to_string();

        if error_kind.contains("Required") {
            // Extract field name from error message
            let field = self.extract_missing_field(&error_msg);
            ValidationErrorKind::MissingRequired { field }
        } else if error_kind.contains("Type") {
            ValidationErrorKind::TypeMismatch {
                expected: self.extract_expected_type(&error_msg),
                actual: self.extract_actual_type(&error_msg),
            }
        } else if error_kind.contains("AdditionalProperties") {
            // Extract field from path (format: "/field" or "/nested/field")
            let path = error.instance_path.to_string();
            let field = path
                .rsplit('/')
                .next()
                .filter(|s| !s.is_empty())
                .unwrap_or("unknown")
                .to_string();
            let suggestions = self.find_suggestions(&field, &schema.properties);
            ValidationErrorKind::UnknownField { field, suggestions }
        } else if error_kind.contains("Enum") {
            ValidationErrorKind::InvalidEnum {
                value: format!("{}", error.instance),
                allowed: vec![], // Could extract from schema if needed
            }
        } else {
            ValidationErrorKind::InvalidValue { reason: error_msg }
        }
    }

    /// Extract missing field name from error message
    fn extract_missing_field(&self, error_msg: &str) -> String {
        // Pattern: "fieldname" is a required property (double quotes)
        if let Some(start) = error_msg.find('"') {
            if let Some(end) = error_msg[start + 1..].find('"') {
                return error_msg[start + 1..start + 1 + end].to_string();
            }
        }
        // Fallback: try single quotes
        if let Some(start) = error_msg.find('\'') {
            if let Some(end) = error_msg[start + 1..].find('\'') {
                return error_msg[start + 1..start + 1 + end].to_string();
            }
        }
        "unknown".to_string()
    }

    /// Extract expected type from error message
    fn extract_expected_type(&self, error_msg: &str) -> String {
        // Simple extraction - could be improved
        if error_msg.contains("string") {
            "string".to_string()
        } else if error_msg.contains("integer") {
            "integer".to_string()
        } else if error_msg.contains("number") {
            "number".to_string()
        } else if error_msg.contains("boolean") {
            "boolean".to_string()
        } else if error_msg.contains("array") {
            "array".to_string()
        } else if error_msg.contains("object") {
            "object".to_string()
        } else {
            "expected".to_string()
        }
    }

    /// Extract actual type from error message
    fn extract_actual_type(&self, _error_msg: &str) -> String {
        // Would need to inspect the actual value
        "actual".to_string()
    }

    /// Format a human-readable error message
    fn format_error(&self, error: &jsonschema::ValidationError, schema: &CachedSchema) -> String {
        let base = error.to_string();

        // Add suggestions for missing fields
        if !schema.required.is_empty() {
            format!(
                "{}. Required fields: [{}]",
                base,
                schema.required.join(", ")
            )
        } else {
            base
        }
    }

    /// Find similar field names (for "did you mean?")
    pub fn find_suggestions(&self, field: &str, properties: &[String]) -> Vec<String> {
        properties
            .iter()
            .filter(|p| Self::edit_distance(field, p) <= self.config.suggestion_distance)
            .cloned()
            .collect()
    }

    /// Simple Levenshtein distance (case-insensitive)
    pub fn edit_distance(a: &str, b: &str) -> usize {
        let a = a.to_lowercase();
        let b = b.to_lowercase();

        if a.is_empty() {
            return b.len();
        }
        if b.is_empty() {
            return a.len();
        }

        let a_chars: Vec<char> = a.chars().collect();
        let b_chars: Vec<char> = b.chars().collect();

        let mut matrix = vec![vec![0usize; b_chars.len() + 1]; a_chars.len() + 1];

        for (i, row) in matrix.iter_mut().enumerate().take(a_chars.len() + 1) {
            row[0] = i;
        }
        for (j, val) in matrix[0].iter_mut().enumerate() {
            *val = j;
        }

        for i in 1..=a_chars.len() {
            for j in 1..=b_chars.len() {
                let cost = if a_chars[i - 1] == b_chars[j - 1] {
                    0
                } else {
                    1
                };
                matrix[i][j] = std::cmp::min(
                    std::cmp::min(
                        matrix[i - 1][j] + 1, // deletion
                        matrix[i][j - 1] + 1, // insertion
                    ),
                    matrix[i - 1][j - 1] + cost, // substitution
                );
            }
        }

        matrix[a_chars.len()][b_chars.len()]
    }
}

// ============================================================================
// TESTS (TDD)
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mcp::types::ToolDefinition;
    use serde_json::json;

    // ========================================================================
    // Test: Validate missing required field
    // ========================================================================
    #[test]
    fn test_validate_missing_required_field() {
        let validator = McpValidator::new(ValidationConfig::default());
        validator
            .cache()
            .populate(
                "novanet",
                &[
                    ToolDefinition::new("novanet_context").with_input_schema(json!({
                        "type": "object",
                        "properties": {
                            "entity": { "type": "string" },
                            "locale": { "type": "string" }
                        },
                        "required": ["entity"]
                    })),
                ],
            )
            .unwrap();

        // Missing required "entity" field
        let result = validator.validate(
            "novanet",
            "novanet_context",
            &json!({
                "locale": "fr-FR"
            }),
        );

        assert!(!result.is_valid);
        assert_eq!(result.errors.len(), 1);

        // Check that it's a MissingRequired error
        match &result.errors[0].kind {
            ValidationErrorKind::MissingRequired { field } => {
                assert_eq!(field, "entity");
            }
            other => {
                panic!("Expected MissingRequired, got {:?}", other);
            }
        }
    }

    // ========================================================================
    // Test: Valid params passes
    // ========================================================================
    #[test]
    fn test_validate_valid_params_passes() {
        let validator = McpValidator::new(ValidationConfig::default());
        validator
            .cache()
            .populate(
                "novanet",
                &[
                    ToolDefinition::new("novanet_context").with_input_schema(json!({
                        "type": "object",
                        "properties": {
                            "entity": { "type": "string" }
                        },
                        "required": ["entity"]
                    })),
                ],
            )
            .unwrap();

        let result = validator.validate(
            "novanet",
            "novanet_context",
            &json!({
                "entity": "qr-code"
            }),
        );

        assert!(result.is_valid);
        assert!(result.errors.is_empty());
    }

    // ========================================================================
    // Test: Validation disabled always passes
    // ========================================================================
    #[test]
    fn test_validate_disabled_always_passes() {
        let config = ValidationConfig {
            pre_validate: false,
            ..Default::default()
        };
        let validator = McpValidator::new(config);

        // No schema cached, but should pass
        let result = validator.validate("any", "tool", &json!({}));
        assert!(result.is_valid);
    }

    // ========================================================================
    // Test: No cached schema passes
    // ========================================================================
    #[test]
    fn test_validate_no_cached_schema_passes() {
        let validator = McpValidator::new(ValidationConfig::default());

        // No schema cached for this tool
        let result = validator.validate(
            "unknown",
            "tool",
            &json!({
                "anything": "goes"
            }),
        );

        assert!(result.is_valid);
    }

    // ========================================================================
    // Test: Type mismatch
    // ========================================================================
    #[test]
    fn test_validate_type_mismatch() {
        let validator = McpValidator::new(ValidationConfig::default());
        validator
            .cache()
            .populate(
                "s",
                &[ToolDefinition::new("t").with_input_schema(json!({
                    "type": "object",
                    "properties": {
                        "count": { "type": "integer" }
                    }
                }))],
            )
            .unwrap();

        let result = validator.validate(
            "s",
            "t",
            &json!({
                "count": "not-an-integer"
            }),
        );

        assert!(!result.is_valid);
        assert!(matches!(
            &result.errors[0].kind,
            ValidationErrorKind::TypeMismatch { .. }
        ));
    }

    // ========================================================================
    // Test: Edit distance exact match
    // ========================================================================
    #[test]
    fn test_edit_distance_exact_match() {
        assert_eq!(McpValidator::edit_distance("entity", "entity"), 0);
    }

    // ========================================================================
    // Test: Edit distance one char diff
    // ========================================================================
    #[test]
    fn test_edit_distance_one_char_diff() {
        assert_eq!(McpValidator::edit_distance("entity", "entityy"), 1);
        assert_eq!(McpValidator::edit_distance("entty", "entity"), 1);
    }

    // ========================================================================
    // Test: Edit distance case insensitive
    // ========================================================================
    #[test]
    fn test_edit_distance_case_insensitive() {
        assert_eq!(McpValidator::edit_distance("Entity", "ENTITY"), 0);
    }

    // ========================================================================
    // Test: Find suggestions within distance
    // ========================================================================
    #[test]
    fn test_find_suggestions_within_distance() {
        let validator = McpValidator::new(ValidationConfig::default());
        validator
            .cache()
            .populate(
                "s",
                &[ToolDefinition::new("t").with_input_schema(json!({
                    "type": "object",
                    "properties": {
                        "entity": {},
                        "locale": {},
                        "forms": {}
                    }
                }))],
            )
            .unwrap();

        let schema = validator.cache().get("s", "t").unwrap();
        let suggestions = validator.find_suggestions("entiy", &schema.properties);

        assert!(suggestions.contains(&"entity".to_string()));
    }

    // ========================================================================
    // Test: Edit distance empty strings
    // ========================================================================
    #[test]
    fn test_edit_distance_empty_strings() {
        assert_eq!(McpValidator::edit_distance("", ""), 0);
        assert_eq!(McpValidator::edit_distance("abc", ""), 3);
        assert_eq!(McpValidator::edit_distance("", "xyz"), 3);
    }

    // ========================================================================
    // Test: Edit distance completely different
    // ========================================================================
    #[test]
    fn test_edit_distance_completely_different() {
        assert_eq!(McpValidator::edit_distance("abc", "xyz"), 3);
    }

    // ========================================================================
    // Test: Multiple validation errors
    // ========================================================================
    #[test]
    fn test_multiple_validation_errors() {
        let validator = McpValidator::new(ValidationConfig::default());
        validator
            .cache()
            .populate(
                "s",
                &[ToolDefinition::new("t").with_input_schema(json!({
                    "type": "object",
                    "properties": {
                        "a": { "type": "string" },
                        "b": { "type": "integer" }
                    },
                    "required": ["a", "b"]
                }))],
            )
            .unwrap();

        // Missing both required fields
        let result = validator.validate("s", "t", &json!({}));

        assert!(!result.is_valid);
        assert_eq!(result.errors.len(), 2);
    }

    // ========================================================================
    // Test: Error message includes required fields
    // ========================================================================
    #[test]
    fn test_error_message_includes_required_fields() {
        let validator = McpValidator::new(ValidationConfig::default());
        validator
            .cache()
            .populate(
                "s",
                &[ToolDefinition::new("t").with_input_schema(json!({
                    "type": "object",
                    "properties": {
                        "entity": { "type": "string" },
                        "locale": { "type": "string" }
                    },
                    "required": ["entity"]
                }))],
            )
            .unwrap();

        let result = validator.validate("s", "t", &json!({}));

        assert!(!result.is_valid);
        // Message should mention required fields
        assert!(result.errors[0].message.contains("Required fields"));
        assert!(result.errors[0].message.contains("entity"));
    }

    // ========================================================================
    // Test: Suggestion distance config respected
    // ========================================================================
    #[test]
    fn test_suggestion_distance_config() {
        let config = ValidationConfig {
            suggestion_distance: 1,
            ..Default::default()
        };
        let validator = McpValidator::new(config);

        // "entiy" is distance 1 from "entity" - should be suggested
        let suggestions = validator.find_suggestions(
            "entiy",
            &["entity".to_string(), "completely_different".to_string()],
        );
        assert!(suggestions.contains(&"entity".to_string()));
        assert!(!suggestions.contains(&"completely_different".to_string()));
    }
}