mcplint 0.4.0

MCP Server Testing, Fuzzing, and Security Scanning Platform
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//! Anthropic Provider - Claude API integration
//!
//! Implements the AiProvider trait for Anthropic's Claude models.

use std::time::{Duration, Instant};

use anyhow::{Context, Result};
use async_trait::async_trait;
use futures::StreamExt;
use serde::{Deserialize, Serialize};

use crate::scanner::Finding;

use super::super::config::ExplanationContext;
use super::super::prompt::{PromptBuilder, SYSTEM_PROMPT};
use super::super::prompt_templates::AdvancedPromptBuilder;
use super::super::response::{
    CodeExample, EducationalContext, ExplanationMetadata, ExplanationResponse, Likelihood,
    RemediationGuide, ResourceCategory, ResourceLink, VulnerabilityExplanation, WeaknessInfo,
};
use super::super::streaming::{ChunkSender, StreamChunk};
use super::{AiProvider, AiProviderError};

/// Anthropic Claude API provider
pub struct AnthropicProvider {
    api_key: String,
    model: String,
    max_tokens: u32,
    temperature: f32,
    timeout: Duration,
    client: reqwest::Client,
    /// Whether to use advanced prompts with few-shot examples and chain-of-thought
    use_advanced_prompts: bool,
}

impl AnthropicProvider {
    const API_URL: &'static str = "https://api.anthropic.com/v1/messages";
    const API_VERSION: &'static str = "2023-06-01";

    /// Create a new Anthropic provider
    pub fn new(
        api_key: String,
        model: String,
        max_tokens: u32,
        temperature: f32,
        timeout: Duration,
    ) -> Self {
        let client = reqwest::Client::builder()
            .timeout(timeout)
            .build()
            .expect("Failed to create HTTP client");

        Self {
            api_key,
            model,
            max_tokens,
            temperature,
            timeout,
            client,
            use_advanced_prompts: false, // Disabled by default - advanced prompts need schema alignment
        }
    }

    /// Enable or disable advanced prompts with few-shot examples
    pub fn with_advanced_prompts(mut self, enabled: bool) -> Self {
        self.use_advanced_prompts = enabled;
        self
    }

    /// Build prompts for a finding, using advanced prompts if enabled
    fn build_prompts(&self, finding: &Finding, context: &ExplanationContext) -> (String, String) {
        if self.use_advanced_prompts {
            let builder = AdvancedPromptBuilder::new()
                .with_finding(finding.clone())
                .with_chain_of_thought(true)
                .with_confidence_scoring(true);
            builder.build_prompts()
        } else {
            let user_prompt = PromptBuilder::new()
                .with_finding(finding.clone())
                .with_context(context.clone())
                .build_finding_prompt();
            (SYSTEM_PROMPT.to_string(), user_prompt)
        }
    }

    /// Make a request to the Anthropic API
    async fn make_request(
        &self,
        system_prompt: &str,
        messages: Vec<Message>,
    ) -> Result<ApiResponse> {
        let request = ApiRequest {
            model: self.model.clone(),
            max_tokens: self.max_tokens,
            temperature: Some(self.temperature),
            system: Some(system_prompt.to_string()),
            messages,
        };

        let response = self
            .client
            .post(Self::API_URL)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", Self::API_VERSION)
            .header("content-type", "application/json")
            .json(&request)
            .send()
            .await
            .context("Failed to send request to Anthropic API")?;

        let status = response.status();

        if status == reqwest::StatusCode::TOO_MANY_REQUESTS {
            return Err(AiProviderError::RateLimitExceeded {
                message: "Anthropic API rate limit exceeded".to_string(),
            }
            .into());
        }

        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(AiProviderError::ApiError {
                provider: "Anthropic".to_string(),
                message: format!("HTTP {}: {}", status, error_text),
            }
            .into());
        }

        let api_response: ApiResponse = response
            .json()
            .await
            .context("Failed to parse Anthropic API response")?;

        Ok(api_response)
    }

    /// Make a streaming request to the Anthropic API
    async fn make_streaming_request(
        &self,
        system_prompt: &str,
        messages: Vec<Message>,
        sender: ChunkSender,
    ) -> Result<(String, u32)> {
        let request = StreamingApiRequest {
            model: self.model.clone(),
            max_tokens: self.max_tokens,
            temperature: Some(self.temperature),
            system: Some(system_prompt.to_string()),
            messages,
            stream: true,
        };

        let response = self
            .client
            .post(Self::API_URL)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", Self::API_VERSION)
            .header("content-type", "application/json")
            .json(&request)
            .send()
            .await
            .context("Failed to send streaming request to Anthropic API")?;

        let status = response.status();

        if status == reqwest::StatusCode::TOO_MANY_REQUESTS {
            let _ = sender.send(StreamChunk::error("Rate limit exceeded")).await;
            return Err(AiProviderError::RateLimitExceeded {
                message: "Anthropic API rate limit exceeded".to_string(),
            }
            .into());
        }

        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            let _ = sender
                .send(StreamChunk::error(format!(
                    "HTTP {}: {}",
                    status, error_text
                )))
                .await;
            return Err(AiProviderError::ApiError {
                provider: "Anthropic".to_string(),
                message: format!("HTTP {}: {}", status, error_text),
            }
            .into());
        }

        // Process the SSE stream
        let mut stream = response.bytes_stream();
        let mut accumulated_text = String::new();
        let mut total_tokens = 0u32;
        let mut buffer = String::new();

        while let Some(chunk_result) = stream.next().await {
            match chunk_result {
                Ok(bytes) => {
                    let text = String::from_utf8_lossy(&bytes);
                    buffer.push_str(&text);

                    // Process complete SSE events from buffer
                    while let Some(event_end) = buffer.find("\n\n") {
                        let event = buffer[..event_end].to_string();
                        buffer = buffer[event_end + 2..].to_string();

                        // Parse SSE event
                        if let Some(data_line) = event.lines().find(|l| l.starts_with("data: ")) {
                            let json_str = &data_line[6..];

                            // Skip [DONE] marker
                            if json_str.trim() == "[DONE]" {
                                continue;
                            }

                            // Parse the streaming event
                            if let Ok(sse_event) = serde_json::from_str::<StreamEvent>(json_str) {
                                match sse_event.event_type.as_str() {
                                    "content_block_delta" => {
                                        if let Some(delta) = sse_event.delta {
                                            if let Some(text) = delta.text {
                                                accumulated_text.push_str(&text);
                                                let _ = sender.send(StreamChunk::text(&text)).await;
                                            }
                                        }
                                    }
                                    "message_delta" => {
                                        if let Some(usage) = sse_event.usage {
                                            total_tokens = usage.input_tokens + usage.output_tokens;
                                            let _ = sender
                                                .send(StreamChunk::TokenUpdate {
                                                    input: usage.input_tokens,
                                                    output: usage.output_tokens,
                                                })
                                                .await;
                                        }
                                    }
                                    "message_stop" => {
                                        // Stream complete
                                    }
                                    _ => {}
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    let _ = sender
                        .send(StreamChunk::error(format!("Stream error: {}", e)))
                        .await;
                    return Err(anyhow::anyhow!("Stream error: {}", e));
                }
            }
        }

        let _ = sender.send(StreamChunk::Done).await;
        Ok((accumulated_text, total_tokens))
    }

    /// Parse the AI response into structured format
    fn parse_response(
        &self,
        finding: &Finding,
        response_text: &str,
        response_time_ms: u64,
        tokens_used: u32,
    ) -> Result<ExplanationResponse> {
        // Try to extract JSON from the response
        let json_str = extract_json(response_text)?;

        // Debug: log what we're trying to parse if it fails
        let parsed: ParsedExplanation =
            serde_json::from_str(&json_str).map_err(|e| AiProviderError::ParseError {
                message: format!(
                    "Failed to parse AI response: {}. JSON preview: {}",
                    e,
                    &json_str[..json_str.len().min(500)]
                ),
            })?;

        // Convert to our response type
        let explanation = VulnerabilityExplanation {
            summary: parsed.explanation.summary,
            technical_details: parsed.explanation.technical_details,
            attack_scenario: parsed.explanation.attack_scenario,
            impact: parsed.explanation.impact,
            likelihood: parsed
                .explanation
                .likelihood
                .parse()
                .unwrap_or(Likelihood::Medium),
        };

        let code_example = parsed.remediation.code_example.map(|ce| {
            CodeExample::new(ce.language, ce.before, ce.after).with_explanation(ce.explanation)
        });

        let remediation = RemediationGuide {
            immediate_actions: parsed.remediation.immediate_actions,
            permanent_fix: parsed.remediation.permanent_fix,
            code_example,
            verification: parsed.remediation.verification,
            ..Default::default()
        };

        let education = parsed.education.map(|edu| {
            let related_weaknesses = edu
                .related_weaknesses
                .into_iter()
                .map(|w| WeaknessInfo::new(w.cwe_id, w.name, w.description))
                .collect();

            let resources = edu
                .resources
                .into_iter()
                .map(|r| ResourceLink {
                    title: r.title,
                    url: r.url,
                    category: match r.category.as_str() {
                        "article" => ResourceCategory::Article,
                        "tool" => ResourceCategory::Tool,
                        "video" => ResourceCategory::Video,
                        "course" => ResourceCategory::Course,
                        _ => ResourceCategory::Documentation,
                    },
                })
                .collect();

            EducationalContext {
                related_weaknesses,
                similar_patterns: edu.similar_patterns,
                best_practices: edu.best_practices,
                resources,
            }
        });

        let metadata = ExplanationMetadata::new("anthropic", &self.model)
            .with_tokens(tokens_used)
            .with_response_time(response_time_ms);

        let mut response = ExplanationResponse::new(&finding.id, &finding.rule_id)
            .with_explanation(explanation)
            .with_remediation(remediation)
            .with_metadata(metadata);

        if let Some(edu) = education {
            response = response.with_education(edu);
        }

        Ok(response)
    }
}

#[async_trait]
impl AiProvider for AnthropicProvider {
    fn name(&self) -> &'static str {
        "Anthropic"
    }

    fn model(&self) -> &str {
        &self.model
    }

    fn supports_streaming(&self) -> bool {
        true
    }

    async fn explain_finding(
        &self,
        finding: &Finding,
        context: &ExplanationContext,
    ) -> Result<ExplanationResponse> {
        let start = Instant::now();

        // Use advanced prompts with few-shot examples if enabled
        let (system_prompt, user_prompt) = self.build_prompts(finding, context);

        let messages = vec![Message {
            role: "user".to_string(),
            content: user_prompt,
        }];

        let response = self.make_request(&system_prompt, messages).await?;

        let response_text = response
            .content
            .first()
            .map(|c| c.text.as_str())
            .unwrap_or("");

        let tokens_used = response.usage.output_tokens + response.usage.input_tokens;
        let response_time_ms = start.elapsed().as_millis() as u64;

        self.parse_response(finding, response_text, response_time_ms, tokens_used)
    }

    async fn explain_finding_streaming(
        &self,
        finding: &Finding,
        context: &ExplanationContext,
        sender: ChunkSender,
    ) -> Result<ExplanationResponse> {
        let start = Instant::now();

        // Use advanced prompts with few-shot examples if enabled
        let (system_prompt, user_prompt) = self.build_prompts(finding, context);

        let messages = vec![Message {
            role: "user".to_string(),
            content: user_prompt,
        }];

        let (response_text, tokens_used) = self
            .make_streaming_request(&system_prompt, messages, sender)
            .await?;

        let response_time_ms = start.elapsed().as_millis() as u64;

        self.parse_response(finding, &response_text, response_time_ms, tokens_used)
    }

    async fn ask_followup(
        &self,
        explanation: &ExplanationResponse,
        question: &str,
    ) -> Result<String> {
        // Create a minimal finding for context
        let finding = Finding::new(
            &explanation.rule_id,
            crate::scanner::Severity::Medium,
            &explanation.explanation.summary,
            "",
        );

        let prompt = PromptBuilder::build_followup_prompt(
            &finding,
            &explanation.explanation.summary,
            question,
        );

        let messages = vec![Message {
            role: "user".to_string(),
            content: prompt,
        }];

        // Use standard system prompt for followups
        let response = self.make_request(SYSTEM_PROMPT, messages).await?;

        Ok(response
            .content
            .first()
            .map(|c| c.text.clone())
            .unwrap_or_default())
    }

    async fn health_check(&self) -> Result<bool> {
        // Simple health check - try to make a minimal request
        let messages = vec![Message {
            role: "user".to_string(),
            content: "Respond with just the word 'ok'".to_string(),
        }];

        let request = ApiRequest {
            model: self.model.clone(),
            max_tokens: 10,
            temperature: Some(0.0),
            system: None,
            messages,
        };

        let response = self
            .client
            .post(Self::API_URL)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", Self::API_VERSION)
            .header("content-type", "application/json")
            .json(&request)
            .send()
            .await?;

        Ok(response.status().is_success())
    }
}

/// Sanitize JSON string by properly escaping control characters within string values
fn sanitize_json(text: &str) -> String {
    let mut result = String::with_capacity(text.len());
    let mut in_string = false;
    let mut escape_next = false;

    for c in text.chars() {
        if escape_next {
            // Previous char was backslash, this char is escaped
            result.push(c);
            escape_next = false;
            continue;
        }

        match c {
            '\\' if in_string => {
                result.push(c);
                escape_next = true;
            }
            '"' => {
                in_string = !in_string;
                result.push(c);
            }
            '\n' | '\r' if in_string => {
                result.push_str("\\n");
            }
            '\t' if in_string => {
                result.push_str("\\t");
            }
            c if c.is_control() => {
                result.push(' ');
            }
            _ => {
                result.push(c);
            }
        }
    }
    result
}

/// Extract JSON from a response that might have markdown code blocks
fn extract_json(text: &str) -> Result<String> {
    // Sanitize the text first to remove problematic control characters
    let text = sanitize_json(text);

    // Try to find JSON in code blocks
    if let Some(start) = text.find("```json") {
        if let Some(end) = text[start..]
            .find("```\n")
            .or_else(|| text[start..].rfind("```"))
        {
            let json_start = start + 7; // Skip "```json"
            let json_end = start + end;
            if json_start < json_end {
                return Ok(text[json_start..json_end].trim().to_string());
            }
        }
    }

    // Try to find JSON in regular code blocks
    if let Some(start) = text.find("```") {
        if let Some(end) = text[start + 3..].find("```") {
            let json_start = start + 3;
            let json_end = start + 3 + end;
            let content = text[json_start..json_end].trim();
            // Skip language identifier if present
            if let Some(newline) = content.find('\n') {
                return Ok(content[newline..].trim().to_string());
            }
        }
    }

    // Try to find raw JSON (starts with { and ends with })
    if let Some(start) = text.find('{') {
        if let Some(end) = text.rfind('}') {
            if start < end {
                return Ok(text[start..=end].to_string());
            }
        }
    }

    // Return the whole text if no JSON found
    Ok(text.to_string())
}

// API Request/Response types

#[derive(Serialize)]
struct ApiRequest {
    model: String,
    max_tokens: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    system: Option<String>,
    messages: Vec<Message>,
}

#[derive(Serialize)]
struct StreamingApiRequest {
    model: String,
    max_tokens: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    system: Option<String>,
    messages: Vec<Message>,
    stream: bool,
}

// Streaming event types
#[derive(Deserialize)]
struct StreamEvent {
    #[serde(rename = "type")]
    event_type: String,
    #[serde(default)]
    delta: Option<StreamDelta>,
    #[serde(default)]
    usage: Option<StreamUsage>,
}

#[derive(Deserialize)]
struct StreamDelta {
    #[serde(default)]
    text: Option<String>,
}

#[derive(Deserialize)]
struct StreamUsage {
    #[serde(default)]
    input_tokens: u32,
    #[serde(default)]
    output_tokens: u32,
}

#[derive(Serialize, Deserialize)]
struct Message {
    role: String,
    content: String,
}

#[derive(Deserialize)]
struct ApiResponse {
    content: Vec<ContentBlock>,
    usage: Usage,
}

#[derive(Deserialize)]
struct ContentBlock {
    text: String,
}

#[derive(Deserialize)]
struct Usage {
    input_tokens: u32,
    output_tokens: u32,
}

// Parsed response structure

#[derive(Deserialize)]
struct ParsedExplanation {
    explanation: ParsedVulnerability,
    #[serde(default)]
    remediation: ParsedRemediation,
    education: Option<ParsedEducation>,
}

#[derive(Deserialize)]
struct ParsedVulnerability {
    summary: String,
    #[serde(default)]
    technical_details: String,
    #[serde(default)]
    attack_scenario: String,
    #[serde(default)]
    impact: String,
    #[serde(default = "default_likelihood")]
    likelihood: String,
}

fn default_likelihood() -> String {
    "medium".to_string()
}

#[derive(Deserialize, Default)]
struct ParsedRemediation {
    #[serde(default)]
    immediate_actions: Vec<String>,
    #[serde(default)]
    permanent_fix: String,
    code_example: Option<ParsedCodeExample>,
    #[serde(default)]
    verification: Vec<String>,
}

#[derive(Deserialize)]
struct ParsedCodeExample {
    language: String,
    before: String,
    after: String,
    explanation: String,
}

#[derive(Deserialize)]
struct ParsedEducation {
    related_weaknesses: Vec<ParsedWeakness>,
    similar_patterns: Vec<String>,
    best_practices: Vec<String>,
    resources: Vec<ParsedResource>,
}

#[derive(Deserialize)]
struct ParsedWeakness {
    cwe_id: String,
    name: String,
    description: String,
}

#[derive(Deserialize)]
struct ParsedResource {
    title: String,
    url: String,
    category: String,
}

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

    #[test]
    fn extract_json_from_code_block() {
        let text = r#"Here's my analysis:
```json
{"key": "value"}
```
"#;
        let result = extract_json(text).unwrap();
        assert!(result.contains("key"));
    }

    #[test]
    fn extract_raw_json() {
        let text = r#"The response is {"key": "value"} as shown."#;
        let result = extract_json(text).unwrap();
        assert_eq!(result, r#"{"key": "value"}"#);
    }
}