# rohityadav34980/legacygraph-mcp architecture

> Architecture diagram of the GitHub repository rohityadav34980/legacygraph-mcp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-03-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rohityadav34980/legacygraph-mcp
- Repository: https://github.com/rohityadav34980/legacygraph-mcp

## Overview

##### What this project is (type + purpose)
**LegacyGraph-MCP** is a **Python 3.11 MCP (Model Context Protocol) server** that lets AI agents query a **C++ codebase as a knowledge graph** instead of raw text. It:
- **Parses C++** via **tree-sitter** to produce an AST (accurate handling of macros/templates vs regex).
- **Builds a dependency/call graph** using **NetworkX**.
- **Exposes MCP tools** over **JSON-RPC** so an external agent/client (Claude Desktop, etc.) can query callers/callees, cycles, orphans, etc.

From the repository:

##### Core runtime (the “product”)
- `src/server.py` → **Interface Layer**: MCP server, JSON-RPC tool endpoints.
- `src/parser.py` → **Ingestion Layer**: tree-sitter parsing of C++ source into AST-derived facts.
- `src/graph.py` → **Graph Layer**: constructs/queries NetworkX graph; cycle detection, callers/callees, orphan detection.
- `src/__init__.py` → package entry glue.

##### Quality gates / validation tooling
- `tools/verifier.py` → integration-style verifier that runs the stack end-to-end and checks “100% accuracy” expectations.
- `tests/test_core.py` → unit tests around core behavior.

##### Build / dependency management
- `pyproject.toml` → Poetry deps (tree-sitter, networkx, MCP libs, pytest, mypy, black, etc.).

##### Documentation
- `README.md`, `PROJECT_MANUAL.md` → architecture + API/tool descriptions and usage.

##### AI Agent / MCP Client
- Claude Desktop / other MCP-compatible client.
- Communicates via **JSON-RPC** to the MCP server.

##### LegacyGraph-MCP Server (Python process)
- Contains three internal subsystems (below).
- Owns tool routing, request validation, response formatting.

##### Interface Layer: MCP Tool Handlers (`src/server.py`)
- Tools: `analyze_codebase`, `get_callers`, `get_callees`, `detect_cycles`, `get_orphan_functions`.
- Converts JSON-RPC requests into internal method calls.
##### Ingestion Layer: C++ Parser (`src/parser.py`)
- **tree-sitter** runtime + C++ grammar usage (show as dependency).
- Outputs extracted entities/relations (functions, calls, files, etc.).
##### Graph Layer: Knowledge Graph Engine (`src/graph.py`)
- **NetworkX graph** construction and algorithms (cycle detection, traversal).
- Query operations used by tool handlers.

##### C++ Codebase on Disk
- The target repository being modernized (in demo, `data/legacy_project/`).
##### AST / Parse Trees (ephemeral)
- Produced by tree-sitter; can be treated as in-memory structures.
##### Dependency Graph (NetworkX, in-memory)
- The central representation the MCP server queries.

##### Poetry Environment / pyproject.toml
- Shows how dependencies are resolved and how server is launched.

##### tree-sitter → Parser: AST nodes/events returned
5. **Parser → Graph Layer**: emits nodes/edges (function definitions, call edges, file ownership, etc.)

##### Secondary flows (engineering lifecycle)
- **Verifier CLI → MCP stack**: runs analysis + compares expected dependencies against `data/legacy_project`
- **Pytest → core modules**: unit tests; can mock parser/graph boundaries
- **Poetry → installs deps / runs server**: `poetry run python -m src.server`

- **Layered architecture (3-layer)**:
- Interface (protocol/tools) → Ingestion (parsing) → Graph (query/algorithms)
- **Graph-RAG / structure-first**:
- The “retrieval” is graph traversal (callers/callees/cycles), not embeddings/vector DB.
- **In-memory compute**:
- Graph and AST are computed at runtime; no persistent DB is shown in this repo.
- **Protocol boundary**:
- MCP/JSON-RPC boundary is a key integration contract—label it clearly.

##### “Don’t overthink it” but be very detailed — how to avoid missing essential pieces
Given this repo, the most common omissions are:
- Forgetting that **tree-sitter is a runtime parsing dependency** (not just a build tool)
- Failing to show the **in-memory graph as a first-class artifact**
- Not separating **tool exposure (MCP)** from **graph queries** (they’re different responsibilities)
- Ignoring **verifier/test harness** even though README treats accuracy as a key product claim

A practical approach:
##### Expand each node into subcomponents based on the file tree
- “MCP Server” → tool handlers in `src/server.py`
- “Parse/tree-sitter” → `src/parser.py` + tree-sitter runtime
- “NetworkX Graph” → `src/graph.py` + in-memory graph + algorithms
##### Engineering loop
- `tools/verifier.py` and `tests/` as separate entrypoints that exercise the server/core.

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% =========================
  %% A) System Context (C4 L1)
  %% =========================
  subgraph "A) System Context (C4 L1)"
    direction TB

    Agent["AI Agent / MCP Client<br/>(Claude Desktop, other MCP clients)"]:::external

    subgraph "LegacyGraph-MCP (Service Boundary)"
      direction TB
      ServerProc["LegacyGraph-MCP Server<br/>(Python 3.11 process)"]:::service
    end

    CodebaseDisk[("C++ Codebase on Disk<br/>(filesystem repository)")]:::data
  end

  Agent -->|"MCP / JSON-RPC (tool invocation)"| ServerProc
  ServerProc -->|"read_files()"| CodebaseDisk
  ServerProc -->|"MCP / JSON-RPC (response)"| Agent

  %% ==========================================
  %% B) Container View (C4 L2, runtime deps)
  %% ==========================================
  subgraph "B) Container View (C4 L2) - Runtime Dependencies"
    direction TB

    subgraph "LegacyGraph-MCP Server Internals"
      direction TB

      ToolRouter["Interface Layer<br/>MCP Tool Router / JSON-RPC Handlers"]:::interface
      Parser["Ingestion Layer<br/>C++ Parser"]:::ingestion
      GraphEngine["Graph Layer<br/>Knowledge Graph Engine"]:::graphLayer

      TS["tree-sitter runtime<br/>(C++ grammar)"]:::thirdparty
      NX["NetworkX library"]:::thirdparty

      AST[("AST / Parse Trees<br/>(ephemeral, in-memory)")]:::data
      KG[("Dependency / Call Graph<br/>(in-memory NetworkX graph)")]:::data
    end

    Poetry["Poetry Environment / Dependency Management"]:::build
  end

  ToolRouter -->|"analyze_codebase()"| Parser
  Parser -->|"parse()"| TS
  TS -.->|"AST nodes/events"| Parser
  Parser -.->|"AST structures"| AST
  Parser -->|"emit_nodes_edges()"| GraphEngine
  GraphEngine -->|"store/query"| NX
  GraphEngine -.->|"nodes/edges"| KG

  ToolRouter -->|"get_callers() / get_callees()<br/>detect_cycles() / get_orphan_functions()"| GraphEngine
  GraphEngine -->|"query_results"| ToolRouter

  Poetry -.->|"installs_deps / runs_server"| ToolRouter
  Poetry -.->|"installs_deps"| Parser
  Poetry -.->|"installs_deps"| GraphEngine
  Poetry -.->|"installs_deps"| TS
  Poetry -.->|"installs_deps"| NX

  %% ===================================================
  %% C) Component View (C4 L3) - Module-level breakdown
  %% ===================================================
  subgraph "C) Component View (C4 L3) - Server Modules & Responsibilities"
    direction TB

    subgraph "src/server.py - Interface Layer"
      direction TB
      HAnalyze["Tool: analyze_codebase"]:::interface
      HCallers["Tool: get_callers"]:::interface
      HCallees["Tool: get_callees"]:::interface
      HCycles["Tool: detect_cycles"]:::interface
      HOrphans["Tool: get_orphan_functions"]:::interface
    end

    subgraph "src/parser.py - Ingestion Layer"
      direction TB
      FileEnum["File Enumerator<br/>(walk codebase, load files)"]:::ingestion
      TSRuntime["tree-sitter Adapter<br/>(init parser, run parse)"]:::ingestion
      ASTWalk["AST Walker"]:::ingestion
      SymExtract["Symbol Extractor<br/>(functions, files, defs)"]:::ingestion
      CallExtract["Call-edge Extractor<br/>(caller -> callee)"]:::ingestion
      FactsOut["Extracted Facts<br/>(entities + relations)"]:::data
    end

    subgraph "src/graph.py - Graph Layer"
      direction TB
      GBuild["Graph Builder<br/>(nodes/edges -> NetworkX)"]:::graphLayer
      GQuery["Query API<br/>(callers/callees traversal)"]:::graphLayer
      GCycles["Cycle Detection"]:::graphLayer
      GOrphans["Orphan Detection"]:::graphLayer
    end

    PkgGlue["src/__init__.py<br/>(package entry glue)"]:::service
  end

  %% Wiring (components)
  HAnalyze -->|"calls"| FileEnum
  FileEnum -->|"reads"| CodebaseDisk
  FileEnum -->|"feeds_source"| TSRuntime
  TSRuntime -->|"uses"| TS
  TSRuntime -->|"produces"| ASTWalk
  ASTWalk -->|"drives"| SymExtract
  ASTWalk -->|"drives"| CallExtract
  SymExtract -->|"emits"| FactsOut
  CallExtract -->|"emits"| FactsOut
  FactsOut -->|"build_graph()"| GBuild
  GBuild -->|"populate"| KG

  HCallers -->|"query()"| GQuery
  HCallees -->|"query()"| GQuery
  HCycles -->|"analyze()"| GCycles
  HOrphans -->|"analyze()"| GOrphans

  GQuery -->|"reads"| KG
  GCycles -->|"reads"| KG
  GOrphans -->|"reads"| KG

  %% Interface layer uses graph layer
  GQuery -->|"returns"| HCallers
  GQuery -->|"returns"| HCallees
  GCycles -->|"returns"| HCycles
  GOrphans -->|"returns"| HOrphans

  %% Bind handlers into server process
  PkgGlue -.->|"imports/wires"| HAnalyze
  PkgGlue -.->|"imports/wires"| HCallers
  PkgGlue -.->|"imports/wires"| HCallees
  PkgGlue -.->|"imports/wires"| HCycles
  PkgGlue -.->|"imports/wires"| HOrphans

  %% ============================================
  %% D) Engineering / Validation (separate lane)
  %% ============================================
  subgraph "D) Engineering Lifecycle - Validation & Quality Gates"
    direction TB

    Verifier["Verifier CLI<br/>(integration-style accuracy harness)"]:::qa
    Pytest["Pytest Test Suite<br/>(unit tests)"]:::qa

    FixtureDir[("Demo Fixture Codebase<br/>data/legacy_project/")]:::data
    MainCPP["main.cpp (fixture)"]:::data
    UtilsCPP["utils.cpp (fixture)"]:::data

    DocsReadme["README.md<br/>(architecture + usage)"]:::docs
    DocsManual["PROJECT_MANUAL.md<br/>(API/tool details)"]:::docs
  end

  FixtureDir -->|"contains"| MainCPP
  FixtureDir -->|"contains"| UtilsCPP

  Verifier -->|"runs_end_to_end"| HAnalyze
  Verifier -->|"validates_expected_graph"| KG
  Verifier -->|"uses_fixture"| FixtureDir

  Pytest -->|"unit_tests"| Parser
  Pytest -->|"unit_tests"| GraphEngine
  Pytest -->|"unit_tests"| ToolRouter

  DocsReadme -.->|"documents_tools"| ToolRouter
  DocsManual -.->|"specifies_schemas"| ToolRouter

  %% ======================
  %% Cross-diagram anchors
  %% ======================
  ServerProc -.->|"hosts"| ToolRouter
  ToolRouter -.->|"implements"| HAnalyze
  ToolRouter -.->|"implements"| HCallers
  ToolRouter -.->|"implements"| HCallees
  ToolRouter -.->|"implements"| HCycles
  ToolRouter -.->|"implements"| HOrphans

  Parser -.->|"contains"| FileEnum
  Parser -.->|"contains"| TSRuntime
  Parser -.->|"contains"| ASTWalk
  Parser -.->|"contains"| SymExtract
  Parser -.->|"contains"| CallExtract

  GraphEngine -.->|"contains"| GBuild
  GraphEngine -.->|"contains"| GQuery
  GraphEngine -.->|"contains"| GCycles
  GraphEngine -.->|"contains"| GOrphans

  CodebaseDisk -.->|"demo_fixture"| FixtureDir

  %% ============
  %% Click events
  %% ============
  click ServerProc "https://github.com/rohityadav34980/legacygraph-mcp/tree/main/src/"
  click ToolRouter "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/src/server.py"
  click Parser "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/src/parser.py"
  click GraphEngine "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/src/graph.py"
  click PkgGlue "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/src/__init__.py"
  click FixtureDir "https://github.com/rohityadav34980/legacygraph-mcp/tree/main/data/legacy_project/"
  click MainCPP "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/data/legacy_project/main.cpp"
  click UtilsCPP "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/data/legacy_project/utils.cpp"
  click Verifier "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/tools/verifier.py"
  click Pytest "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/tests/test_core.py"
  click Poetry "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/pyproject.toml"
  click DocsReadme "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/README.md"
  click DocsManual "https://github.com/rohityadav34980/legacygraph-mcp/blob/main/PROJECT_MANUAL.md"

  %% ======
  %% Styles
  %% ======
  classDef external fill:#e5e7eb,stroke:#374151,stroke-width:1px,color:#111827;
  classDef service fill:#dbeafe,stroke:#1d4ed8,stroke-width:1.5px,color:#0b1f4b;
  classDef interface fill:#bfdbfe,stroke:#1d4ed8,stroke-width:1px,color:#0b1f4b;
  classDef ingestion fill:#c7d2fe,stroke:#4338ca,stroke-width:1px,color:#1e1b4b;
  classDef graphLayer fill:#93c5fd,stroke:#1e40af,stroke-width:1px,color:#0b1f4b;
  classDef thirdparty fill:#e9d5ff,stroke:#7c3aed,stroke-width:1px,color:#2e1065;
  classDef data fill:#fed7aa,stroke:#c2410c,stroke-width:1px,color:#3b1d0a;
  classDef qa fill:#bbf7d0,stroke:#15803d,stroke-width:1px,color:#052e16;
  classDef build fill:#cffafe,stroke:#0891b2,stroke-width:1px,color:#042f2e;
  classDef docs fill:#fde68a,stroke:#b45309,stroke-width:1px,color:#3b2f0a;
```
