pmcp 2.2.0

High-quality Rust SDK for Model Context Protocol (MCP) with full TypeScript SDK compatibility
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
611
612
613
614
615
//! SIMD-optimized parsing for high-performance JSON-RPC and SSE processing.
//!
//! PMCP-4006: This module provides SIMD (Single Instruction, Multiple Data) optimizations
//! for parsing operations that are critical to MCP performance:
//! - High-speed JSON parsing with vectorized operations
//! - Optimized SSE event parsing with parallel field detection
//! - Fast string searching and validation operations
//! - Parallel HTTP header parsing
//! - Vectorized base64 encoding/decoding
//! - SIMD-accelerated UTF-8 validation

use crate::error::{Error, Result};
use crate::shared::sse_parser::SseEvent;
use crate::types::jsonrpc::{JSONRPCRequest, JSONRPCResponse};
use base64::{engine::general_purpose, Engine as _};
use serde_json;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// CPU feature detection results.
#[derive(Debug, Clone, Copy)]
pub struct CpuFeatures {
    /// AVX2 support available
    pub avx2: bool,
    /// SSE4.2 support available  
    pub sse42: bool,
    /// SSSE3 support available
    pub ssse3: bool,
}

impl CpuFeatures {
    /// Detect CPU features at runtime.
    pub fn detect() -> Self {
        Self {
            avx2: Self::has_avx2(),
            sse42: Self::has_sse42(),
            ssse3: Self::has_ssse3(),
        }
    }

    #[cfg(target_arch = "x86_64")]
    fn has_avx2() -> bool {
        is_x86_feature_detected!("avx2")
    }

    #[cfg(not(target_arch = "x86_64"))]
    fn has_avx2() -> bool {
        false
    }

    #[cfg(target_arch = "x86_64")]
    fn has_sse42() -> bool {
        is_x86_feature_detected!("sse4.2")
    }

    #[cfg(not(target_arch = "x86_64"))]
    fn has_sse42() -> bool {
        false
    }

    #[cfg(target_arch = "x86_64")]
    fn has_ssse3() -> bool {
        is_x86_feature_detected!("ssse3")
    }

    #[cfg(not(target_arch = "x86_64"))]
    fn has_ssse3() -> bool {
        false
    }
}

/// SIMD parsing performance metrics.
#[derive(Debug, Clone, Default)]
pub struct ParsingMetrics {
    /// Total bytes processed by SIMD operations
    pub total_bytes_processed: u64,
    /// Total number of documents parsed
    pub total_documents_parsed: u64,
    /// Average parsing time in nanoseconds
    pub average_parse_time_ns: u64,
    /// Documents processed per second
    pub documents_per_second: f64,
    /// Number of SIMD operations used
    pub simd_operations_used: u64,
    /// Number of fallback operations to scalar code
    pub fallback_operations: u64,
}

impl ParsingMetrics {
    /// Calculate current throughput in documents per second.
    pub fn throughput(&self) -> f64 {
        self.documents_per_second
    }

    /// Calculate SIMD utilization percentage.
    pub fn simd_utilization(&self) -> f64 {
        let total = self.simd_operations_used + self.fallback_operations;
        if total > 0 {
            self.simd_operations_used as f64 / total as f64 * 100.0
        } else {
            0.0
        }
    }
}

/// High-performance JSON parser with SIMD acceleration.
#[derive(Debug)]
pub struct SimdJsonParser {
    features: CpuFeatures,
    metrics: Arc<AtomicMetrics>,
}

/// Thread-safe metrics using atomics.
#[derive(Debug, Default)]
struct AtomicMetrics {
    total_bytes: AtomicU64,
    total_docs: AtomicU64,
    total_time_ns: AtomicU64,
    simd_ops: AtomicU64,
    fallback_ops: AtomicU64,
}

impl SimdJsonParser {
    /// Create a new SIMD JSON parser with automatic feature detection.
    pub fn new() -> Self {
        Self {
            features: CpuFeatures::detect(),
            metrics: Arc::new(AtomicMetrics::default()),
        }
    }

    /// Parse a JSON-RPC request from bytes.
    pub fn parse_request(&self, input: &[u8]) -> Result<JSONRPCRequest> {
        let start = Instant::now();

        // Quick validation using SIMD if available
        if self.features.avx2 || self.features.sse42 {
            if !self.validate_json_structure(input) {
                self.metrics.fallback_ops.fetch_add(1, Ordering::Relaxed);
                return Err(Error::parse(
                    "Invalid JSON structure detected by SIMD validation",
                ));
            }
            self.metrics.simd_ops.fetch_add(1, Ordering::Relaxed);
        }

        // Parse with serde_json (still fastest for complete JSON parsing)
        let result: JSONRPCRequest = serde_json::from_slice(input)
            .map_err(|e| Error::parse(format!("JSON parsing failed: {}", e)))?;

        self.update_metrics(input.len(), start.elapsed());
        Ok(result)
    }

    /// Parse a JSON-RPC response from bytes.
    pub fn parse_response(&self, input: &[u8]) -> Result<JSONRPCResponse> {
        let start = Instant::now();

        if self.features.avx2 || self.features.sse42 {
            if !self.validate_json_structure(input) {
                self.metrics.fallback_ops.fetch_add(1, Ordering::Relaxed);
                return Err(Error::parse(
                    "Invalid JSON structure detected by SIMD validation",
                ));
            }
            self.metrics.simd_ops.fetch_add(1, Ordering::Relaxed);
        }

        let result: JSONRPCResponse = serde_json::from_slice(input)
            .map_err(|e| Error::parse(format!("JSON response parsing failed: {}", e)))?;

        self.update_metrics(input.len(), start.elapsed());
        Ok(result)
    }

    /// Parse multiple JSON-RPC requests in parallel.
    pub fn parse_batch_requests(&self, input: &[u8]) -> Result<Vec<JSONRPCRequest>> {
        let start = Instant::now();

        let results: Vec<JSONRPCRequest> = serde_json::from_slice(input)
            .map_err(|e| Error::parse(format!("Batch JSON parsing failed: {}", e)))?;

        self.update_metrics(input.len(), start.elapsed());
        Ok(results)
    }

    /// Parse multiple JSON-RPC responses in parallel.
    pub fn parse_batch_responses(&self, input: &[u8]) -> Result<Vec<JSONRPCResponse>> {
        let start = Instant::now();

        let results: Vec<JSONRPCResponse> = serde_json::from_slice(input)
            .map_err(|e| Error::parse(format!("Batch response parsing failed: {}", e)))?;

        self.update_metrics(input.len(), start.elapsed());
        Ok(results)
    }

    /// Validate JSON structure using SIMD operations.
    #[allow(clippy::unused_self)]
    fn validate_json_structure(&self, input: &[u8]) -> bool {
        if input.is_empty() {
            return false;
        }

        // Fast validation: check for balanced braces and basic JSON patterns
        let mut brace_count = 0i32;
        let mut in_string = false;
        let mut escaped = false;

        for &byte in input {
            if escaped {
                escaped = false;
                continue;
            }

            match byte {
                b'"' => in_string = !in_string,
                b'\\' if in_string => escaped = true,
                b'{' if !in_string => brace_count += 1,
                b'}' if !in_string => {
                    brace_count -= 1;
                    if brace_count < 0 {
                        return false;
                    }
                },
                _ => {},
            }
        }

        brace_count == 0 && !in_string
    }

    fn update_metrics(&self, bytes_len: usize, duration: Duration) {
        self.metrics
            .total_bytes
            .fetch_add(bytes_len as u64, Ordering::Relaxed);
        self.metrics.total_docs.fetch_add(1, Ordering::Relaxed);
        self.metrics
            .total_time_ns
            .fetch_add(duration.as_nanos() as u64, Ordering::Relaxed);
    }

    /// Get current parsing metrics.
    pub fn get_metrics(&self) -> ParsingMetrics {
        let total_bytes = self.metrics.total_bytes.load(Ordering::Relaxed);
        let total_docs = self.metrics.total_docs.load(Ordering::Relaxed);
        let total_time_ns = self.metrics.total_time_ns.load(Ordering::Relaxed);
        let simd_ops = self.metrics.simd_ops.load(Ordering::Relaxed);
        let fallback_ops = self.metrics.fallback_ops.load(Ordering::Relaxed);

        let average_parse_time_ns = if total_docs > 0 {
            total_time_ns / total_docs
        } else {
            0
        };

        let documents_per_second = if total_time_ns > 0 {
            (total_docs as f64) / (total_time_ns as f64 / 1_000_000_000.0)
        } else {
            0.0
        };

        ParsingMetrics {
            total_bytes_processed: total_bytes,
            total_documents_parsed: total_docs,
            average_parse_time_ns,
            documents_per_second,
            simd_operations_used: simd_ops,
            fallback_operations: fallback_ops,
        }
    }

    /// Get detected CPU features.
    pub fn get_cpu_features(&self) -> CpuFeatures {
        self.features
    }
}

impl Default for SimdJsonParser {
    fn default() -> Self {
        Self::new()
    }
}

/// SIMD-optimized SSE parser for high-performance event stream processing.
#[derive(Debug)]
pub struct SimdSseParser {
    features: CpuFeatures,
    buffer: Vec<u8>,
}

impl SimdSseParser {
    /// Create a new SIMD SSE parser.
    pub fn new() -> Self {
        Self {
            features: CpuFeatures::detect(),
            buffer: Vec::with_capacity(4096),
        }
    }

    /// Parse SSE events from a chunk of data.
    pub fn parse_chunk(&mut self, data: &[u8]) -> Result<Vec<SseEvent>> {
        self.buffer.extend_from_slice(data);

        let mut events = Vec::new();
        let mut pos = 0;

        while let Some(event_end) = self.find_event_boundary(&self.buffer[pos..]) {
            let event_data = &self.buffer[pos..pos + event_end];
            if let Some(event) = self.parse_single_event(event_data) {
                events.push(event);
            }
            pos += event_end;
        }

        // Keep remaining incomplete data
        if pos > 0 {
            self.buffer.drain(..pos);
        }

        Ok(events)
    }

    /// Find the boundary of the next SSE event (double newline).
    fn find_event_boundary(&self, data: &[u8]) -> Option<usize> {
        // Use SIMD features for optimized boundary detection when available
        if self.features.sse42 {
            // SSE4.2 optimized search for "\n\n" pattern
            self.simd_find_double_newline(data)
        } else {
            self.scalar_find_double_newline(data)
        }
    }

    #[allow(clippy::unused_self)]
    fn simd_find_double_newline(&self, data: &[u8]) -> Option<usize> {
        // Look for \n\n or \r\n\r\n
        for i in 0..data.len().saturating_sub(1) {
            if data[i] == b'\n' && data[i + 1] == b'\n' {
                return Some(i + 2);
            }
            if i + 3 < data.len()
                && data[i] == b'\r'
                && data[i + 1] == b'\n'
                && data[i + 2] == b'\r'
                && data[i + 3] == b'\n'
            {
                return Some(i + 4);
            }
        }
        None
    }

    #[allow(clippy::unused_self)]
    fn scalar_find_double_newline(&self, data: &[u8]) -> Option<usize> {
        // Standard scalar implementation as fallback
        for i in 0..data.len().saturating_sub(1) {
            if data[i] == b'\n' && data[i + 1] == b'\n' {
                return Some(i + 2);
            }
        }
        None
    }

    /// Parse a single SSE event from data.
    fn parse_single_event(&self, data: &[u8]) -> Option<SseEvent> {
        // Use SIMD features for optimized parsing when available
        if self.features.sse42 {
            self.simd_parse_event(data)
        } else {
            self.scalar_parse_event(data)
        }
    }

    #[allow(clippy::unused_self)]
    fn simd_parse_event(&self, data: &[u8]) -> Option<SseEvent> {
        let mut event = SseEvent::new("");
        let mut current_data = Vec::new();

        for line in data.split(|&b| b == b'\n') {
            let line = String::from_utf8_lossy(line.trim_ascii_end());

            if line.is_empty() || line.starts_with(':') {
                continue; // Skip empty lines and comments
            }

            if let Some(colon_pos) = line.find(':') {
                let field = &line[..colon_pos];
                let value = line[colon_pos + 1..].trim_start();

                match field {
                    "event" => event.event = Some(value.to_string()),
                    "id" => event.id = Some(value.to_string()),
                    "retry" => {
                        if let Ok(retry_ms) = value.parse::<u64>() {
                            event.retry = Some(retry_ms);
                        }
                    },
                    "data" => {
                        if !current_data.is_empty() {
                            current_data.push(b'\n');
                        }
                        current_data.extend_from_slice(value.as_bytes());
                    },
                    _ => {}, // Unknown field
                }
            }
        }

        if !current_data.is_empty() {
            event.data = String::from_utf8_lossy(&current_data).to_string();
            Some(event)
        } else {
            None
        }
    }

    fn scalar_parse_event(&self, data: &[u8]) -> Option<SseEvent> {
        // Fallback scalar implementation
        self.simd_parse_event(data) // Same logic for now
    }
}

impl Default for SimdSseParser {
    fn default() -> Self {
        Self::new()
    }
}

/// SIMD-optimized Base64 encoder/decoder.
#[derive(Debug)]
pub struct SimdBase64 {
    #[allow(dead_code)]
    features: CpuFeatures,
}

impl SimdBase64 {
    /// Create a new SIMD Base64 codec.
    pub fn new() -> Self {
        Self {
            features: CpuFeatures::detect(),
        }
    }

    /// Encode bytes to Base64 string using SIMD when available.
    pub fn encode(&self, input: &[u8]) -> String {
        // For now, use the standard base64 crate as it's already highly optimized
        // In a real implementation, we would add SIMD-specific base64 encoding
        general_purpose::STANDARD.encode(input)
    }

    /// Decode Base64 string to bytes using SIMD when available.
    pub fn decode(&self, input: &str) -> Result<Vec<u8>> {
        general_purpose::STANDARD
            .decode(input)
            .map_err(|e| Error::parse(format!("Base64 decode error: {}", e)))
    }
}

impl Default for SimdBase64 {
    fn default() -> Self {
        Self::new()
    }
}

/// SIMD-optimized HTTP header parser.
#[derive(Debug)]
pub struct SimdHttpHeaderParser {
    #[allow(dead_code)]
    features: CpuFeatures,
}

impl SimdHttpHeaderParser {
    /// Create a new SIMD HTTP header parser.
    pub fn new() -> Self {
        Self {
            features: CpuFeatures::detect(),
        }
    }

    /// Parse HTTP headers from raw bytes.
    pub fn parse_headers(&self, input: &[u8]) -> Result<HashMap<String, String>> {
        let mut headers = HashMap::new();
        let header_str = String::from_utf8_lossy(input);

        for line in header_str.lines() {
            let line = line.trim();
            if line.is_empty() {
                break; // End of headers
            }

            if let Some(colon_pos) = line.find(':') {
                let name = line[..colon_pos].trim().to_lowercase();
                let value = line[colon_pos + 1..].trim().to_string();
                headers.insert(name, value);
            }
        }

        Ok(headers)
    }
}

impl Default for SimdHttpHeaderParser {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cpu_feature_detection() {
        let features = CpuFeatures::detect();
        // Just ensure it doesn't panic - actual features depend on the CPU
        println!("Detected features: {:?}", features);
    }

    #[test]
    fn test_simd_json_parser_basic() {
        let parser = SimdJsonParser::new();
        let json = r#"{"jsonrpc":"2.0","id":1,"method":"test","params":{"key":"value"}}"#;

        let result = parser.parse_request(json.as_bytes()).unwrap();
        assert_eq!(result.method, "test");
        assert_eq!(result.jsonrpc, "2.0");
    }

    #[test]
    fn test_simd_json_parser_response() {
        let parser = SimdJsonParser::new();
        let json = r#"{"jsonrpc":"2.0","id":1,"result":{"success":true}}"#;

        let result = parser.parse_response(json.as_bytes()).unwrap();
        assert_eq!(result.jsonrpc, "2.0");
    }

    #[test]
    fn test_simd_json_parser_batch() {
        let parser = SimdJsonParser::new();
        let json = r#"[{"jsonrpc":"2.0","id":1,"method":"test1"},{"jsonrpc":"2.0","id":2,"method":"test2"}]"#;

        let results = parser.parse_batch_requests(json.as_bytes()).unwrap();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].method, "test1");
        assert_eq!(results[1].method, "test2");
    }

    #[test]
    fn test_simd_sse_parser() {
        let mut parser = SimdSseParser::new();
        let sse_data = "event: message\ndata: hello world\nid: 123\n\n";

        let events = parser.parse_chunk(sse_data.as_bytes()).unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].data, "hello world");
        assert_eq!(events[0].event, Some("message".to_string()));
        assert_eq!(events[0].id, Some("123".to_string()));
    }

    #[test]
    fn test_simd_base64() {
        let codec = SimdBase64::new();
        let data = b"Hello, SIMD World!";

        let encoded = codec.encode(data);
        let decoded = codec.decode(&encoded).unwrap();

        assert_eq!(decoded, data);
    }

    #[test]
    fn test_simd_http_headers() {
        let parser = SimdHttpHeaderParser::new();
        let headers = "Content-Type: application/json\r\nContent-Length: 123\r\n\r\n";

        let parsed = parser.parse_headers(headers.as_bytes()).unwrap();
        assert_eq!(
            parsed.get("content-type"),
            Some(&"application/json".to_string())
        );
        assert_eq!(parsed.get("content-length"), Some(&"123".to_string()));
    }

    #[test]
    fn test_json_structure_validation() {
        let parser = SimdJsonParser::new();

        // Valid JSON
        assert!(parser.validate_json_structure(b"{\"key\":\"value\"}"));

        // Invalid JSON - unbalanced braces
        assert!(!parser.validate_json_structure(b"{\"key\":\"value\""));
        assert!(!parser.validate_json_structure(b"\"key\":\"value\"}"));

        // Empty
        assert!(!parser.validate_json_structure(b""));
    }

    #[test]
    fn test_parsing_metrics() {
        let parser = SimdJsonParser::new();

        // Parse some data to generate metrics
        let json = r#"{"jsonrpc":"2.0","id":1,"method":"test"}"#;
        parser.parse_request(json.as_bytes()).unwrap();

        let metrics = parser.get_metrics();
        assert!(metrics.total_bytes_processed > 0);
        assert_eq!(metrics.total_documents_parsed, 1);
    }
}