Skip to main content
Glama
README.mdβ€’17.1 kB
# Architecture Overview This document describes the system architecture, design patterns, and component interactions of the Local Search MCP Server. ## πŸ—οΈ System Architecture ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ LOCAL SEARCH MCP SERVER - EVENT LOOP ARCHITECTURE β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ MCP Layer β”‚ β”‚ Service β”‚ β”‚ Persistence β”‚ β”‚ External β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Orchestration β”‚ β”‚ Layer β”‚ β”‚ Services β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚ MCP Server β”‚ β”‚ β”‚ β”‚ Search β”‚ β”‚ β”‚ β”‚ Vector β”‚ β”‚ β”‚ β”‚ GitHub β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Interface β”‚β—„β–Ίβ”‚ β”‚ β”‚ Service β”‚β—„β–Ίβ”‚ β”‚ β”‚ Index │◄┼─┼►│ Repos β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β€’ Tool Schema β”‚ β”‚ β”‚ Repo β”‚ β”‚ β”‚ β€’ SQLite β”‚ β”‚ β”‚ File β”‚ β”‚ β”‚ β”‚ β”‚ β€’ Request/Resp β”‚ β”‚ β”‚ Service β”‚β—„β–Ίβ”‚ β”‚ β€’ Embeddingsβ”‚ β”‚ β”‚ URLs β”‚ β”‚ β”‚ β”‚ β”‚ β€’ Error Handlingβ”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β€’ Chunks β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ File β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Download β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Service β”‚β—„β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ EVENT LOOP PROCESSING ENGINE β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚ File β”‚ β”‚ Text β”‚ β”‚ Embedding β”‚ β”‚ Vector β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ Processor │─►│ Chunker │─►│ Service │─►│ Storage β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ (Async I/O) β”‚ β”‚ (Yielding) β”‚ β”‚ (Batched) β”‚ β”‚ (ACID) β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β”‚ EVENT LOOP EVENT LOOP TRANSACTION β”‚ β”‚ β”‚ Text Extract Yielding Tokenize Batched Yield Similarity Storage β”‚ β”‚ β”‚ .txt,.md Every 1K & Clean Every 10 Search Operations β”‚ β”‚ β”‚ .json,.js Chunks Process Batches Engine (CRUD) β”‚ β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ CONCURRENCY MODEL β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β€’ NO THREADING - Simple async functions with setImmediate() yielding β”‚ β”‚ β”‚ β”‚ β€’ MULTIPLE JOBS - Concurrent background processing β”‚ β”‚ β”‚ β”‚ β€’ MCP RESPONSIVE - Never blocks main thread despite long operations β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## πŸ“Š Component Overview ### 🎯 MCP Layer (Protocol Interface) **Purpose**: Handle MCP protocol communication and tool exposition | Component | Responsibility | Key Features | |-----------|----------------|--------------| | **MCP Server** | - Tool registration and schema management<br>- Request/response routing<br>- Error formatting and handling<br>- Stdio protocol compliance | - Type-safe tool definitions<br>- Request validation<br>- Graceful error recovery | | **Tool Handlers** | - Parameter validation and transformation<br>- Service method orchestration<br>- Response formatting | - Input sanitization<br>- Result aggregation<br>- Progress indication | ### βš™οΈ Service Orchestration Layer **Purpose**: Coordinate complex operations across multiple subsystems | Component | Responsibility | Key Features | |-----------|----------------|--------------| | **SearchService** | - Primary API for indexing/searching<br>- Pipeline orchestration<br>- Result ranking and filtering<br>- Error aggregation | - Parallel processing with p-limit<br>- Memory-efficient batching<br>- Comprehensive error handling | | **BackgroundProcessor** | - Asynchronous file processing<br>- Repository downloading via repomix<br>- HTTP file downloads with progress<br>- Event loop yielding for MCP responsiveness | - setImmediate() yielding<br>- Batch embedding processing<br>- Job progress tracking | ### πŸ’Ύ Persistence Layer **Purpose**: Store and retrieve vector embeddings and document metadata | Component | Responsibility | Key Features | |-----------|----------------|--------------| | **VectorIndex** | - Vector storage and retrieval<br>- Similarity search algorithms<br>- Database management | - SQLite optimization<br>- Cosine similarity<br>- Metadata tracking<br>- Index versioning | ### πŸš€ Core Processing Engine **Purpose**: Execute the document processing pipeline with performance optimization | Component | Responsibility | Key Features | |-----------|----------------|--------------| | **FileProcessor** | - Multi-format text extraction<br>- Character encoding handling<br>- Size validation | - 8 file types supported<br>- Content sanitization<br>- Memory limits | | **TextChunker** | - Intelligent document segmentation<br>- Overlap management<br>- Content preservation | - Event loop yielding<br>- Configurable chunk sizes<br>- Token counting | | **EmbeddingService** | - Vector generation using transformers<br>- GPU acceleration<br>- Batch processing | - Single model instance<br>- Memory optimization<br>- Error recovery | | **ProgressManager** | - Job progress tracking<br>- Callback registration<br>- Event loop yielding | - Real-time progress updates<br>- setImmediate() yielding<br>- Background coordination | ### 🎯 Job Management Layer **Purpose**: Handle background processing jobs and progress reporting | Component | Responsibility | Key Features | |-----------|----------------|--------------| | **JobManager** | - Job lifecycle management<br>- Status tracking<br>- Result storage | - In-memory job storage<br>- Progress callbacks<br>- Error aggregation | ## πŸ”„ Data Flow Architecture ### Document Processing Pipeline ```mermaid graph TD A[File Discovery] --> B[File Selection] B --> C[Text Extraction] C --> D[Document Chunking] D --> E[Embedding Generation] E --> F[Vector Storage] F --> G[Index Complete] H[MCP Request] --> I[Query Processing] I --> J[Vector Similarity] J --> K[Result Ranking] K --> L[Response Formatting] ``` ### Detailed Processing Flow #### 1. Content Ingestion Phase ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ File System │───▢│ File Scan │───▢│ File Filter β”‚ β”‚ Discovery β”‚ β”‚ (Recursive) β”‚ β”‚ (Patterns) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β–Ό β–Ό ``` #### 2. Content Processing Phase ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ File Reader │───▢│ Text Extract │───▢│ Content β”‚ β”‚ (Buffered) β”‚ β”‚ (Format-aware)β”‚ β”‚ Validator β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β–Ό β–Ό ``` #### 3. Chunking and Embedding Phase ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Text Chunker │───▢│ Token Counter │───▢│ Embedding β”‚ β”‚ (Semantic) β”‚ β”‚ (Statistics) β”‚ β”‚ Generator β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β–Ό β–Ό ``` #### 4. Storage and Search Phase ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Vector Index │───▢│ Similarity │───▢│ Result Rank β”‚ β”‚ (SQLite) β”‚ β”‚ Search β”‚ β”‚ & Filter β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β–Ό β–Ό ``` ### File Watcher Integration ```mermaid graph TD A[File System] --> B[chokidar Watcher] B --> C{File Event} C --> D[add/change/unlink] D --> E[Queue Processing] E --> F[p-limit Concurrency] F --> G[Process File] G --> H[Update Index] H --> I[Index Updated] ``` ## πŸ›οΈ Design Principles ### **Separation of Concerns** - Each service handles exactly one responsibility - Clean interfaces between layers - No business logic in protocol layer ### **Performance Optimization** - Parallel processing with concurrency limits - Memory-efficient streaming operations - Lazy initialization and resource pooling - Optimized database queries with proper indexing ### **Error Resilience** - Comprehensive error handling at each layer - Graceful degradation for partial failures - Detailed error logging and debugging - Recovery mechanisms for transient failures ### **Scalability Architecture** - Stateless service design - Horizontal scaling potential - Resource usage monitoring - Configuration-driven performance tuning ## πŸ”§ Key Technical Decisions ### **Embedding Model Selection** - **Choice**: `@xenova/transformers` (JavaScript-native) - **Rationale**: - No Python dependency - Client-side compatible - Smaller footprint than full PyTorch models - GPU acceleration support ### **Database Choice** - **Choice**: SQLite with better-sqlite3 - **Rationale**: - Single-file database - ACID transactions - Excellent performance for read-heavy workloads - No additional services required ### **Concurrency Strategy** - **Choice**: p-limit for controlled parallelism - **Rationale**: - Prevents resource exhaustion - Configurable per operation type - Backpressure handling - Memory usage control ### **File Watching** - **Choice**: chokidar with custom patterns - **Rationale**: - Cross-platform filesystem events - Configurable ignore patterns - Event debouncing - Memory-efficient filtering ## πŸ“ˆ Performance Characteristics ### **Scalability Metrics** | Component | Scaling Factor | Bottleneck | Optimization Strategy | |-----------|----------------|------------|----------------------| | **File Processing** | Linear w/ CPU cores | CPU embedding | p-limit concurrency | | **Directory Scanning** | Quadratic w/ depth | I/O operations | Parallel traversal | | **Vector Search** | Logarithmic w/ docs | Memory β†’ Storage | SQLite indexes | | **Storage Operations** | Linear w/ writes | Disk I/O | Transaction batching | ### **Memory Usage Patterns** ``` Low Load (100 docs): β”œβ”€β”€ Embedding models: ~500MB β”œβ”€β”€ Document cache: ~50MB β”œβ”€β”€ Working memory: ~100MB └── Total: ~650MB High Load (10K docs): β”œβ”€β”€ Embedding models: ~500MB β”œβ”€β”€ Document cache: ~500MB β”œβ”€β”€ Working memory: ~200MB └── Total: ~1.2GB ``` ### **CPU Utilization** - **Single file processing**: ~5-10% CPU per file - **Batch processing**: Scales linearly with concurrency - **Search operations**: <1% CPU per query - **Background monitoring**: <1% continuous CPU ## πŸ”„ Asynchronous Patterns ### **Promise-based Processing** - All I/O operations are async - Proper error propagation through chains - Cancellation support via AbortController ### **Streaming Operations** ```typescript // Large file processing with memory limits const stream = fs.createReadStream(filePath, { highWaterMark: 64 * 1024 }); stream.pipe(transformer).pipe(output); ``` ### **Batch Processing** ```typescript // Embedding generation with batching const batches = chunkArray(embeddings, batchSize); for (const batch of batches) { const vectors = await model.generate(batch); await index.store(vectors); } ``` ## πŸ› οΈ Extensibility Architecture ### **Plugin System Design** - Interface-based component registration - Configurable processing pipelines - Hot-swappable implementations ### **Configuration Driven** - Runtime parameter adjustment - Environment variable support - Profile-based optimization ### **API Compatibility** - Versioned interfaces - Backward compatibility preservation - Migration path support ## πŸ§ͺ Testing Architecture ### **Unit Testing Context** - Isolated component testing - Mock external dependencies - Performance regression monitoring ### **Integration Testing** - End-to-end pipeline validation - Performance benchmarking - Resource usage monitoring ### **File Format Testing** - Comprehensive format coverage - Edge case handling - Performance validation --- See [Performance Guide](../performance/) for optimization details and [Usage Guide](../usage/) for integration patterns.

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/PatrickRuddiman/local-search-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server