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
//! AI Provider - Provider trait and implementations
//!
//! Defines the trait for AI providers and provides implementations
//! for Anthropic, OpenAI, Ollama, and a Mock provider for testing.

pub mod anthropic;
pub mod mock;
pub mod ollama;
pub mod openai;

use anyhow::Result;
use async_trait::async_trait;

use crate::scanner::Finding;

use super::config::ExplanationContext;
use super::response::ExplanationResponse;
use super::streaming::ChunkSender;

// Re-exports
pub use anthropic::AnthropicProvider;
pub use mock::MockProvider;
pub use ollama::OllamaProvider;
pub use openai::OpenAiProvider;

/// Trait for AI providers that can generate vulnerability explanations
#[async_trait]
pub trait AiProvider: Send + Sync {
    /// Provider name
    fn name(&self) -> &'static str;

    /// Model being used
    fn model(&self) -> &str;

    /// Check if this provider supports streaming
    fn supports_streaming(&self) -> bool {
        false
    }

    /// Generate explanation for a single finding
    async fn explain_finding(
        &self,
        finding: &Finding,
        context: &ExplanationContext,
    ) -> Result<ExplanationResponse>;

    /// Generate explanation with streaming output
    ///
    /// Sends chunks through the provided sender as they arrive.
    /// Returns the final complete response.
    async fn explain_finding_streaming(
        &self,
        finding: &Finding,
        context: &ExplanationContext,
        sender: ChunkSender,
    ) -> Result<ExplanationResponse> {
        // Default implementation: fall back to non-streaming
        use super::streaming::StreamChunk;

        let response = self.explain_finding(finding, context).await?;

        // Send the complete response as a single chunk
        let _ = sender
            .send(StreamChunk::text(&response.explanation.summary))
            .await;
        let _ = sender.send(StreamChunk::Done).await;

        Ok(response)
    }

    /// Generate explanations for multiple findings (batch)
    async fn explain_batch(
        &self,
        findings: &[Finding],
        context: &ExplanationContext,
    ) -> Result<Vec<ExplanationResponse>> {
        // Default implementation: sequential processing
        let mut results = Vec::with_capacity(findings.len());
        for finding in findings {
            let explanation = self.explain_finding(finding, context).await?;
            results.push(explanation);
        }
        Ok(results)
    }

    /// Interactive follow-up question
    async fn ask_followup(
        &self,
        explanation: &ExplanationResponse,
        question: &str,
    ) -> Result<String>;

    /// Interactive follow-up with streaming
    async fn ask_followup_streaming(
        &self,
        explanation: &ExplanationResponse,
        question: &str,
        sender: ChunkSender,
    ) -> Result<String> {
        // Default implementation: fall back to non-streaming
        use super::streaming::StreamChunk;

        let response = self.ask_followup(explanation, question).await?;

        // Send the complete response as a single chunk
        let _ = sender.send(StreamChunk::text(&response)).await;
        let _ = sender.send(StreamChunk::Done).await;

        Ok(response)
    }

    /// Check if provider is available and configured correctly
    async fn health_check(&self) -> Result<bool>;
}

/// Error types for AI providers
#[derive(Debug, thiserror::Error)]
pub enum AiProviderError {
    #[error("API key not configured for {provider}")]
    MissingApiKey { provider: String },

    #[error("Rate limit exceeded: {message}")]
    RateLimitExceeded { message: String },

    #[error("API error from {provider}: {message}")]
    ApiError { provider: String, message: String },

    #[error("Invalid response from {provider}: {message}")]
    InvalidResponse { provider: String, message: String },

    #[error("Request timeout after {seconds}s")]
    Timeout { seconds: u64 },

    #[error("Provider {provider} is not available: {reason}")]
    Unavailable { provider: String, reason: String },

    #[error("Failed to parse AI response: {message}")]
    ParseError { message: String },
}

/// Create a provider based on configuration
pub fn create_provider(config: &super::config::AiConfig) -> Result<Box<dyn AiProvider>> {
    match config.provider {
        super::config::AiProvider::Anthropic => {
            let api_key = config
                .api_key
                .clone()
                .or_else(|| std::env::var("ANTHROPIC_API_KEY").ok())
                .ok_or_else(|| AiProviderError::MissingApiKey {
                    provider: "Anthropic".to_string(),
                })?;

            Ok(Box::new(AnthropicProvider::new(
                api_key,
                config.model.clone(),
                config.max_tokens,
                config.temperature,
                config.timeout(),
            )))
        }
        super::config::AiProvider::OpenAI => {
            let api_key = config
                .api_key
                .clone()
                .or_else(|| std::env::var("OPENAI_API_KEY").ok())
                .ok_or_else(|| AiProviderError::MissingApiKey {
                    provider: "OpenAI".to_string(),
                })?;

            Ok(Box::new(OpenAiProvider::new(
                api_key,
                config.model.clone(),
                config.max_tokens,
                config.temperature,
                config.timeout(),
            )))
        }
        super::config::AiProvider::Ollama => Ok(Box::new(OllamaProvider::new(
            config.ollama_url.clone(),
            config.model.clone(),
            config.timeout(),
        ))),
    }
}

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

    fn sample_finding() -> Finding {
        Finding::new(
            "MCP-TEST-001",
            Severity::High,
            "Test Finding",
            "This is a test finding for unit tests",
        )
    }

    #[test]
    fn error_display() {
        let err = AiProviderError::MissingApiKey {
            provider: "Anthropic".to_string(),
        };
        assert!(err.to_string().contains("Anthropic"));

        let err = AiProviderError::RateLimitExceeded {
            message: "Too many requests".to_string(),
        };
        assert!(err.to_string().contains("Rate limit"));
    }

    #[test]
    fn all_error_variants_display() {
        let errors = vec![
            AiProviderError::MissingApiKey {
                provider: "TestProvider".to_string(),
            },
            AiProviderError::RateLimitExceeded {
                message: "Limit exceeded".to_string(),
            },
            AiProviderError::ApiError {
                provider: "TestProvider".to_string(),
                message: "API failed".to_string(),
            },
            AiProviderError::InvalidResponse {
                provider: "TestProvider".to_string(),
                message: "Bad JSON".to_string(),
            },
            AiProviderError::Timeout { seconds: 30 },
            AiProviderError::Unavailable {
                provider: "TestProvider".to_string(),
                reason: "Service down".to_string(),
            },
            AiProviderError::ParseError {
                message: "Parse failed".to_string(),
            },
        ];

        for error in errors {
            let display = error.to_string();
            assert!(!display.is_empty(), "Error should have non-empty display");
        }
    }

    #[test]
    fn create_provider_ollama_success() {
        let config = super::super::config::AiConfig::builder()
            .provider(super::super::config::AiProvider::Ollama)
            .model("llama3.2")
            .build();

        let result = create_provider(&config);
        assert!(result.is_ok());

        let provider = result.unwrap();
        assert_eq!(provider.name(), "Ollama");
        assert_eq!(provider.model(), "llama3.2");
    }

    #[test]
    fn create_provider_anthropic_missing_key() {
        // Clear env var to ensure it's not set
        std::env::remove_var("ANTHROPIC_API_KEY");

        let config = super::super::config::AiConfig::builder()
            .provider(super::super::config::AiProvider::Anthropic)
            .model("claude-3-5-sonnet-20241022")
            .build();

        let result = create_provider(&config);
        assert!(result.is_err(), "Should fail without API key");
    }

    #[test]
    fn create_provider_anthropic_with_config_key() {
        let config = super::super::config::AiConfig::builder()
            .provider(super::super::config::AiProvider::Anthropic)
            .model("claude-3-5-sonnet-20241022")
            .api_key("test-key")
            .build();

        let result = create_provider(&config);
        assert!(result.is_ok());

        let provider = result.unwrap();
        assert_eq!(provider.name(), "Anthropic");
        assert_eq!(provider.model(), "claude-3-5-sonnet-20241022");
    }

    #[test]
    fn create_provider_openai_missing_key() {
        // Clear env var to ensure it's not set
        std::env::remove_var("OPENAI_API_KEY");

        let config = super::super::config::AiConfig::builder()
            .provider(super::super::config::AiProvider::OpenAI)
            .model("gpt-4")
            .build();

        let result = create_provider(&config);
        assert!(result.is_err(), "Should fail without API key");
    }

    #[test]
    fn create_provider_openai_with_config_key() {
        let config = super::super::config::AiConfig::builder()
            .provider(super::super::config::AiProvider::OpenAI)
            .model("gpt-4")
            .api_key("test-key")
            .build();

        let result = create_provider(&config);
        assert!(result.is_ok());

        let provider = result.unwrap();
        assert_eq!(provider.name(), "OpenAI");
        assert_eq!(provider.model(), "gpt-4");
    }

    #[tokio::test]
    async fn provider_trait_default_streaming() {
        let provider = MockProvider::new();
        let finding = sample_finding();
        let context = ExplanationContext::default();

        let (sender, mut receiver) = super::super::streaming::stream_channel(32);

        // Test that default implementation falls back to non-streaming
        let result = provider
            .explain_finding_streaming(&finding, &context, sender)
            .await;
        assert!(result.is_ok());

        // Should get at least a Done chunk
        let mut got_chunk = false;
        while let Ok(chunk) = receiver.try_recv() {
            got_chunk = true;
            if chunk.is_terminal() {
                break;
            }
        }
        assert!(got_chunk);
    }

    #[tokio::test]
    async fn provider_trait_default_batch() {
        let provider = MockProvider::new();
        let findings = vec![
            sample_finding(),
            Finding::new(
                "MCP-TEST-002",
                Severity::Medium,
                "Second Finding",
                "Another test",
            ),
        ];
        let context = ExplanationContext::default();

        // Default batch implementation should process sequentially
        let result = provider.explain_batch(&findings, &context).await;
        assert!(result.is_ok());

        let responses = result.unwrap();
        assert_eq!(responses.len(), 2);
        assert_eq!(responses[0].finding_id, findings[0].id);
        assert_eq!(responses[1].finding_id, findings[1].id);
    }

    #[tokio::test]
    async fn provider_trait_default_followup_streaming() {
        let provider = MockProvider::new();
        let explanation = ExplanationResponse::new("test", "TEST-001");

        let (sender, mut receiver) = super::super::streaming::stream_channel(32);

        // Test default implementation
        let result = provider
            .ask_followup_streaming(&explanation, "How to fix?", sender)
            .await;
        assert!(result.is_ok());

        // Should get at least a Done chunk
        let mut got_chunk = false;
        while let Ok(chunk) = receiver.try_recv() {
            got_chunk = true;
            if chunk.is_terminal() {
                break;
            }
        }
        assert!(got_chunk);
    }

    #[test]
    fn provider_trait_default_supports_streaming() {
        // Create a minimal provider to test default trait method
        struct MinimalProvider;

        #[async_trait::async_trait]
        impl AiProvider for MinimalProvider {
            fn name(&self) -> &'static str {
                "Minimal"
            }
            fn model(&self) -> &str {
                "test"
            }
            async fn explain_finding(
                &self,
                _: &Finding,
                _: &ExplanationContext,
            ) -> Result<ExplanationResponse> {
                Ok(ExplanationResponse::new("test", "TEST"))
            }
            async fn ask_followup(&self, _: &ExplanationResponse, _: &str) -> Result<String> {
                Ok("test".to_string())
            }
            async fn health_check(&self) -> Result<bool> {
                Ok(true)
            }
        }

        let provider = MinimalProvider;
        // Default implementation should return false
        assert!(!provider.supports_streaming());
    }
}