# droreli/mcpdocsearch architecture

> Architecture diagram of the GitHub repository droreli/mcpdocsearch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/droreli/mcpdocsearch
- Repository: https://github.com/droreli/mcpdocsearch

## Overview

##### Project type and purpose
- This is an open-source documentation crawler and search server. It consists of two main pieces:
a. A CLI tool (`crawler_cli/`) that crawls web pages, cleans HTML, and produces consolidated Markdown files in `./storage/`.
b. An MCP server (`mcp_server/`) that loads those Markdown files, chunks them by headings, generates vector embeddings, caches results, and exposes search/listing operations over the Model Context Protocol (MCP).

##### File structure
- Top-level:
- `crawl.py` – entry point to the crawler.
- `crawler_cli/` – Python package with modules:
– `main.py` (Typer CLI definitions),
– `crawler.py` (crawl4ai integration),
– `markdown.py` (HTML cleaning & Markdown conversion),
– `config.py` and `utils.py` (configuration & helpers).
- `mcp_server/` – Python package with modules:
– `main.py` (server startup),
– `app.py` (FastMCP app wiring),
– `data_loader.py` (load/cache `.md` files and embeddings),
– `mcp_tools.py` (defines MCP methods: list/search/headings),
– `search.py` (vector similarity search),
– `config.py` (server configuration).
- `pyproject.toml` & `uv.lock` – dependency and execution management via `uv`.
- `.cursor/mcp.json` – Cursor client configuration.

##### Key architectural insights from README
- Two-stage workflow: Crawl → Store → Load → Chunk → Embed → Serve.
- Caching layer in `data_loader.py` invalidates on Markdown file changes.
- External libraries: `crawl4ai` for crawling, `BeautifulSoup` for HTML cleaning, `sentence-transformers` & `torch` for embeddings, `fastmcp` for MCP protocol, `typer` for CLI.

a. Main components
- User / Developer
- CLI Layer (`crawl.py` & `crawler_cli` package)
- Crawl Engine (`crawl4ai`)
- HTML Cleaner & Markdown Converter (`crawler_cli/markdown.py`)
- Storage (local filesystem: `./storage/`)
- MCP Server (`mcp_server` package)
- Cache Store (`storage/document_chunks_cache.pkl`)
- Embedding Service (`sentence-transformers` + `torch`)
- MCP API Layer (`fastmcp`)
- MCP Client (Cursor or Claude) via stdio transport
b. Relationships & interactions
1. User invokes `uv run python crawl.py <URL>` → CLI Layer
2. CLI Layer calls Crawl Engine → fetches pages → HTML Cleaner
3. Cleaned HTML → Markdown Converter → writes Markdown to `./storage/`
4. User or Cursor client starts MCP Server (`uv run python -m mcp_server.main`)
5. On startup: Data Loader checks cache file; if stale or missing, reads `.md` files → splits into chunks → sends text to Embedding Service → collects vectors → writes cache. If cache valid, loads from pickle.
6. MCP Server (FastMCP app) registers tools (`list_documents`, `get_document_headings`, `search_documentation`).
7. MCP Client issues JSON-RPC calls over stdio → MCP Server → responds with document lists, heading trees, or search results (semantic similarity scores + chunk content).
c. Architectural patterns & design principles
- Separation of concerns: Crawling vs. serving/search.
- Layered design: CLI → Engine → Storage → Server → Client.
- Caching pattern: File-based invalidation.
- Microservice-style boundary between crawler tool and search server (communicates via filesystem + caching).
d. Key technologies & libraries
- Python packages: `typer`, `crawl4ai`, `beautifulsoup4`, `sentence-transformers`, `torch`, `fastmcp`.
- Dependency manager: `uv` & `pyproject.toml`.
- Protocol: Model Context Protocol (MCP) over stdio.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Crawler Subsystem
    subgraph "Crawler Subsystem"
        direction TB
        CPE["crawl.py"]:::code
        subgraph "CLI Layer"
            direction TB
            CLI_main["main.py"]:::code
            CLI_config["config.py"]:::code
            CLI_utils["utils.py"]:::code
        end
        CrawlInt["crawler.py\n(crawl4ai)"]:::external
        Clean_conv["markdown.py\n(HTML Cleaner & Markdown Converter)"]:::code
        Storage["./storage/\n(Markdown Files)"]:::storage

        CPE --> CLI_main
        CLI_main --> CLI_config
        CLI_main --> CLI_utils
        CLI_main --> CrawlInt
        CrawlInt --> Clean_conv
        Clean_conv --> Storage
    end

    %% MCP Server Subsystem
    subgraph "MCP Server Subsystem"
        direction TB
        ServerStart["main.py\n(Server Startup)"]:::code
        AppWiring["app.py\n(FastMCP App)"]:::code
        ConfigSrv["config.py\n(Server Configuration)"]:::code
        DataLoader["data_loader.py\n(Data Loader & Cache)"]:::code
        MCPtools["mcp_tools.py\n(MCP Methods)"]:::code
        SearchLogic["search.py\n(Vector Similarity Search)"]:::code
        EmbService["Embeddings Service\n(sentence-transformers, torch)"]:::external
        Cache["document_chunks_cache.pkl\n(Cache Store)"]:::storage
        ServerClient["MCP Client\n(.cursor/mcp.json)"]:::external

        ServerClient -->|"JSON-RPC over stdio"| AppWiring
        ServerStart --> AppWiring
        AppWiring --> MCPtools
        MCPtools --> DataLoader
        MCPtools --> SearchLogic
        SearchLogic --> DataLoader
        DataLoader -->|"if cache valid"| Cache
        DataLoader -->|"load .md files"| Storage
        DataLoader -->|"chunk & embed"| EmbService
        EmbService -->|"vectors"| DataLoader
        DataLoader -->|"write cache"| Cache
    end

    %% Project Configuration
    PDeps["pyproject.toml"]:::external
    Lock["uv.lock"]:::external
    PDeps --> CPE
    Lock --> CPE

    %% Click Events
    click CPE "https://github.com/droreli/mcpdocsearch/blob/main/crawl.py"
    click CLI_main "https://github.com/droreli/mcpdocsearch/blob/main/crawler_cli/main.py"
    click CLI_config "https://github.com/droreli/mcpdocsearch/blob/main/crawler_cli/config.py"
    click CLI_utils "https://github.com/droreli/mcpdocsearch/blob/main/crawler_cli/utils.py"
    click CrawlInt "https://github.com/droreli/mcpdocsearch/blob/main/crawler_cli/crawler.py"
    click Clean_conv "https://github.com/droreli/mcpdocsearch/blob/main/crawler_cli/markdown.py"
    click ServerStart "https://github.com/droreli/mcpdocsearch/blob/main/mcp_server/main.py"
    click AppWiring "https://github.com/droreli/mcpdocsearch/blob/main/mcp_server/app.py"
    click ConfigSrv "https://github.com/droreli/mcpdocsearch/blob/main/mcp_server/config.py"
    click DataLoader "https://github.com/droreli/mcpdocsearch/blob/main/mcp_server/data_loader.py"
    click MCPtools "https://github.com/droreli/mcpdocsearch/blob/main/mcp_server/mcp_tools.py"
    click SearchLogic "https://github.com/droreli/mcpdocsearch/blob/main/mcp_server/search.py"
    click PDeps "https://github.com/droreli/mcpdocsearch/blob/main/pyproject.toml"
    click Lock "https://github.com/droreli/mcpdocsearch/blob/main/uv.lock"
    click ServerClient "https://github.com/droreli/mcpdocsearch/blob/main/.cursor/mcp.json"

    %% Styles
    classDef code fill:#e5ffe5,stroke:#2d762e;
    classDef external fill:#e5f0ff,stroke:#1a3a6e;
    classDef storage fill:#ffffcc,stroke:#7a6600;
```
