# Architecture
> How CGraph's pieces fit together — from a source tree to a graph an agent queries.
[Source](https://open.nxtsoft.io/docs/cgraph/architecture)

CGraph is a small set of programs around one shared engine. The engine extracts
and resolves the graph; the programs are different front doors onto it.

## The shape of the system

_Repository to query, and the four front doors onto the engine_

```text
                          +-------------------+
   Repository  ------->   |     engine        |   ----->  exports (json/html/svg/...)
                          |  detect · extract |
                          |  resolve · analyze|
                          +---------+---------+
                                    |
                 +------------------+------------------+
                 |                  |                  |
              cgraph             graphd            cgraph-mcp
             (one-shot)     (warm daemon)     (MCP over stdio)
                                    |
                              cgraph-client
                              (thin client)
```

## The four programs

- **`cgraph`** — the one-shot CLI. Scans a tree, builds the graph, writes exports,
  exits. Good for CI and one-off analysis.
- **`graphd`** — the daemon. Keeps the graph warm, watches the tree, and folds in
  edits incrementally. Good for an interactive session.
- **`cgraph-client`** — a thin client that sends operations to a running daemon.
- **`cgraph-mcp`** — an MCP server that exposes the graph to coding agents; its
  tool calls route through the same daemon operation handler as the thin client.

## The engine

`src/engine` owns the deterministic core: detection, extraction, graph building,
analysis, and daemon operations. Because extraction is deterministic, the same
tree always produces the same graph — which is what lets the daemon diff and fold
in changes rather than rebuilding.

## Repository layout

```text
src/cli/       one-shot CLI: cgraph
src/daemon/    daemon: graphd
src/client/    thin client: cgraph-client
src/mcp/       MCP server: cgraph-mcp
src/engine/    detection, extraction, graph building, analysis, daemon ops
vendor/        vendored tree-sitter core and grammars
```

## Reading order

The rest of this section follows a request through the system: how the graph is
built ([Indexing Pipeline](/docs/cgraph/indexing-pipeline)), how it stays warm
([Daemon Architecture](/docs/cgraph/daemon) and
[Incremental Updates](/docs/cgraph/incremental-updates)), and how a query is
answered ([Retrieval](/docs/cgraph/retrieval) and
[Context Packing](/docs/cgraph/context-packing)).
