pulseengine-mcp-protocol 0.17.1

[DEPRECATED] Use rmcp instead. MCP protocol types for PulseEngine.
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
//! Comprehensive unit tests for MCP protocol validation utilities

#[cfg(test)]
mod tests {
    use super::super::validation::*;
    use serde_json::json;
    use std::collections::HashMap;

    #[test]
    fn test_validate_uuid_valid_cases() {
        // Test various valid UUID formats
        let valid_uuids = vec![
            "550e8400-e29b-41d4-a716-446655440000",
            "00000000-0000-0000-0000-000000000000",
            "ffffffff-ffff-ffff-ffff-ffffffffffff",
            "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
            "6ba7b811-9dad-11d1-80b4-00c04fd430c8",
        ];

        for uuid_str in valid_uuids {
            let result = Validator::validate_uuid(uuid_str);
            assert!(result.is_ok(), "UUID '{uuid_str}' should be valid");
            assert_eq!(result.unwrap().to_string(), uuid_str.to_lowercase());
        }
    }

    #[test]
    fn test_validate_uuid_invalid_cases() {
        let invalid_uuids = vec![
            "not-a-uuid",
            "550e8400-e29b-41d4-a716",
            "550e8400-e29b-41d4-a716-446655440000-extra",
            "550e8400_e29b_41d4_a716_446655440000",
            "GGGGGGGG-GGGG-GGGG-GGGG-GGGGGGGGGGGG",
            "",
            "   ",
            "550e8400-e29b-41d4-a716-446655440000xyz", // Extra characters
        ];

        for uuid_str in invalid_uuids {
            let result = Validator::validate_uuid(uuid_str);
            assert!(result.is_err(), "UUID '{uuid_str}' should be invalid");
            assert!(result.unwrap_err().message.contains("Invalid UUID"));
        }
    }

    #[test]
    fn test_validate_non_empty_edge_cases() {
        // Valid cases
        assert!(Validator::validate_non_empty("a", "field").is_ok());
        assert!(Validator::validate_non_empty("multi\nline", "field").is_ok());
        assert!(Validator::validate_non_empty("  text  ", "field").is_ok());
        assert!(Validator::validate_non_empty("πŸŽ‰", "field").is_ok());

        // Invalid cases
        assert!(Validator::validate_non_empty("", "field").is_err());
        assert!(Validator::validate_non_empty(" ", "field").is_err());
        assert!(Validator::validate_non_empty("\t", "field").is_err());
        assert!(Validator::validate_non_empty("\n", "field").is_err());
        assert!(Validator::validate_non_empty("   \t\n  ", "field").is_err());
    }

    #[test]
    fn test_validate_non_empty_error_messages() {
        let result = Validator::validate_non_empty("", "Username");
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().message, "Username cannot be empty");

        let result = Validator::validate_non_empty("   ", "API Key");
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().message, "API Key cannot be empty");
    }

    #[test]
    fn test_validate_tool_name_comprehensive() {
        // Valid tool names
        let valid_names = vec![
            "get_weather",
            "calculate-sum",
            "tool123",
            "UPPERCASE_TOOL",
            "a",
            "tool_with_many_underscores_and_hyphens",
            "123tool",
            "_leading_underscore",
            "-leading-hyphen",
        ];

        for name in valid_names {
            assert!(
                Validator::validate_tool_name(name).is_ok(),
                "Tool name '{name}' should be valid"
            );
        }

        // Invalid tool names
        let invalid_names = vec![
            "",
            "   ",
            "tool name with spaces",
            "tool@name",
            "tool#name",
            "tool$name",
            "tool.name",
            "tool/name",
            "tool\\name",
            "tool:name",
            "tool;name",
            "tool(name)",
            "tool[name]",
            "tool{name}",
            "tool|name",
            "tool+name",
            "tool=name",
            "tool!name",
            "tool?name",
            "tool*name",
            "tool%name",
            "tool&name",
            "tool^name",
            "tool~name",
            "tool`name",
            "tool\"name",
            "tool'name",
            "tool<name>",
            "tool,name",
        ];

        for name in invalid_names {
            assert!(
                Validator::validate_tool_name(name).is_err(),
                "Tool name '{name}' should be invalid"
            );
        }
    }

    #[test]
    fn test_validate_resource_uri_comprehensive() {
        // Valid URIs
        let valid_uris = vec![
            "file:///path/to/file.txt",
            "http://example.com",
            "https://example.com/path?query=value",
            "ftp://server.com/file",
            "custom://protocol/path",
            "/absolute/path",
            "relative/path",
            "../parent/path",
            "path with spaces",
            "unicode/θ·―εΎ„/ζ–‡δ»Ά.txt",
            "emoji/πŸŽ‰/file",
        ];

        for uri in valid_uris {
            assert!(
                Validator::validate_resource_uri(uri).is_ok(),
                "URI '{uri}' should be valid"
            );
        }

        // Invalid URIs
        let invalid_uris = vec![
            "",
            "   ",
            "uri\0with\0null",
            "uri\nwith\nnewline",
            "uri\rwith\rcarriage",
            "uri\twith\ttab",
            "\x01\x02\x03",
        ];

        for uri in invalid_uris {
            assert!(
                Validator::validate_resource_uri(uri).is_err(),
                "URI '{uri}' should be invalid"
            );
        }
    }

    #[test]
    fn test_validate_json_schema_complex() {
        // Valid schemas
        let valid_schemas = vec![
            json!({"type": "object"}),
            json!({"type": "string", "minLength": 1}),
            json!({"type": "number", "minimum": 0, "maximum": 100}),
            json!({"type": "array", "items": {"type": "string"}}),
            json!({
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "number"}
                },
                "required": ["name"]
            }),
            json!({"type": "boolean"}),
            json!({"type": "null"}),
            json!({"type": ["string", "null"]}),
        ];

        for schema in valid_schemas {
            assert!(
                Validator::validate_json_schema(&schema).is_ok(),
                "Schema {schema:?} should be valid"
            );
        }

        // Invalid schemas
        let invalid_schemas = vec![
            json!("not an object"),
            json!(123),
            json!(true),
            json!(null),
            json!([]),
            json!({"properties": {}}), // Missing type
            json!({"minLength": 1}),   // Missing type
        ];

        for schema in invalid_schemas {
            assert!(
                Validator::validate_json_schema(&schema).is_err(),
                "Schema {schema:?} should be invalid"
            );
        }
    }

    #[test]
    fn test_validate_tool_arguments_basic() {
        let schema = json!({
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "age": {"type": "number"},
                "email": {"type": "string"}
            },
            "required": ["name", "age"]
        });

        // Valid arguments
        let mut valid_args = HashMap::new();
        valid_args.insert("name".to_string(), json!("John"));
        valid_args.insert("age".to_string(), json!(30));
        assert!(Validator::validate_tool_arguments(&valid_args, &schema).is_ok());

        // Valid with optional field
        valid_args.insert("email".to_string(), json!("john@example.com"));
        assert!(Validator::validate_tool_arguments(&valid_args, &schema).is_ok());

        // Missing required field
        let mut invalid_args = HashMap::new();
        invalid_args.insert("name".to_string(), json!("John"));
        let result = Validator::validate_tool_arguments(&invalid_args, &schema);
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("age"));

        // Empty arguments with required fields
        let empty_args = HashMap::new();
        let result = Validator::validate_tool_arguments(&empty_args, &schema);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_tool_arguments_no_required() {
        let schema = json!({
            "type": "object",
            "properties": {
                "optional1": {"type": "string"},
                "optional2": {"type": "number"}
            }
        });

        // Empty arguments should be valid when no required fields
        let empty_args = HashMap::new();
        assert!(Validator::validate_tool_arguments(&empty_args, &schema).is_ok());

        // Any combination of optional fields should be valid
        let mut args = HashMap::new();
        args.insert("optional1".to_string(), json!("value"));
        assert!(Validator::validate_tool_arguments(&args, &schema).is_ok());
    }

    #[test]
    fn test_validate_tool_arguments_edge_cases() {
        // Schema without properties
        let schema_no_props = json!({"type": "object"});
        let args = HashMap::new();
        assert!(Validator::validate_tool_arguments(&args, &schema_no_props).is_ok());

        // Non-object schema
        let array_schema = json!({"type": "array"});
        assert!(Validator::validate_tool_arguments(&args, &array_schema).is_ok());

        // Schema with non-array required field
        let invalid_required_schema = json!({
            "type": "object",
            "properties": {"field": {"type": "string"}},
            "required": "not an array"
        });
        assert!(Validator::validate_tool_arguments(&args, &invalid_required_schema).is_ok());

        // Schema with non-string items in required array
        let invalid_items_schema = json!({
            "type": "object",
            "properties": {"field": {"type": "string"}},
            "required": [123, true, null]
        });
        assert!(Validator::validate_tool_arguments(&args, &invalid_items_schema).is_ok());
    }

    #[test]
    fn test_validate_pagination_comprehensive() {
        // Valid cases
        assert!(Validator::validate_pagination(None, None).is_ok());
        assert!(Validator::validate_pagination(Some("cursor123"), None).is_ok());
        assert!(Validator::validate_pagination(None, Some(1)).is_ok());
        assert!(Validator::validate_pagination(None, Some(100)).is_ok());
        assert!(Validator::validate_pagination(None, Some(1000)).is_ok());
        assert!(Validator::validate_pagination(Some("abc"), Some(50)).is_ok());

        // Invalid cursor
        let result = Validator::validate_pagination(Some(""), None);
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("Cursor"));

        let result = Validator::validate_pagination(Some("   "), None);
        assert!(result.is_err());

        // Invalid limit
        let result = Validator::validate_pagination(None, Some(0));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("greater than 0"));

        let result = Validator::validate_pagination(None, Some(1001));
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("cannot exceed 1000"));

        let result = Validator::validate_pagination(None, Some(u32::MAX));
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_prompt_name_comprehensive() {
        // Valid prompt names
        let valid_names = vec![
            "simple_prompt",
            "prompt-with-hyphens",
            "prompt.with.dots",
            "prompt_123",
            "UPPERCASE_PROMPT",
            "mixed.Case-Prompt_123",
            "a",
            "prompt.with.multiple.dots",
            "1234",
            "_",
            "-",
            ".",
        ];

        for name in valid_names {
            assert!(
                Validator::validate_prompt_name(name).is_ok(),
                "Prompt name '{name}' should be valid"
            );
        }

        // Invalid prompt names
        let invalid_names = vec![
            "",
            "   ",
            "prompt with spaces",
            "prompt@name",
            "prompt#name",
            "prompt$name",
            "prompt/name",
            "prompt\\name",
            "prompt:name",
            "prompt;name",
            "prompt(name)",
            "prompt[name]",
            "prompt{name}",
            "prompt|name",
            "prompt+name",
            "prompt=name",
            "prompt!name",
            "prompt?name",
            "prompt*name",
            "prompt%name",
            "prompt&name",
            "prompt^name",
            "prompt~name",
            "prompt`name",
            "prompt\"name",
            "prompt'name",
            "prompt<name>",
            "prompt,name",
        ];

        for name in invalid_names {
            assert!(
                Validator::validate_prompt_name(name).is_err(),
                "Prompt name '{name}' should be invalid"
            );
        }
    }

    #[test]
    fn test_validate_struct_with_validator_crate() {
        use validator::Validate;

        #[derive(Debug, Validate)]
        struct User {
            #[validate(length(min = 1, max = 100))]
            name: String,
            #[validate(email)]
            email: String,
            #[validate(range(min = 0, max = 150))]
            age: u8,
        }

        // Valid struct
        let valid_user = User {
            name: "John Doe".to_string(),
            email: "john@example.com".to_string(),
            age: 30,
        };
        assert!(Validator::validate_struct(&valid_user).is_ok());

        // Invalid email
        let invalid_email_user = User {
            name: "John Doe".to_string(),
            email: "not-an-email".to_string(),
            age: 30,
        };
        let result = Validator::validate_struct(&invalid_email_user);
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("email"));

        // Invalid age
        let invalid_age_user = User {
            name: "John Doe".to_string(),
            email: "john@example.com".to_string(),
            age: 200,
        };
        let result = Validator::validate_struct(&invalid_age_user);
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("age"));

        // Empty name
        let empty_name_user = User {
            name: "".to_string(),
            email: "john@example.com".to_string(),
            age: 30,
        };
        let result = Validator::validate_struct(&empty_name_user);
        assert!(result.is_err());
        assert!(result.unwrap_err().message.contains("name"));
    }

    #[test]
    fn test_validation_error_types() {
        // All validation functions should return validation errors
        let uuid_err = Validator::validate_uuid("invalid").unwrap_err();
        assert_eq!(uuid_err.code, crate::error::ErrorCode::ValidationError);

        let empty_err = Validator::validate_non_empty("", "field").unwrap_err();
        assert_eq!(empty_err.code, crate::error::ErrorCode::ValidationError);

        let tool_err = Validator::validate_tool_name("invalid@name").unwrap_err();
        assert_eq!(tool_err.code, crate::error::ErrorCode::ValidationError);

        let uri_err = Validator::validate_resource_uri("\0null").unwrap_err();
        assert_eq!(uri_err.code, crate::error::ErrorCode::ValidationError);

        let schema_err = Validator::validate_json_schema(&json!("invalid")).unwrap_err();
        assert_eq!(schema_err.code, crate::error::ErrorCode::ValidationError);

        let pagination_err = Validator::validate_pagination(None, Some(0)).unwrap_err();
        assert_eq!(
            pagination_err.code,
            crate::error::ErrorCode::ValidationError
        );

        let prompt_err = Validator::validate_prompt_name("invalid name").unwrap_err();
        assert_eq!(prompt_err.code, crate::error::ErrorCode::ValidationError);
    }

    #[test]
    fn test_unicode_handling() {
        // Unicode in various validators
        assert!(Validator::validate_non_empty("δ½ ε₯½", "field").is_ok());
        assert!(Validator::validate_non_empty("πŸŽ‰πŸŽŠ", "field").is_ok());
        assert!(Validator::validate_non_empty("CafΓ©", "field").is_ok());

        // Tool names actually accept unicode characters that pass is_alphanumeric()
        // Chinese characters are considered alphanumeric by Rust
        assert!(Validator::validate_tool_name("tool_名前").is_ok());
        // But emoji characters are NOT alphanumeric
        assert!(Validator::validate_tool_name("tool_πŸŽ‰").is_err());
        // And special symbols are still rejected
        assert!(Validator::validate_tool_name("tool@name").is_err());
        assert!(Validator::validate_tool_name("tool name").is_err());

        // Resource URIs should accept unicode
        assert!(Validator::validate_resource_uri("file:///θ·―εΎ„/ζ–‡δ»Ά.txt").is_ok());
        assert!(Validator::validate_resource_uri("https://example.com/cafΓ©").is_ok());

        // Prompt names also accept unicode characters that pass is_alphanumeric()
        assert!(Validator::validate_prompt_name("prompt.名前").is_ok());
        // But emoji characters are NOT alphanumeric
        assert!(Validator::validate_prompt_name("prompt.πŸŽ‰").is_err());
        // And special symbols are still rejected
        assert!(Validator::validate_prompt_name("prompt@name").is_err());
        assert!(Validator::validate_prompt_name("prompt name").is_err());
    }

    #[test]
    fn test_large_input_handling() {
        // Test with very long strings
        let long_string = "a".repeat(10000);
        assert!(Validator::validate_non_empty(&long_string, "field").is_ok());
        assert!(Validator::validate_resource_uri(&long_string).is_ok());

        // Very long but valid tool name
        let long_tool_name = "tool_".to_string() + &"a".repeat(1000);
        assert!(Validator::validate_tool_name(&long_tool_name).is_ok());

        // Very long but valid prompt name
        let long_prompt_name = "prompt.".to_string() + &"a".repeat(1000);
        assert!(Validator::validate_prompt_name(&long_prompt_name).is_ok());
    }
}