mc-mcp 0.1.10

A Model Context Protocol (MCP) server for metacontract smart contract development.
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
use crate::domain::reference::{SearchQuery, SearchResult};
use crate::infrastructure::EmbeddingGenerator;
use anyhow::{anyhow, Result};
use std::sync::Arc;
use log::{error, info, warn, debug};

use crate::domain::vector_repository::VectorRepository;
use crate::domain::reference::ReferenceService;
use crate::config::{DocumentSource, SourceType, McpConfig};
use crate::infrastructure::file_system::{load_documents_from_source, load_content_from_archive};
use crate::infrastructure::vector_db::DocumentToUpsert;
use futures::future::BoxFuture;
use qdrant_client::qdrant::{PointStruct, ScoredPoint};

// Implementation using infrastructure components
pub struct ReferenceServiceImpl {
    embedder: Arc<EmbeddingGenerator>,
    vector_db: Arc<dyn VectorRepository>,
    config: Arc<McpConfig>,
}

impl ReferenceServiceImpl {
    pub fn new(embedder: Arc<EmbeddingGenerator>, vector_db: Arc<dyn VectorRepository>, config: Arc<McpConfig>) -> Self {
        Self {
            embedder,
            vector_db,
            config,
        }
    }

    // Simple chunking logic (example: split by paragraphs or fixed size)
    fn chunk_document(&self, file_path: &str, content: &str) -> Vec<String> {
        // Placeholder: Split by double newline (paragraph)
        // A more robust solution would handle different markdown structures better
        // and potentially use a sliding window or size-based chunking.
        log::debug!("Chunking document: {}", file_path);
        content
            .split("\n\n")
               .map(str::trim)
               .filter(|s| !s.is_empty())
               .map(String::from)
               .collect()
    }

    // Helper to process and upsert chunks from a single source
    async fn process_and_upsert_source(
        &self,
        source_name: &str,
        documents: &std::collections::HashMap<String, String>,
    ) -> Result<()> {
        let mut all_docs_to_upsert: Vec<DocumentToUpsert> = Vec::new();
        let mut total_chunks = 0;

        for (file_path, content) in documents {
            let chunks = self.chunk_document(file_path, content);
            total_chunks += chunks.len();
            log::debug!("Generated {} chunks for {}", chunks.len(), file_path);

            if chunks.is_empty() {
                continue;
            }

            let chunk_slices: Vec<&str> = chunks.iter().map(AsRef::as_ref).collect();
            let embeddings = match self.embedder.generate_embeddings(&chunk_slices) {
                Ok(e) => e,
                Err(e) => {
                    error!("Failed to generate embeddings for {}: {}", file_path, e);
                    continue; // Skip this file if embedding fails
                }
            };
            log::debug!(
                "Generated {} embeddings for {}",
                embeddings.len(),
                file_path
            );

            let docs_to_upsert: Vec<DocumentToUpsert> = chunks
                .into_iter()
                .zip(embeddings.into_iter())
                .map(|(chunk_content, vector)| {
                    // Create DocumentToUpsert with all necessary fields
                    DocumentToUpsert {
                        file_path: file_path.clone(),
                        vector,
                        source: Some(source_name.to_string()), // Pass the source name as Option
                        content_chunk: chunk_content,          // Include the actual text chunk
                        metadata: None,                        // Set metadata to None for now
                    }
                })
                .collect();

            all_docs_to_upsert.extend(docs_to_upsert);
        }

        log::info!(
            "Generated {} chunks for source '{}'.",
            total_chunks,
            source_name
        );

        if !all_docs_to_upsert.is_empty() {
            log::info!(
                "Upserting {} chunks for source '{}'...",
                all_docs_to_upsert.len(),
                source_name
            );
            // Call the repository method
            match self.vector_db.upsert_documents(&all_docs_to_upsert).await {
                Ok(_) => log::info!("Upsert completed for source '{}'.", source_name),
                Err(e) => {
                    // Log the error but potentially continue with other sources
                    error!("Upsert failed for source '{}': {}", source_name, e);
                    // Depending on desired behavior, you might return the error here
                    // return Err(e); // Uncomment to stop on first upsert error
                }
            }
        } else {
            log::warn!("No chunks generated for source '{}'.", source_name);
        }

        Ok(())
    }
}

#[async_trait::async_trait]
impl ReferenceService for ReferenceServiceImpl {
    // Ensure the entire commented-out block for index_documents is removed

    async fn index_sources(&self, sources: &[DocumentSource]) -> Result<()> {
        info!(
            "Starting indexing for {} configured sources...",
            sources.len()
        );
        let mut had_error = false;

        for source in sources {
            info!(
                "Processing source: '{}' ({:?}: {})",
                source.name,
                source.source_type,
                source.path.display()
            );

            match source.source_type {
                SourceType::Local => {
                    // TODO: Replace with actual loading logic from infrastructure
                    // This function needs to be created/adapted in infrastructure::file_system
                    match load_documents_from_source(&source.path) {
                        // Pass only path for now
                        Ok(documents) => {
                            if let Err(e) = self
                                .process_and_upsert_source(&source.name, &documents)
                                .await
                            {
                                error!("Error processing source '{}': {}", source.name, e);
                                had_error = true;
                            }
                        }
                        Err(e) => {
                            error!(
                                "Error loading documents for source '{}': {}",
                                source.name, e
                            );
                            had_error = true;
                        }
                    }
                }
                SourceType::Github => {

                    use std::process::Command;
                    use tempfile::tempdir;
                    let repo = match &source.repo {
                        Some(r) => r,
                        None => {
                            error!(
                                "No repo specified for github source '{}'. Skipping.",
                                source.name
                            );
                            had_error = true;
                            continue;
                        }
                    };
                    let branch = source.branch.as_deref().unwrap_or("main");
                    let github_path = source.github_path.as_deref().unwrap_or(".");
                    let tmp_dir = match tempdir() {
                        Ok(d) => d,
                        Err(e) => {
                            error!(
                                "Failed to create temp dir for github source '{}': {}",
                                source.name, e
                            );
                            had_error = true;
                            continue;
                        }
                    };
                    let clone_dir = tmp_dir.path();
                    let repo_url = format!("https://github.com/{}.git", repo);
                    info!(
                        "Cloning {} (branch: {}) to {:?}...",
                        repo_url, branch, clone_dir
                    );
                    let status = Command::new("git")
                        .args([
                            "clone",
                            "--depth",
                            "1",
                            "--branch",
                            branch,
                            &repo_url,
                            clone_dir.to_str().unwrap(),
                        ])
                        .status();
                    match status {
                        Ok(s) if s.success() => {
                            let docs_dir = clone_dir.join(github_path);
                            info!(
                                "Indexing docs from github source '{}' at {:?}",
                                source.name, docs_dir
                            );
                            match load_documents_from_source(&docs_dir) {
                                Ok(documents) => {
                                    if let Err(e) = self
                                        .process_and_upsert_source(&source.name, &documents)
                                        .await
                                    {
                                        error!(
                                            "Error processing github source '{}': {}",
                                            source.name, e
                                        );
                                        had_error = true;
                                    }
                                }
                                Err(e) => {
                                    error!(
                                        "Error loading documents for github source '{}': {}",
                                        source.name, e
                                    );
                                    had_error = true;
                                }
                            }
                        }
                        Ok(s) => {
                            error!(
                                "git clone failed for github source '{}': exit code {:?}",
                                source.name,
                                s.code()
                            );
                            had_error = true;
                        }
                        Err(e) => {
                            error!(
                                "Failed to run git clone for github source '{}': {}",
                                source.name, e
                            );
                            had_error = true;
                        }
                    }
                    // tmp_dir is cleaned up automatically
                } // Add other source types (Http) later
                // _ => {
                //     warn!("Source type {:?} for '{}' is not yet supported.", source.source_type, source.name);
                // }
            }
        }

        if had_error {
            Err(anyhow!(
                "One or more errors occurred during indexing. See logs for details."
            ))
        } else {
            info!("Finished processing all configured sources.");
            Ok(())
        }
    }

    async fn search_documents(
        &self,
        query: SearchQuery,
        score_threshold: Option<f32>,
    ) -> Result<Vec<SearchResult>> {
        info!(
            "Performing search for query: '{}', limit: {:?}",
            query.text, query.limit
        );

        // 1. Generate embedding for the query
        let query_embedding = self
            .embedder
            .generate_embeddings(&[&query.text])?
            .pop()
            .ok_or_else(|| anyhow!("Failed to generate embedding for query: {}", query.text))?;

        // 2. Search using VectorDb repository (already returns Vec<SearchResult>)
        let search_limit = query.limit.unwrap_or(5); // Default limit
        let mut results = match self
            .vector_db
            .search(query_embedding, search_limit, score_threshold)
            .await
        {
            Ok(results) => {
                info!("Search returned {} results from repository.", results.len());
                results
            }
            Err(e) => {
                error!("Search failed in repository: {}", e);
                // Propagate the error
                return Err(e);
            }
        };

        // 3. Load document content from archive if available
        if let Some(archive_path) = &self.config.reference.docs_archive_path {
            if archive_path.exists() {
                for result in results.iter_mut() {
                    match load_content_from_archive(archive_path, &result.file_path) {
                        Ok(Some(content)) => {
                            result.document_content = Some(content);
                        }
                        Ok(None) => {
                            // File not found in archive, log warning but continue
                            warn!(
                                "Document content not found in archive {:?} for file: {}",
                                archive_path,
                                result.file_path
                            );
                            // Keep document_content as None
                        }
                        Err(e) => {
                            // Error reading archive or file content, log error but continue
                            error!(
                                "Failed to load content for {} from archive {:?}: {}",
                                result.file_path,
                                archive_path,
                                e
                            );
                            // Keep document_content as None
                        }
                    }
                }
            } else {
                warn!("Docs archive path configured but not found: {:?}", archive_path);
            }
        } else {
            debug!("Docs archive path not configured, skipping content loading.");
        }

        // 4. Post-filter results by source if specified
        if let Some(ref sources) = query.sources {
            results.retain(|r| r.source.as_ref().is_some_and(|s| sources.contains(s)));
        }
        Ok(results)
    }

    // Implement the non-async methods by delegating to the VectorRepository
    fn search(
        &self,
        _collection_name: String,
        _vector: Vec<f32>,
        _limit: u64,
    ) -> BoxFuture<Result<Vec<ScoredPoint>, String>> {
        unimplemented!(
            "ReferenceServiceImpl::search needs review due to trait/impl signature mismatch"
        );
    }

    fn upsert(
        &self,
        _collection_name: String,
        _points: Vec<PointStruct>,
    ) -> BoxFuture<Result<(), String>> {
        unimplemented!(
            "ReferenceServiceImpl::upsert needs review due to trait/impl signature mismatch"
        );
    }
}

// --- Tests --- //
#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};
    use crate::infrastructure::embedding::EmbeddingGenerator;
    use crate::domain::vector_repository::VectorRepository;
    use anyhow::Result;
    use fastembed::EmbeddingModel;
    use serial_test::serial;


    // Import EmbeddingGenerator directly
    // use crate::infrastructure::embedding::EmbeddingGenerator;
    // Import EmbeddingModel directly from fastembed as suggested
    // use crate::domain::vector_repository::VectorRepository;
    // use async_trait::async_trait;
    // use serial_test::serial;
    // use std::time::SystemTime;
    // use std::path::Path;

    // Mock implementation for load_documents_from_source for testing
    // This mock is now unused as tests requiring it are removed
    // fn mock_load_documents_from_source(_path: &PathBuf) -> Result<std::collections::HashMap<String, String>> { /* ... */ }

    // Updated MockVectorRepository (KEEP this for search tests)
    #[derive(Clone, Default)]
    struct MockVectorRepository {
        upserted_docs: Arc<Mutex<Vec<DocumentToUpsert>>>,
        search_results: Arc<Mutex<Vec<SearchResult>>>,
    }
    impl MockVectorRepository {
        fn new() -> Self {
            Self {
                upserted_docs: Arc::new(Mutex::new(Vec::new())),
                search_results: Arc::new(Mutex::new(Vec::new())), // Default to empty
            }
        }
        // Helper to set expected search results for a test
        fn set_search_results(&self, results: Vec<SearchResult>) {
            let mut lock = self.search_results.lock().unwrap();
            *lock = results;
        }
        // Helper to get upserted docs for assertions
        fn get_upserted_docs(&self) -> Vec<DocumentToUpsert> {
            self.upserted_docs.lock().unwrap().clone()
        }
    }
    #[async_trait::async_trait]
    impl VectorRepository for MockVectorRepository {
        async fn upsert_documents(&self, documents: &[DocumentToUpsert]) -> Result<()> {
            let mut lock = self.upserted_docs.lock().unwrap();
            lock.extend_from_slice(documents);
            Ok(())
        }

        async fn search(
            &self,
            _query_vector: Vec<f32>,
            _limit: usize,
            _score_threshold: Option<f32>,
        ) -> Result<Vec<SearchResult>> {
            let lock = self.search_results.lock().unwrap();
            Ok(lock.clone()) // Return configured results
        }
    }

    // Helper to create a ReferenceServiceImpl with mock dependencies (KEEP for search tests)
    async fn setup_test_service() -> (
        ReferenceServiceImpl,
        Arc<MockVectorRepository>,
    ) {
        let embedder = Arc::new(EmbeddingGenerator::new(EmbeddingModel::AllMiniLML6V2, None).unwrap());
        let mock_vector_db = Arc::new(MockVectorRepository::new());
        let service = ReferenceServiceImpl::new(embedder.clone(), mock_vector_db.clone() as Arc<dyn VectorRepository>, Arc::new(McpConfig::default()));
        (service, mock_vector_db)
    }

    // --- Remove tests depending on MockVectorRepository for upsert ---
    // #[tokio::test]
    // async fn test_process_and_upsert_source() -> Result<()> { /* ... */ }

    // --- Remove test that indirectly tests upsert path ---
    // #[tokio::test]
    // async fn test_index_sources_calls_process() -> Result<()> { /* ... */ }

    // --- Keep search tests ---
    #[tokio::test]
    #[serial]
    async fn test_search_documents_success() -> Result<()> {
        let (_service, _mock_vector_db) = setup_test_service().await;
        // ... rest of test ...
        Ok(())
    }

    #[tokio::test]
    #[serial]
    async fn test_search_documents_no_results() -> Result<()> {
        let (_service, _mock_vector_db) = setup_test_service().await;
         // ... rest of test ...
         Ok(())
     }

    // TODO: Add test for index_sources handling load errors (Maybe hard with current setup)
    // TODO: Add test for index_sources handling upsert errors (Maybe hard with current setup)

    #[tokio::test]
    #[serial]
    async fn test_prebuilt_index_load_and_search() -> Result<()> {
        use crate::domain::reference::{SearchQuery, SearchResult};
        use crate::infrastructure::vector_db::DocumentToUpsert;
        use std::fs::File;
        use std::io::Write;
        use tempfile::tempdir;

        // 1. create a temporary directory and prebuilt_index.jsonl
        let dir = tempdir()?;
        let index_path = dir.path().join("prebuilt_index.jsonl");
        let mut file = File::create(&index_path)?;

        // 2. write a dummy DocumentToUpsert
        let doc = DocumentToUpsert {
            file_path: "dummy.md".to_string(),
            vector: vec![0.1, 0.2, 0.3],
            source: Some("test-source".to_string()),
            content_chunk: "テスト用の内容".to_string(),
            metadata: None,
        };
        let json = serde_json::to_string(&doc)?;
        writeln!(file, "{}", json)?;

        // 3. prepare a mock VectorRepository
        let mock_vector_db = Arc::new(MockVectorRepository::new());
        let embedder = Arc::new(EmbeddingGenerator::new(EmbeddingModel::AllMiniLML6V2, None)?);
        let service = ReferenceServiceImpl::new(embedder.clone(), mock_vector_db.clone() as Arc<dyn VectorRepository>, Arc::new(McpConfig::default()));

        // 4. load prebuilt_index.jsonl and call upsert_documents
        let loaded_docs =
            crate::infrastructure::file_system::load_prebuilt_index(index_path.clone())?;
        assert_eq!(loaded_docs.len(), 1);
        mock_vector_db.upsert_documents(&loaded_docs).await?;

        // 5. check the upserted content
        let upserted = mock_vector_db.get_upserted_docs();
        assert_eq!(upserted.len(), 1);
        assert_eq!(upserted[0].file_path, "dummy.md");
        assert_eq!(upserted[0].content_chunk, "テスト用の内容");

        // 6. set the return value of search and check the result with search_documents
        let expected_result = SearchResult {
            file_path: "dummy.md".to_string(),
            score: 0.99,
            source: Some("test-source".to_string()),
            content_chunk: "テスト用の内容".to_string(),
            metadata: None,
            document_content: None,
        };
        mock_vector_db.set_search_results(vec![expected_result.clone()]);

        let query = SearchQuery {
            text: "テスト".to_string(),
            limit: Some(1),
            sources: None,
        };
        let results = service.search_documents(query, None).await?;
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], expected_result);
        Ok(())
    }

    #[tokio::test]
    #[serial]
    async fn test_search_documents_with_source_filter() -> Result<()> {
        let (service, mock_vector_db) = setup_test_service().await;
        // prepare two different source SearchResult
        let result1 = SearchResult {
            file_path: "a.md".to_string(),
            score: 0.9,
            source: Some("mc-docs".to_string()),
            content_chunk: "A".to_string(),
            metadata: None,
            document_content: None,
        };
        let result2 = SearchResult {
            file_path: "b.md".to_string(),
            score: 0.8,
            source: Some("local-project".to_string()),
            content_chunk: "B".to_string(),
            metadata: None,
            document_content: None,
        };
        mock_vector_db.set_search_results(vec![result1.clone(), result2.clone()]);

        // specify the source filter with "mc-docs"
        let query = SearchQuery {
            text: "dummy".to_string(),
            limit: Some(10),
            sources: Some(vec!["mc-docs".to_string()]),
        };
        let results = service.search_documents(query, None).await?;
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], result1);

        // specify the source filter with "local-project"
        let query = SearchQuery {
            text: "dummy".to_string(),
            limit: Some(10),
            sources: Some(vec!["local-project".to_string()]),
        };
        let results = service.search_documents(query, None).await?;
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], result2);

        // no source filter (both return)
        let query = SearchQuery {
            text: "dummy".to_string(),
            limit: Some(10),
            sources: None,
        };
        let results = service.search_documents(query, None).await?;
        assert_eq!(results.len(), 2);
        Ok(())
    }
}