context-mcp 0.1.6

MCP server for context storage, text-based retrieval, and temporal tracking with optional persistence
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
//! CPU-optimized text-based context retrieval with scoring
//!
//! Provides parallel processing capabilities using rayon for efficient
//! text matching and relevance scoring of stored contexts.

use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use crate::context::{Context, ContextDomain, ContextQuery};
use crate::error::ContextResult;
use crate::storage::ContextStore;
use crate::temporal::{TemporalQuery, TemporalStats};

/// RAG processor configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RagConfig {
    /// Maximum results per query
    pub max_results: usize,
    /// Minimum relevance threshold (0.0 to 1.0)
    pub min_relevance: f64,
    /// Enable parallel processing
    pub parallel: bool,
    /// Number of threads (0 = auto)
    pub num_threads: usize,
    /// Apply temporal decay to scoring
    pub temporal_decay: bool,
    /// Only retrieve screened-safe contexts
    pub safe_only: bool,
    /// Chunk size for parallel processing
    pub chunk_size: usize,
}

impl Default for RagConfig {
    fn default() -> Self {
        Self {
            max_results: 10,
            min_relevance: 0.1,
            parallel: true,
            num_threads: 0, // Auto-detect
            temporal_decay: true,
            safe_only: true,
            chunk_size: 1000,
        }
    }
}

/// Result from RAG retrieval with scoring
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScoredContext {
    /// The context
    pub context: Context,
    /// Relevance score (0.0 to 1.0)
    pub score: f64,
    /// Contributing score components
    pub score_breakdown: ScoreBreakdown,
}

/// Breakdown of score components
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ScoreBreakdown {
    /// Temporal relevance
    pub temporal: f64,
    /// Importance score
    pub importance: f64,
    /// Domain match score
    pub domain_match: f64,
    /// Tag match score
    pub tag_match: f64,
    /// Content similarity (if embedding available)
    pub similarity: Option<f64>,
}

/// RAG retrieval results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetrievalResult {
    /// Scored contexts
    pub contexts: Vec<ScoredContext>,
    /// Query used
    pub query_summary: String,
    /// Processing time in ms
    pub processing_time_ms: u64,
    /// Total candidates considered
    pub candidates_considered: usize,
    /// Temporal statistics
    pub temporal_stats: TemporalStats,
}

/// CPU-optimized RAG processor
pub struct RagProcessor {
    config: RagConfig,
    store: Arc<ContextStore>,
}

impl RagProcessor {
    /// Create a new RAG processor
    pub fn new(store: Arc<ContextStore>, config: RagConfig) -> Self {
        // Configure thread pool if specified
        if config.num_threads > 0 {
            rayon::ThreadPoolBuilder::new()
                .num_threads(config.num_threads)
                .build_global()
                .ok();
        }

        Self { config, store }
    }

    /// Create with default configuration
    pub fn with_defaults(store: Arc<ContextStore>) -> Self {
        Self::new(store, RagConfig::default())
    }

    /// Retrieve contexts using a query
    pub async fn retrieve(&self, query: &RetrievalQuery) -> ContextResult<RetrievalResult> {
        let start = std::time::Instant::now();

        // Build context query
        let mut ctx_query = ContextQuery::new();

        if let Some(domain) = &query.domain {
            ctx_query = ctx_query.with_domain(domain.clone());
        }

        for tag in &query.tags {
            ctx_query = ctx_query.with_tag(tag.clone());
        }

        if let Some(min_importance) = query.min_importance {
            ctx_query = ctx_query.with_min_importance(min_importance);
        }

        // Get candidates from storage
        let candidates: Vec<Context> = self.store.query(&ctx_query).await?;
        let candidates_count = candidates.len();

        // Apply temporal filtering
        let temporal_query = query.temporal.clone().unwrap_or_default();
        let filtered: Vec<Context> = candidates
            .into_iter()
            .filter(|c| temporal_query.matches(c))
            .filter(|c| !self.config.safe_only || c.is_safe())
            .collect();

        // Score contexts (parallel or sequential)
        let scored = if self.config.parallel && filtered.len() > self.config.chunk_size {
            self.score_parallel(&filtered, query, &temporal_query)
        } else {
            self.score_sequential(&filtered, query, &temporal_query)
        };

        // Filter by minimum relevance and sort
        let mut results: Vec<ScoredContext> = scored
            .into_iter()
            .filter(|s| s.score >= self.config.min_relevance)
            .collect();

        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        results.truncate(self.config.max_results);

        let temporal_stats = TemporalStats::from_contexts(
            &results
                .iter()
                .map(|s| s.context.clone())
                .collect::<Vec<_>>(),
        );

        Ok(RetrievalResult {
            contexts: results,
            query_summary: query.to_string(),
            processing_time_ms: start.elapsed().as_millis() as u64,
            candidates_considered: candidates_count,
            temporal_stats,
        })
    }

    /// Score contexts in parallel using rayon
    fn score_parallel(
        &self,
        contexts: &[Context],
        query: &RetrievalQuery,
        temporal: &TemporalQuery,
    ) -> Vec<ScoredContext> {
        contexts
            .par_iter()
            .map(|ctx| self.score_context(ctx, query, temporal))
            .collect()
    }

    /// Score contexts sequentially
    fn score_sequential(
        &self,
        contexts: &[Context],
        query: &RetrievalQuery,
        temporal: &TemporalQuery,
    ) -> Vec<ScoredContext> {
        contexts
            .iter()
            .map(|ctx| self.score_context(ctx, query, temporal))
            .collect()
    }

    /// Score a single context
    fn score_context(
        &self,
        ctx: &Context,
        query: &RetrievalQuery,
        temporal: &TemporalQuery,
    ) -> ScoredContext {
        let temporal_score = if self.config.temporal_decay {
            temporal.relevance_score(ctx)
        } else {
            1.0
        };

        let importance_score = ctx.metadata.importance as f64;

        let domain_match_score = if query.domain.as_ref() == Some(&ctx.domain) {
            1.0
        } else if query.domain.is_none() {
            0.5 // Neutral if no domain specified
        } else {
            0.2 // Partial credit for different domains
        };

        let tag_match_score = if !query.tags.is_empty() {
            let matching_tags = query
                .tags
                .iter()
                .filter(|t| ctx.metadata.tags.contains(*t))
                .count();
            matching_tags as f64 / query.tags.len() as f64
        } else {
            0.5 // Neutral
        };

        let breakdown = ScoreBreakdown {
            temporal: temporal_score,
            importance: importance_score,
            domain_match: domain_match_score,
            tag_match: tag_match_score,
            similarity: None, // Placeholder for embedding-based scoring
        };

        // Weighted final score
        let score = 0.25 * breakdown.temporal
            + 0.25 * breakdown.importance
            + 0.25 * breakdown.domain_match
            + 0.25 * breakdown.tag_match;

        ScoredContext {
            context: ctx.clone(),
            score,
            score_breakdown: breakdown,
        }
    }

    /// Retrieve by text query with simple keyword matching
    pub async fn retrieve_by_text(&self, text: &str) -> ContextResult<RetrievalResult> {
        let query = RetrievalQuery::from_text(text);
        self.retrieve(&query).await
    }

    /// Get configuration
    pub fn config(&self) -> &RagConfig {
        &self.config
    }
}

/// Query for RAG retrieval
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RetrievalQuery {
    /// Text query (for keyword/semantic matching)
    pub text: Option<String>,
    /// Domain filter
    pub domain: Option<ContextDomain>,
    /// Tag filters
    pub tags: Vec<String>,
    /// Minimum importance
    pub min_importance: Option<f32>,
    /// Temporal query parameters
    pub temporal: Option<TemporalQuery>,
    /// Maximum results
    pub max_results: Option<usize>,
}

impl RetrievalQuery {
    /// Create a new retrieval query
    pub fn new() -> Self {
        Self::default()
    }

    /// Create from text
    pub fn from_text(text: &str) -> Self {
        Self {
            text: Some(text.to_string()),
            ..Default::default()
        }
    }

    /// Set domain filter
    pub fn with_domain(mut self, domain: ContextDomain) -> Self {
        self.domain = Some(domain);
        self
    }

    /// Add tag filter
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Set minimum importance
    pub fn with_min_importance(mut self, importance: f32) -> Self {
        self.min_importance = Some(importance);
        self
    }

    /// Set temporal parameters
    pub fn with_temporal(mut self, temporal: TemporalQuery) -> Self {
        self.temporal = Some(temporal);
        self
    }

    /// Query for recent contexts
    pub fn recent(hours: i64) -> Self {
        Self::new().with_temporal(TemporalQuery::recent(hours))
    }
}

impl std::fmt::Display for RetrievalQuery {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut parts = Vec::new();

        if let Some(text) = &self.text {
            parts.push(format!("text: '{}'", text));
        }
        if let Some(domain) = &self.domain {
            parts.push(format!("domain: {:?}", domain));
        }
        if !self.tags.is_empty() {
            parts.push(format!("tags: {:?}", self.tags));
        }
        if let Some(importance) = self.min_importance {
            parts.push(format!("min_importance: {}", importance));
        }

        if parts.is_empty() {
            write!(f, "all contexts")
        } else {
            write!(f, "{}", parts.join(", "))
        }
    }
}

/// Batch processing for multiple queries
pub struct BatchProcessor {
    processor: Arc<RagProcessor>,
}

impl BatchProcessor {
    /// Create a new batch processor
    pub fn new(processor: Arc<RagProcessor>) -> Self {
        Self { processor }
    }

    /// Process multiple queries (sequential for async compatibility)
    pub async fn process_batch(
        &self,
        queries: Vec<RetrievalQuery>,
    ) -> Vec<ContextResult<RetrievalResult>> {
        let mut results = Vec::with_capacity(queries.len());
        for query in queries {
            results.push(self.processor.retrieve(&query).await);
        }
        results
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::StorageConfig;
    use tempfile::TempDir;

    fn create_test_store() -> (Arc<ContextStore>, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let config = StorageConfig {
            persist_path: Some(temp_dir.path().to_path_buf()),
            enable_persistence: true,
            ..Default::default()
        };
        let store = ContextStore::new(config).unwrap();
        (Arc::new(store), temp_dir)
    }

    #[test]
    fn test_retrieval_query() {
        let query = RetrievalQuery::from_text("test query")
            .with_domain(ContextDomain::Code)
            .with_tag("rust");

        assert_eq!(query.text, Some("test query".to_string()));
        assert_eq!(query.domain, Some(ContextDomain::Code));
        assert!(query.tags.contains(&"rust".to_string()));
    }

    #[tokio::test]
    async fn test_rag_processor() {
        let (store, _temp) = create_test_store();
        let processor = RagProcessor::with_defaults(store.clone());

        // Add test context
        let ctx = Context::new("Test content", ContextDomain::Code);
        store.store(ctx).await.unwrap();

        // Retrieve
        let result = processor.retrieve(&RetrievalQuery::new()).await.unwrap();
        assert_eq!(result.candidates_considered, 1);
    }
}