|
1 | | -# Architecture Documentation |
| 1 | +# System Architecture |
2 | 2 |
|
3 | | -This document provides a detailed overview of the architecture of the CodeGraphContext project. |
| 3 | +CodeGraphContext (CGC) is a **context-aware code intelligence engine** that bridges the gap between your source code and your AI tools. |
4 | 4 |
|
5 | | -## High-Level Overview |
| 5 | +It operates primarily as a background service (MCP Server) backed by a graph database, with a CLI for management. |
6 | 6 |
|
7 | | -The project is a client-server application designed to analyze and visualize codebases. It consists of: |
| 7 | +## High-Level Diagram |
8 | 8 |
|
9 | | -* **A Python backend:** This is the core of the application, responsible for parsing and analyzing code, building a graph representation of the codebase, and exposing this data through an API. |
10 | | -* **A web-based frontend:** A user interface for interacting with the backend, visualizing the code graph, and exploring the codebase. |
11 | | -* **A command-line interface (CLI):** For managing the backend and performing analysis from the terminal. |
| 9 | +```mermaid |
| 10 | +graph TD |
| 11 | + Client[AI Client / CLI] |
| 12 | + Server[MCP Server] |
| 13 | + Graph[Graph Builder] |
| 14 | + DB[(Graph Database)] |
| 15 | + FS[File System] |
12 | 16 |
|
13 | | -## Backend Architecture |
| 17 | + Client -- "1. Query (MCP/CLI)" --> Server |
| 18 | + Server -- "2. Read Graph" --> DB |
| 19 | + Server -- "3. Trigger Index" --> Graph |
| 20 | + Graph -- "4. Scan Code" --> FS |
| 21 | + Graph -- "5. Store Nodes" --> DB |
| 22 | +``` |
14 | 23 |
|
15 | | -The backend is a Python application located in the `src/codegraphcontext` directory. |
| 24 | +## 1. The Core (Backend) |
16 | 25 |
|
17 | | -### Core Components |
| 26 | +The "brain" of the operation lives in `src/codegraphcontext`. It is a pure Python application. |
18 | 27 |
|
19 | | -The `src/codegraphcontext/core` directory contains the fundamental building blocks of the backend: |
| 28 | +| Component | Responsibility | |
| 29 | +| :--- | :--- | |
| 30 | +| **Server (`server.py`)** | Acts as the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) host. It translates JSON requests from Claude/Cursor into database queries. | |
| 31 | +| **Graph Builder** | The "indexer". It parses your code using **Tree-sitter**, extracts dependencies, and builds the knowledge graph. | |
| 32 | +| **Database Layer** | An abstraction layer that talks to either **FalkorDB** (embedded) or **Neo4j** (production). | |
| 33 | +| **Background Jobs** | Handles long-running tasks like "Index the entire repo" or "Watch for file changes" without blocking the AI. | |
| 34 | +| **Watcher** | A file system monitor that triggers incremental re-indexing when you save a file. | |
20 | 35 |
|
21 | | -* **Database:** A graph database is used to store the code graph. This allows for efficient querying of relationships between code elements (e.g., function calls, class inheritance). |
22 | | -* **Jobs:** Asynchronous jobs are used for long-running tasks like indexing a new codebase. This prevents the application from becoming unresponsive. |
23 | | -* **Watcher:** A file system watcher monitors the codebase for changes and triggers re-indexing, keeping the code graph up-to-date. |
| 36 | +## 2. No "Frontend" Application |
24 | 37 |
|
25 | | -### Tools |
| 38 | +**Important:** CodeGraphContext does **not** have a traditional web application frontend. |
| 39 | +The `website/` folder you see in the repository is strictly for **documentation** (this site). |
26 | 40 |
|
27 | | -The `src/codegraphcontext/tools` directory contains the logic for code analysis: |
| 41 | +Instead of a custom UI, we rely on: |
28 | 42 |
|
29 | | -* **Graph Builder:** This component is responsible for parsing the code and building the graph representation that is stored in the database. |
30 | | -* **Code Finder:** Provides functionality to search for specific code elements within the indexed codebase. |
31 | | -* **Import Extractor:** This tool analyzes the import statements in the code to understand dependencies between modules. |
| 43 | +1. **AI Chat Interfaces:** Your existing IDE chat (Cursor, VS Code) is the UI. |
| 44 | +2. **Neo4j Browser:** For advanced users wanting a raw visual exploration of the graph (available at `localhost:7474` if using Neo4j). |
| 45 | +3. **Visualizer Tool:** The CLI can generate standalone HTML files (`cgc visualize`) using Vis.js for quick inspections. |
32 | 46 |
|
33 | | -### Server |
| 47 | +## 3. Data Flow |
34 | 48 |
|
35 | | -The `src/codegraphcontext/server.py` file implements the API server. It exposes the functionality of the backend to the frontend through a JSON-RPC API. |
| 49 | +1. **Indexing:** |
| 50 | + * `cgc index .` -> Scans files -> Parses AST -> Creates Nodes (Functions, Classes) and Edges (CALLS, INHERITS) -> Saves to DB. |
| 51 | +2. **Querying (Natural Language):** |
| 52 | + * User asks "Who calls function X?" -> MCP Server receives request -> Runs Cypher Query (`MATCH (n)-[:CALLS]->(m)...`) -> Returns context to LLM. |
36 | 53 |
|
37 | | -### CLI |
| 54 | +## 4. Key Technologies |
38 | 55 |
|
39 | | -The `src/codegraphcontext/cli` directory contains the implementation of the command-line interface. It allows users to: |
40 | | - |
41 | | -* Start and stop the backend server. |
42 | | -* Index new projects. |
43 | | -* Run analysis tools from the command line. |
44 | | - |
45 | | -## Frontend Architecture |
46 | | - |
47 | | -The frontend is a modern web application located in the `website/` directory. |
48 | | - |
49 | | -* **Framework:** It is built using React and TypeScript. |
50 | | -* **Build Tool:** Vite is used for fast development and building the application. |
51 | | -* **Component-Based:** The UI is organized into reusable components, located in `website/src/components`. This includes UI elements like buttons and dialogs, as well as higher-level components for different sections of the application. |
52 | | -* **Styling:** Tailwind CSS is used for styling the application. |
53 | | - |
54 | | -## Testing |
55 | | - |
56 | | -The `tests/` directory contains the test suite for the project. |
57 | | - |
58 | | -* **Integration Tests:** `test_cgc_integration.py` contains tests that verify the interaction between different components of the backend. |
59 | | -* **Unit Tests:** Other files in this directory contain unit tests for specific modules and functions. |
60 | | -* **Sample Project:** The `tests/sample_project` directory contains a variety of Python files used as input for testing the code analysis tools. |
| 56 | +* **Language:** Python 3.10+ |
| 57 | +* **Parsing:** Tree-sitter (for robust, multi-language support) |
| 58 | +* **Protocol:** Model Context Protocol (MCP) |
| 59 | +* **Database:** FalkorDB (via Redis) or Neo4j |
| 60 | +* **CLI:** Typer / Click |
0 commit comments