code-context-graph

module
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2026 License: MIT

README

code-context-graph

Local code analysis tool that parses codebases via Tree-sitter into a knowledge graph. Supports 12 languages, 17 MCP tools, and custom annotation search.

CCG is built primarily for GPT, Claude, Codex, and other LLM-based coding agents. It acts as local or self-hosted context infrastructure: agents can search code by intent, inspect call graphs, trace impact, retrieve docs, and keep responses bounded instead of reading entire repositories into context.

This is a developer-operated tool, not a general SaaS admin product. The expected users are coding agents and developers who can run CLI commands, configure MCP, read logs, and use the generated graph/docs to guide code changes.

Inspired by code-review-graph — a Python-based code analysis tool. This project reimplements and extends the concept in Go with multi-DB support, custom annotation system, and MCP integration for AI-powered code understanding.

Features

  • 12 languages: Go, Python, TypeScript, Java, Ruby, JavaScript, C, C++, Rust, Kotlin, PHP, Lua/Luau
  • 17 MCP tools: parse, search, graph queries, impact analysis, flow tracing, change detection, documentation discovery, and more
  • Evidence-driven code exploration: DB-backed retrieval returns small file-level candidates with matched fields, evidence nodes, and optional docs before agents drill into exact graph nodes
  • Browser Wiki UI: ccg-server can serve generated docs, tree search, DB-backed retrieval, Context Tray copying, and an Obsidian-style graph viewer
  • Custom annotations: @intent, @domainRule, @sideEffect, @mutates, @index — search code by business context (details)
  • Webhook sync: GitHub / Gitea push events → auto clone + build with per-repo branch filtering and .ccg.yaml build-scope (include_paths / exclude) auto-loading (details)
  • Multi-DB: SQLite (local), PostgreSQL
  • Full-text search: FTS5 (SQLite), tsvector+GIN (PostgreSQL)

Installation

npm install -g code-context-graph
# or
bun install -g code-context-graph

The npm package installs both ccg and ccg-server.

go install
go install github.com/tae2089/code-context-graph/cmd/ccg@latest
go install github.com/tae2089/code-context-graph/cmd/ccg-server@latest
Build from source
CGO_ENABLED=1 go build -tags "fts5" -o ccg ./cmd/ccg/
CGO_ENABLED=1 go build -tags "fts5" -o ccg-server ./cmd/ccg-server/

# Or use Makefile (injects version from git tag automatically)
make build
# Local stripped release-style build
make release

Quick Start

# Parse your project. For the default local SQLite database (ccg.db), runtime
# commands create and migrate the database automatically on first use only when
# the schema is missing.
ccg build .
# Build complete: 70 files, 749 nodes, 7387 edges

# Search (includes annotations)
ccg search "authentication"

# Search by business context
ccg search "payment"    # finds functions with @intent/@domainRule about payments

# Build generated docs and the browser Wiki compatibility index
ccg docs --out docs

# Serve the browser Wiki UI from built assets; builds the graph for DB-backed APIs
make wiki-run

# Graph statistics
ccg status

# Version info
ccg version

# Namespace isolation (MSA)
ccg build ./backend --namespace backend
ccg search --namespace backend "auth"

ccg docs writes generated Markdown plus .ccg/wiki-index.json as a browser Wiki compatibility snapshot. The Wiki prefers the graph database for tree navigation and search, then uses wiki-index.json only when DB-backed navigation is unavailable. Runtime search_docs uses DB-backed graph and annotation evidence; it does not depend on a separately generated retrieval index.

For LLM agents, use DB-backed search_docs as the first stop for broad natural-language questions such as "how does webhook sync work?" or "where are the operational risks?". It is not a Top1 search engine; it is an evidence-driven narrowing layer that should return a small set of relevant files with matched_fields, matched_terms, and evidence nodes. Read the narrowed docs with get_doc_content, then use get_node, query_graph, trace_flow, and impact tools only after the route is narrowed. Use ccg search as a focused annotation/keyword candidate search rather than the first tool for broad code understanding.

If you use PostgreSQL, a custom SQLite DSN, an existing schema, or a controlled upgrade workflow, run ccg migrate explicitly before runtime commands. This also applies when upgrading CCG against an existing default ccg.db created by an older version. See the CLI Reference for the full migration contract.

Browser Wiki

ccg-server can serve a React-based Wiki UI at /wiki when --wiki-dir points at a built web/wiki/dist directory. Docker images include that built UI at /usr/share/ccg/wiki; standalone binaries keep the assets separate so binary size stays small.

The Wiki is meant for developers and agents inspecting a generated codebase:

  • Tree navigation over folders, packages, files, and annotated symbols
  • Keyword search and DB-backed search_docs with matched evidence and small file-level result sets
  • Rich symbol detail cards from CCG annotations even when a symbol has no generated Markdown file
  • Context Tray for collecting files and doc-less symbols into one Markdown bundle that can be copied into another LLM tool
  • Graph tab backed by /wiki/api/graph, showing namespace nodes and edges with filters for structure, calls, imports, types, and symbols

Local development shortcut:

make wiki-run

Use make wiki-run-indexed when you also want generated Markdown and the wiki-index.json compatibility snapshot before starting the server.

For self-hosted deployments, run ccg-server --wiki-dir <dist-dir> and protect /wiki/api/* with the same bearer token policy used for /mcp. See Docker and Runtime Layout for deployment details.

Demo

Actual output from CCG parsing its own codebase.

1. Parse the Codebase
$ ccg build .
Build complete: 145 files, 1654 nodes, 10489 edges
2. Graph Statistics
$ ccg status
Nodes: 1654
Edges: 3767
Files: 192

Node kinds:
  class: 253
  file: 145
  function: 1111
  package: 47
  type: 98

Edge kinds:
  calls: 1822
  contains: 1597
  imports_from: 278
  implements: 66
  inherits: 4

Fallback call analysis:
  calls: 1823
  fallback_calls: 0
  fallback_ratio: 0.00%
$ ccg search "impact analysis"
internal/app/analyze/impact/impact.go              file      internal/app/analyze/impact/impact.go:1
mcp.impactRadiusResponse                           class     internal/adapters/inbound/mcp/handler_analysis.go:36
internal/adapters/inbound/mcp/handler_analysis.go  file      internal/adapters/inbound/mcp/handler_analysis.go:1
mcp.ImpactAnalyzer                                 type      internal/adapters/inbound/mcp/deps.go:40
mcp.impactRadiusMetadata                           class     internal/adapters/inbound/mcp/handler_analysis.go:27
mcp.handlers.getImpactRadius                       function  internal/adapters/inbound/mcp/handler_analysis.go:109

$ ccg search "repository sync"
reposync.Service.Sync                               function  internal/app/reposync/service.go:27
remote.buildRepoSyncHTTP                            function  internal/runtime/remote/http.go:88
reposync.syncPayload                                class     internal/app/reposync/queue.go:94
reposync.SyncHandlerFunc                            type      internal/app/reposync/queue.go:21
reposync.SyncQueue                                  class     internal/app/reposync/queue.go:118
webhook.SyncFunc                                    type      internal/adapters/inbound/webhook/handler.go:22
4. Agent Integration via MCP

After configuring .mcp.json, you can ask an MCP-capable coding agent directly:

"Explain the webhook sync flow in this project"

The agent calls CCG MCP tools and answers directly from the graph:

trace_flow(qualified_name: "webhook.WebhookHandler.ServeHTTP")
→ WebhookHandler.ServeHTTP
  → SyncQueue.Add
    → safeHandle (retry loop: max 3 attempts, exponential backoff 1s→30s)
      → clone (git clone, 15min timeout)
      → build (ccg build, same context)

"Where is the authentication-related code?"

search(query: "authentication")
→ internal/adapters/inbound/webhook/handler.go  (HMAC signature validation)
→ cmd/ccg-server/main.go       (CCG_WEBHOOK_SECRET / --webhook-secret)

MCP Server

Add .mcp.json to your project:

{
  "mcpServers": {
    "ccg": {
      "command": "ccg",
      "args": ["serve", "--db-driver", "sqlite", "--db-dsn", "ccg.db"]
    }
  }
}

For remote HTTP mode:

Run the self-hosted server with ccg-server and connect to /mcp. The same server can also expose /wiki when --wiki-dir is configured:

{
  "mcpServers": {
    "ccg": {
      "type": "streamable-http",
      "url": "http://your-server:8080/mcp"
    }
  }
}

MCP-capable clients such as Codex or Claude Code can connect and get access to 17 MCP tools. See MCP Tools Reference for the full list.

Architecture

Source Code → Tree-sitter Parser → Nodes + Edges + Annotations
                                        ↓
                              SQLite / PostgreSQL (GORM)
                                        ↓
                                   FTS Search
                                        ↓
                         ccg serve                ccg-server
                         stdio MCP        Streamable HTTP + Wiki UI
                            ↓              ↓          ↓          ↑
                     Coding Agents   Remote Clients  Browser   GitHub / Gitea Webhook
                                                       Wiki      push → clone → build → DB

See Architecture Details for component breakdown and DB schema.

Documentation

Guide Description
Korean Guide 한국어 문서 인덱스 (Korean Documentation Index)
CLI Reference All commands, flags, and config file (.ccg.yaml)
Lint Detailed ccg lint category reference, interpretation guide, and CI usage
MCP Tools 17 MCP tools, agent skills, AI-Driven Annotation
Annotations Annotation system — tags, examples, search
Webhook Webhook sync, branch filtering, HMAC, graceful shutdown
Docker Docker build, MCP server, Wiki UI, PostgreSQL deployment
Operations Deployment profiles, database choice, readiness, webhook operations
Runtime Layout ccg, ccg-server, Wiki serving, and shared ccg-core ownership boundaries
Development Dev guide, integration test, project structure
Namespace Migration Default namespace change and migration guide
Architecture Data flow, components, DB schema
CLAUDE.md Guide Template for projects using CCG

License

MIT

Directories

Path Synopsis
cmd
ccg command
@index Local CLI bootstrap entry point for one-shot commands and stdio MCP.
@index Local CLI bootstrap entry point for one-shot commands and stdio MCP.
ccg-server command
@index Self-hosted HTTP server bootstrap for code-context-graph.
@index Self-hosted HTTP server bootstrap for code-context-graph.
internal
adapters/inbound/http
@index Compile-time MCP dependency checks owned by the server runtime.
@index Compile-time MCP dependency checks owned by the server runtime.
adapters/inbound/mcp
@index In-memory TTL cache for repeat MCP read-tool responses with background eviction.
@index In-memory TTL cache for repeat MCP read-tool responses with background eviction.
adapters/inbound/webhook
@index HTTP webhook intake for repository sync dispatch.
@index HTTP webhook intake for repository sync dispatch.
adapters/inbound/wikihttp
@index HTTP handlers for the ccg-server Wiki UI and its viewer-oriented JSON API.
@index HTTP handlers for the ccg-server Wiki UI and its viewer-oriented JSON API.
adapters/outbound/contentfiles
@index Symlink-safe atomic filesystem access rooted beneath one generated-content directory.
@index Symlink-safe atomic filesystem access rooted beneath one generated-content directory.
adapters/outbound/gitrepo
@index Authentication helpers for webhook-driven git access.
@index Authentication helpers for webhook-driven git access.
adapters/outbound/graphgorm
@index GORM persistence adapter for git-diff change-risk analysis.
@index GORM persistence adapter for git-diff change-risk analysis.
adapters/outbound/reposyncobs
@index OpenTelemetry and trace-log adapter for repository sync queue lifecycles.
@index OpenTelemetry and trace-log adapter for repository sync queue lifecycles.
adapters/outbound/searchsql
@index Shared search backend interface and errors for SQLite FTS5 and PostgreSQL tsvector implementations.
@index Shared search backend interface and errors for SQLite FTS5 and PostgreSQL tsvector implementations.
adapters/outbound/treesitter
@index Language-specific grammar specifications and discovery hooks for the Tree-sitter parser.
@index Language-specific grammar specifications and discovery hooks for the Tree-sitter parser.
app/analyze
@index Consumer-owned outbound ports shared by graph analysis use cases.
@index Consumer-owned outbound ports shared by graph analysis use cases.
app/analyze/changes
@index Application git-diff overlap and deterministic change-risk policy.
@index Application git-diff overlap and deterministic change-risk policy.
app/analyze/flow
@index Persisted flow rebuild service for namespace-scoped stored flows.
@index Persisted flow rebuild service for namespace-scoped stored flows.
app/analyze/impact
@index Application BFS blast-radius analysis with depth and node-count bounds.
@index Application BFS blast-radius analysis with depth and node-count bounds.
app/analyze/query
@index Application graph-query policy for callers, imports, inheritance, summaries, and exact-name fallback.
@index Application graph-query policy for callers, imports, inheritance, summaries, and exact-name fallback.
app/crossref
@index Cross-namespace reference sync: materializes @see ccg:// annotation tags into cross_refs rows.
@index Cross-namespace reference sync: materializes @see ccg:// annotation tags into cross_refs rows.
app/docs
@index Consumer-owned graph read contracts for generated documentation and lint policy.
@index Consumer-owned graph read contracts for generated documentation and lint policy.
app/ingest
@index Consumer-owned transaction ports for graph ingest application workflows.
@index Consumer-owned transaction ports for graph ingest application workflows.
app/ingest/incremental
@index Session-local parsed-edge spool for staged incremental reconciliation.
@index Session-local parsed-edge spool for staged incremental reconciliation.
app/ingest/resolve
@index Language-specific dispatch hooks used by call edge resolution.
@index Language-specific dispatch hooks used by call edge resolution.
app/ingest/workflow
@index Full graph build pipeline: spool, batch persistence, and edge resolution.
@index Full graph build pipeline: spool, batch persistence, and edge resolution.
app/reposync
@index Repository/branch admission, ref normalization, and namespace policy for repository sync.
@index Repository/branch admission, ref normalization, and namespace policy for repository sync.
app/search/document
@index Pure search-document content construction from graph nodes and annotations.
@index Pure search-document content construction from graph nodes and annotations.
app/search/identtoken
Package identtoken splits source identifiers into lowercased sub-tokens on separators, camelCase boundaries, and letter/digit transitions.
Package identtoken splits source identifiers into lowercased sub-tokens on separators, camelCase boundaries, and letter/digit transitions.
app/search/rank
Package rank reranks FTS-ranked code-search candidates using dependency-free structural signals (name fuzzy similarity, path proximity), so both the CLI `search` command and the MCP `search` tool share one ranking.
Package rank reranks FTS-ranked code-search candidates using dependency-free structural signals (name fuzzy similarity, path proximity), so both the CLI `search` command and the MCP `search` tool share one ranking.
app/search/retrieval
@index retrieval 패키지의 순수 DB 후보 처리 헬퍼 함수 모음.
@index retrieval 패키지의 순수 DB 후보 처리 헬퍼 함수 모음.
app/wiki
@index Built-in CCG Wiki eager/lazy tree construction and compatibility snapshot policy.
@index Built-in CCG Wiki eager/lazy tree construction and compatibility snapshot policy.
archtest
Package archtest holds executable package-boundary contracts for the CCG modular-hexagonal refactor.
Package archtest holds executable package-boundary contracts for the CCG modular-hexagonal refactor.
ctx
@index 요청 범위 값을 context.Context에 안전하게 전파하는 공용 패키지.
@index 요청 범위 값을 context.Context에 안전하게 전파하는 공용 패키지.
db
@index Database connection, SQLite WAL/busy_timeout, connection pool, and search-backend selection helpers for SQLite and PostgreSQL.
@index Database connection, SQLite WAL/busy_timeout, connection pool, and search-backend selection helpers for SQLite and PostgreSQL.
domain/annotation
@index 커스텀 어노테이션 파서.
@index 커스텀 어노테이션 파서.
domain/graph
@index Materialized cross-namespace annotation references for federated graph analysis.
@index Materialized cross-namespace annotation references for federated graph analysis.
domain/reference
@index CCG reference parser for cross-namespace annotation links.
@index CCG reference parser for cross-namespace annotation links.
obs
@index OpenTelemetry SDK tracer setup and trace-aware logging helpers for server paths.
@index OpenTelemetry SDK tracer setup and trace-aware logging helpers for server paths.
pathspec
Package pathspec evaluates lexical path filters without filesystem access.
Package pathspec evaluates lexical path filters without filesystem access.
runtime
@index Shared runtime wiring for ccg CLI and ccg-server binaries.
@index Shared runtime wiring for ccg CLI and ccg-server binaries.
runtime/mcp
@index Shared MCP runtime assembly for stdio and HTTP server entry points.
@index Shared MCP runtime assembly for stdio and HTTP server entry points.
runtime/remote
@index Remote HTTP runtime composition for MCP, Wiki, webhook, and repository sync.
@index Remote HTTP runtime composition for MCP, Wiki, webhook, and repository sync.
safepath
@index safepath는 여러 핸들러가 공유하는 저수준 경로 안전 프리미티브를 단일화한다.
@index safepath는 여러 핸들러가 공유하는 저수준 경로 안전 프리미티브를 단일화한다.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL