Build Knowledge Graph
Reverse engineer the architecture of the codebase and build a comprehensive knowledge graph
using the gnapsis MCP tools, following a strict derivation order methodology.
If an argument is provided, focus the analysis on that scope or path. Otherwise, analyze
the entire codebase.
CRITICAL RULES
- Always use gnapsis MCP tools to register all nodes and relationships in the knowledge graph. The gnapsis graph is the primary output.
- Proceed through phases, summarize at each phase boundary. Complete each phase fully, then summarize what was done before moving to the next.
- Use best judgment for ambiguity: When you encounter ambiguity about business domains, feature boundaries, or architectural decisions, use your best technical judgment and document your reasoning. Add a note in the entity description when a decision was ambiguous.
- ALWAYS run
analyze_document before creating entities for a source file. This gives you the exact LSP symbol names. Never guess symbol names.
- Use
ref_type: "code" with lsp_symbol for source files. Only use ref_type: "text" for markdown, docs, and config files.
- Be exhaustive, not superficial. Scan every directory, every configuration file, every module entry point. Leave no stone unturned.
GNAPSIS TOOL REFERENCE
Initialization & Discovery
init_project — Initialize the database schema (run once at start)
project_overview — Get current ontology: taxonomy (categories by scope), entity hierarchy, statistics
Entity Lifecycle
create_entity(name, description, category_ids, parent_ids, commands) — Create an entity with at least one reference
update_entity(entity_id, ...) — Update entity, add/remove references, create relationships
delete_entity(entity_id) — Delete entity (must have no children)
Taxonomy
create_category(name, scope) — Create a new category at a scope (if the defaults don't fit)
Querying
get_entity(entity_id) — Full entity details with references and relationships
find_entities(scope, category, parent_id) — Filter entities by scope/category/parent
search(query) — Semantic search across entities and references
query(entity_id, semantic_query) — Extract relevant subgraph within token budget
get_document_entities(document_path) — Get all entities referenced in a file
Document Analysis
analyze_document(document_path) — CRITICAL: Discover tracked refs, untracked LSP symbols, and git diffs. Run this BEFORE creating entities for any source file.
Maintenance
alter_references(commands) — Bulk update/delete references
validate_graph() — Check for orphans, cycles, scope violations, missing references
get_changed_files() — Find files modified since last sync
Entity Commands (used in create_entity/update_entity commands array)
{ type: "add", ref_type: "code", document_path: "...", lsp_symbol: "...", description: "..." } — For source files
{ type: "add", ref_type: "text", document_path: "...", start_line: N, end_line: M, description: "..." } — For docs/config
{ type: "relate", entity_id: "...", note: "..." } — Create RELATED_TO relationship
{ type: "link", entity_id: "...", link_type: "calls|imports|implements|instantiates" } — Code links (Component/Unit only)
Default Categories (from project_overview)
| Scope |
Categories |
| Domain |
core, infrastructure |
| Feature |
functional, technical, non-functional |
| Namespace |
module, library |
| Component |
struct, trait, enum, class |
| Unit |
function, method, constant, field |
MANDATORY WORKFLOW: Creating Entities from Source Files
1. analyze_document(document_path: "src/foo.rs")
→ Returns untracked[] with exact LSP symbol names
2. create_entity(
name: "FooService",
description: "Service for foo operations",
category_ids: ["<struct-category-id>"],
parent_ids: ["<parent-namespace-id>"],
commands: [{
type: "add",
ref_type: "code",
document_path: "src/foo.rs",
lsp_symbol: "FooService", ← MUST match analyze_document output
description: "FooService struct"
}]
)
NEVER skip analyze_document. NEVER guess lsp_symbol names.
PHASE 1: STACK IDENTIFICATION
Before anything else, build a complete understanding of the project's technology stack.
Steps:
- Scan the root directory for configuration files: package.json, Cargo.toml, go.mod, pom.xml, build.gradle, requirements.txt, pyproject.toml, Gemfile, composer.json, Makefile, Dockerfile, docker-compose.yml, CI/CD configs, etc.
- Identify the primary language(s) and their versions.
- Catalog all libraries and frameworks with their roles (web framework, ORM, testing, logging, auth, etc.).
- Map the persistence layer: databases, migration tools, ORM configurations.
- Identify service dependencies: external APIs, microservice connections, message brokers, cloud services.
- Identify infrastructure patterns: containerization, orchestration, CI/CD pipelines, deployment targets.
- Identify architectural style: monolith, microservices, modular monolith, serverless, event-driven, hexagonal, clean architecture, MVC, CQRS, etc.
Gnapsis Setup:
- Run
project_overview to get the current ontology state and available category IDs.
- Note all category IDs — you will need them for every
create_entity call.
Output:
Summarize findings to the user. Optionally write a docs/stack.md if the user requests documentation.
After summarizing, proceed to Phase 2.
PHASE 2: KNOWLEDGE GRAPH CONSTRUCTION (Gnapsis Derivation Order)
Build the knowledge graph layer by layer following the strict gnapsis scope hierarchy:
Domain → Feature → Namespace → Component → Unit
Scope Definitions:
- Domain: A bounded context representing a major business or technical concern (e.g., Authentication, Graph Abstraction, MCP Server).
- Feature: A cohesive capability within a domain (e.g., within Auth: Login, Registration, Password Reset).
- Namespace: A code module or package that implements part of a feature (e.g.,
services, repositories, mcp::tools).
- Component: A concrete code artifact — struct, trait, class, enum (e.g., UserService, AuthTrait).
- Unit: An atomic functional element — function, method, constant, field (e.g.,
validate(), MAX_RETRIES).
Process for EACH Domain:
Step 2.1: Domain Discovery
- Analyze directory structure, namespace patterns, and module boundaries.
- Look for domain indicators: directory names, namespace prefixes, bounded context markers, configuration sections.
- Cross-reference with the stack analysis to understand framework-specific conventions.
- Register each domain using
create_entity with a Domain-scope category.
Step 2.2: Domain Summary
For each identified domain, briefly log:
- Domain name and description
- Constituent features (enumerated)
- Key entry points and interfaces
- Dependencies on other domains (preliminary)
Then proceed immediately to build the subgraph.
Step 2.3: Full Domain Subgraph Construction
Build the complete subgraph for each domain:
Register Features under the domain. For each feature:
- Use
create_entity with Feature-scope category and parent_ids: [<domain-id>].
- Identify all code paths that implement this feature.
Register Namespaces under each feature. For each namespace:
- Use
create_entity with Namespace-scope category and parent_ids: [<feature-id>].
- Map module boundaries and imports/exports.
Register Components under each namespace. For each component:
- First run
analyze_document(document_path) to discover LSP symbols.
- Use
create_entity with Component-scope category, parent_ids: [<namespace-id>], and a code reference using the exact lsp_symbol from analyze_document.
Register Units under each component. For each unit:
- Use
create_entity with Unit-scope category and parent_ids: [<component-id>].
- Reference the exact LSP symbol from
analyze_document.
Register relationships using update_entity commands:
{ type: "relate", entity_id: "...", note: "..." } for semantic relationships.
{ type: "link", entity_id: "...", link_type: "calls" } for code-level links.
Step 2.4: Repeat for All Domains
Proceed domain by domain. After completing all domains, summarize progress and proceed to Phase 3.
PHASE 3: INTER-DOMAIN RELATIONSHIP ANALYSIS AND OPTIMIZATION
After all domain subgraphs are built:
Step 3.1: Cross-Domain Dependency Scan
- Trace all imports, calls, events, and data flows that cross domain boundaries.
- Identify shared models, common utilities, and cross-cutting concerns.
- Map integration points: API calls between domains, shared database tables, event bus topics.
Step 3.2: Register Inter-Domain Relationships
For each cross-domain relationship, use update_entity with:
{ type: "relate", entity_id: "<target>", note: "description of relationship" } for semantic links.
{ type: "link", entity_id: "<target>", link_type: "calls|imports|implements|instantiates" } for code links.
Step 3.3: Coupling and Cohesion Analysis
- Cohesion check: Are all elements within each domain/feature/namespace closely related? Flag low-cohesion areas.
- Coupling check: Are inter-domain dependencies minimal and well-defined? Flag high-coupling areas.
- Identify architectural smells: circular dependencies, god modules, feature envy, shotgun surgery.
Step 3.4: Graph Validation
Run validate_graph() to check for:
- Orphan entities (no parent at non-Domain scope)
- Cycles in BELONGS_TO relationships
- Scope violations (child scope not deeper than parent)
- Entities without references
- Entities without classification
Fix any issues found.
PROGRESS TRACKING
- At the start of each phase, announce what you're about to do.
- After completing each domain, summarize what was registered in the graph (entity counts by scope).
- After completing all phases, provide a final summary.
QUALITY ASSURANCE
Before declaring completion:
- Run
validate_graph() and fix all reported issues.
- Verify every source file has been analyzed via
analyze_document.
- Cross-check the graph against the directory structure for completeness using
find_entities at each scope.
- Ensure all relationship types are consistent and accurately labeled.
- Confirm the derivation order (Domain → Feature → Namespace → Component → Unit) is respected throughout.
- Present a final summary with graph statistics from
project_overview.
The final deliverable is the complete knowledge graph in gnapsis. Every node, every edge, every relationship must be registered through the gnapsis MCP tools.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: build-knowledge-graph3description: Reverse engineer codebase architecture and build a knowledge graph using gnapsis MCP tools. Use when the user wants to map domains, features, modules, components, and their relationships. Use when this capability is needed.4---56# Build Knowledge Graph78Reverse engineer the architecture of the codebase and build a comprehensive knowledge graph9using the gnapsis MCP tools, following a strict derivation order methodology.1011If an argument is provided, focus the analysis on that scope or path. Otherwise, analyze12the entire codebase.1314---1516## CRITICAL RULES17181. **Always use gnapsis MCP tools** to register all nodes and relationships in the knowledge graph. The gnapsis graph is the primary output.192. **Proceed through phases, summarize at each phase boundary.** Complete each phase fully, then summarize what was done before moving to the next.203. **Use best judgment for ambiguity**: When you encounter ambiguity about business domains, feature boundaries, or architectural decisions, use your best technical judgment and document your reasoning. Add a note in the entity description when a decision was ambiguous.214. **ALWAYS run `analyze_document` before creating entities** for a source file. This gives you the exact LSP symbol names. Never guess symbol names.225. **Use `ref_type: "code"` with `lsp_symbol` for source files**. Only use `ref_type: "text"` for markdown, docs, and config files.236. **Be exhaustive, not superficial**. Scan every directory, every configuration file, every module entry point. Leave no stone unturned.2425---2627## GNAPSIS TOOL REFERENCE2829### Initialization & Discovery30- `init_project` — Initialize the database schema (run once at start)31- `project_overview` — Get current ontology: taxonomy (categories by scope), entity hierarchy, statistics3233### Entity Lifecycle34- `create_entity(name, description, category_ids, parent_ids, commands)` — Create an entity with at least one reference35- `update_entity(entity_id, ...)` — Update entity, add/remove references, create relationships36- `delete_entity(entity_id)` — Delete entity (must have no children)3738### Taxonomy39- `create_category(name, scope)` — Create a new category at a scope (if the defaults don't fit)4041### Querying42- `get_entity(entity_id)` — Full entity details with references and relationships43- `find_entities(scope, category, parent_id)` — Filter entities by scope/category/parent44- `search(query)` — Semantic search across entities and references45- `query(entity_id, semantic_query)` — Extract relevant subgraph within token budget46- `get_document_entities(document_path)` — Get all entities referenced in a file4748### Document Analysis49- `analyze_document(document_path)` — **CRITICAL**: Discover tracked refs, untracked LSP symbols, and git diffs. Run this BEFORE creating entities for any source file.5051### Maintenance52- `alter_references(commands)` — Bulk update/delete references53- `validate_graph()` — Check for orphans, cycles, scope violations, missing references54- `get_changed_files()` — Find files modified since last sync5556### Entity Commands (used in create_entity/update_entity `commands` array)57- `{ type: "add", ref_type: "code", document_path: "...", lsp_symbol: "...", description: "..." }` — For source files58- `{ type: "add", ref_type: "text", document_path: "...", start_line: N, end_line: M, description: "..." }` — For docs/config59- `{ type: "relate", entity_id: "...", note: "..." }` — Create RELATED_TO relationship60- `{ type: "link", entity_id: "...", link_type: "calls|imports|implements|instantiates" }` — Code links (Component/Unit only)6162### Default Categories (from `project_overview`)6364| Scope | Categories |65|-------|-----------|66| Domain | `core`, `infrastructure` |67| Feature | `functional`, `technical`, `non-functional` |68| Namespace | `module`, `library` |69| Component | `struct`, `trait`, `enum`, `class` |70| Unit | `function`, `method`, `constant`, `field` |7172---7374## MANDATORY WORKFLOW: Creating Entities from Source Files7576```771. analyze_document(document_path: "src/foo.rs")78 → Returns untracked[] with exact LSP symbol names79802. create_entity(81 name: "FooService",82 description: "Service for foo operations",83 category_ids: ["<struct-category-id>"],84 parent_ids: ["<parent-namespace-id>"],85 commands: [{86 type: "add",87 ref_type: "code",88 document_path: "src/foo.rs",89 lsp_symbol: "FooService", ← MUST match analyze_document output90 description: "FooService struct"91 }]92 )93```9495**NEVER skip `analyze_document`. NEVER guess `lsp_symbol` names.**9697---9899## PHASE 1: STACK IDENTIFICATION100101Before anything else, build a complete understanding of the project's technology stack.102103### Steps:1041. **Scan the root directory** for configuration files: package.json, Cargo.toml, go.mod, pom.xml, build.gradle, requirements.txt, pyproject.toml, Gemfile, composer.json, Makefile, Dockerfile, docker-compose.yml, CI/CD configs, etc.1052. **Identify the primary language(s)** and their versions.1063. **Catalog all libraries and frameworks** with their roles (web framework, ORM, testing, logging, auth, etc.).1074. **Map the persistence layer**: databases, migration tools, ORM configurations.1085. **Identify service dependencies**: external APIs, microservice connections, message brokers, cloud services.1096. **Identify infrastructure patterns**: containerization, orchestration, CI/CD pipelines, deployment targets.1107. **Identify architectural style**: monolith, microservices, modular monolith, serverless, event-driven, hexagonal, clean architecture, MVC, CQRS, etc.111112### Gnapsis Setup:1131. Run `project_overview` to get the current ontology state and available category IDs.1142. Note all category IDs — you will need them for every `create_entity` call.115116### Output:117Summarize findings to the user. Optionally write a `docs/stack.md` if the user requests documentation.118119After summarizing, proceed to Phase 2.120121---122123## PHASE 2: KNOWLEDGE GRAPH CONSTRUCTION (Gnapsis Derivation Order)124125Build the knowledge graph layer by layer following the strict gnapsis scope hierarchy:126127```128Domain → Feature → Namespace → Component → Unit129```130131### Scope Definitions:132- **Domain**: A bounded context representing a major business or technical concern (e.g., Authentication, Graph Abstraction, MCP Server).133- **Feature**: A cohesive capability within a domain (e.g., within Auth: Login, Registration, Password Reset).134- **Namespace**: A code module or package that implements part of a feature (e.g., `services`, `repositories`, `mcp::tools`).135- **Component**: A concrete code artifact — struct, trait, class, enum (e.g., UserService, AuthTrait).136- **Unit**: An atomic functional element — function, method, constant, field (e.g., `validate()`, `MAX_RETRIES`).137138### Process for EACH Domain:139140#### Step 2.1: Domain Discovery141- Analyze directory structure, namespace patterns, and module boundaries.142- Look for domain indicators: directory names, namespace prefixes, bounded context markers, configuration sections.143- Cross-reference with the stack analysis to understand framework-specific conventions.144- Register each domain using `create_entity` with a Domain-scope category.145146#### Step 2.2: Domain Summary147For each identified domain, briefly log:148- Domain name and description149- Constituent features (enumerated)150- Key entry points and interfaces151- Dependencies on other domains (preliminary)152153Then proceed immediately to build the subgraph.154155#### Step 2.3: Full Domain Subgraph Construction156Build the complete subgraph for each domain:1571581. **Register Features** under the domain. For each feature:159 - Use `create_entity` with Feature-scope category and `parent_ids: [<domain-id>]`.160 - Identify all code paths that implement this feature.1611622. **Register Namespaces** under each feature. For each namespace:163 - Use `create_entity` with Namespace-scope category and `parent_ids: [<feature-id>]`.164 - Map module boundaries and imports/exports.1651663. **Register Components** under each namespace. For each component:167 - **First** run `analyze_document(document_path)` to discover LSP symbols.168 - Use `create_entity` with Component-scope category, `parent_ids: [<namespace-id>]`, and a code reference using the exact `lsp_symbol` from `analyze_document`.1691704. **Register Units** under each component. For each unit:171 - Use `create_entity` with Unit-scope category and `parent_ids: [<component-id>]`.172 - Reference the exact LSP symbol from `analyze_document`.1731745. **Register relationships** using `update_entity` commands:175 - `{ type: "relate", entity_id: "...", note: "..." }` for semantic relationships.176 - `{ type: "link", entity_id: "...", link_type: "calls" }` for code-level links.177178#### Step 2.4: Repeat for All Domains179Proceed domain by domain. After completing all domains, summarize progress and proceed to Phase 3.180181---182183## PHASE 3: INTER-DOMAIN RELATIONSHIP ANALYSIS AND OPTIMIZATION184185After all domain subgraphs are built:186187### Step 3.1: Cross-Domain Dependency Scan188- Trace all imports, calls, events, and data flows that cross domain boundaries.189- Identify shared models, common utilities, and cross-cutting concerns.190- Map integration points: API calls between domains, shared database tables, event bus topics.191192### Step 3.2: Register Inter-Domain Relationships193For each cross-domain relationship, use `update_entity` with:194- `{ type: "relate", entity_id: "<target>", note: "description of relationship" }` for semantic links.195- `{ type: "link", entity_id: "<target>", link_type: "calls|imports|implements|instantiates" }` for code links.196197### Step 3.3: Coupling and Cohesion Analysis198- **Cohesion check**: Are all elements within each domain/feature/namespace closely related? Flag low-cohesion areas.199- **Coupling check**: Are inter-domain dependencies minimal and well-defined? Flag high-coupling areas.200- **Identify architectural smells**: circular dependencies, god modules, feature envy, shotgun surgery.201202### Step 3.4: Graph Validation203Run `validate_graph()` to check for:204- Orphan entities (no parent at non-Domain scope)205- Cycles in BELONGS_TO relationships206- Scope violations (child scope not deeper than parent)207- Entities without references208- Entities without classification209210Fix any issues found.211212---213214## PROGRESS TRACKING215216- At the start of each phase, announce what you're about to do.217- After completing each domain, summarize what was registered in the graph (entity counts by scope).218- After completing all phases, provide a final summary.219220---221222## QUALITY ASSURANCE223224Before declaring completion:2251. Run `validate_graph()` and fix all reported issues.2262. Verify every source file has been analyzed via `analyze_document`.2273. Cross-check the graph against the directory structure for completeness using `find_entities` at each scope.2284. Ensure all relationship types are consistent and accurately labeled.2295. Confirm the derivation order (Domain → Feature → Namespace → Component → Unit) is respected throughout.2306. Present a final summary with graph statistics from `project_overview`.231232The final deliverable is the complete knowledge graph in gnapsis. Every node, every edge, every relationship must be registered through the gnapsis MCP tools.233234---235> Converted and distributed by [TomeVault](https://tomevault.io/claim/e7nd7r) — claim your Tome and manage your conversions.236<!-- tomevault:4.0:skill_md:2026-04-11 -->