Overview
This guide explains the architectural patterns and design principles used throughout the Grounded Docs MCP Server. Read ARCHITECTURE.md in the source repository for the complete system architecture.Core Architectural Patterns
Tools Layer Pattern
Business logic resides in the tools layer to enable code reuse across interfaces. Why: CLI commands, MCP endpoints, and web routes should all use the same logic. Structure:Write-Through Architecture
Pipeline jobs serve as the single source of truth, with all state changes immediately persisted to the database. Benefits:- Enables recovery after crashes
- Provides real-time progress tracking
- Ensures consistency between memory and storage
Functionality-Based Design
Components are selected based on capability requirements, not deployment context. Pattern:Protocol Abstraction
Transport layer abstracts stdio vs HTTP differences, enabling identical functionality across access methods. Auto-detection:System Architecture Modes
Unified Mode (In-Process)
All components run in a single process. When to use:- Development environment
- Simple deployments
- Single-user scenarios
Distributed Mode (Hub & Spoke)
Separate coordinator and worker processes. When to use:- Production deployments
- Scaling workload across containers
- Multiple coordinators sharing one worker
Content Processing Architecture
Content processing follows a modular pipeline:Scraper Strategies
Handle different source types:Content Pipelines
Transform content using middleware chains:Document Splitters
Two-phase splitting approach: Phase 1: Semantic SplittingEvent-Driven Architecture
The EventBus decouples producers from consumers:Storage Architecture
Normalized SQLite schema with three core tables:Configuration System
Configuration resolves once per process with strict validation:Design Principles
Single Responsibility
Each component has one clear purpose:- PipelineManager: Job queue and coordination
- PipelineWorker: Job execution
- EventBus: Event distribution
- DocumentService: Document CRUD operations
Dependency Injection
Services receive dependencies through constructors:Interface Segregation
Use focused interfaces:Composition Over Inheritance
Prefer composition:Extension Points
To add new functionality:New Content Source
- Create strategy in
src/scraper/strategies/ - Implement
BaseScraperStrategyinterface - Register in strategy factory
New Tool
- Create tool in
src/tools/ - Add validation and business logic
- Expose in CLI, MCP, and Web interfaces
New Embedding Provider
- Add provider configuration
- Implement LangChain embeddings interface
- Register in embeddings factory
Best Practices
Layered Architecture
Layered Architecture
- Interfaces delegate to Tools
- Tools implement business logic
- Services handle data operations
- Keep layers separate
Event-Driven Updates
Event-Driven Updates
- Use EventBus for decoupling
- Emit events at state boundaries
- Subscribe for real-time updates
- Don’t poll for status
Write-Through State
Write-Through State
- Update memory and DB together
- Enable crash recovery
- Maintain single source of truth
- Emit events after persistence
Configuration
Configuration
- Use Zod for validation
- Load once per process
- Support environment variables
- Provide sensible defaults
Reading the Source
Key files to understand the architecture:- ARCHITECTURE.md - Complete system architecture
- src/app/AppServer.ts - Service composition
- src/pipeline/PipelineFactory.ts - Mode selection
- src/tools/ - Business logic layer
- src/store/ - Data persistence
Next Steps
Getting Started
Set up your development environment
Code Style Guide
Learn code conventions
