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.
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.
analyze_document before creating entities for a source file. This gives you the exact LSP symbol names. Never guess symbol names.ref_type: "code" with lsp_symbol for source files. Only use ref_type: "text" for markdown, docs, and config files.init_project — Initialize the database schema (run once at start)project_overview — Get current ontology: taxonomy (categories by scope), entity hierarchy, statisticscreate_entity(name, description, category_ids, parent_ids, commands) — Create an entity with at least one referenceupdate_entity(entity_id, ...) — Update entity, add/remove references, create relationshipsdelete_entity(entity_id) — Delete entity (must have no children)create_category(name, scope) — Create a new category at a scope (if the defaults don't fit)get_entity(entity_id) — Full entity details with references and relationshipsfind_entities(scope, category, parent_id) — Filter entities by scope/category/parentsearch(query) — Semantic search across entities and referencesquery(entity_id, semantic_query) — Extract relevant subgraph within token budgetget_document_entities(document_path) — Get all entities referenced in a fileanalyze_document(document_path) — CRITICAL: Discover tracked refs, untracked LSP symbols, and git diffs. Run this BEFORE creating entities for any source file.alter_references(commands) — Bulk update/delete referencesvalidate_graph() — Check for orphans, cycles, scope violations, missing referencesget_changed_files() — Find files modified since last synccommands 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)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 |
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.
Before anything else, build a complete understanding of the project's technology stack.
project_overview to get the current ontology state and available category IDs.create_entity call.Summarize findings to the user. Optionally write a docs/stack.md if the user requests documentation.
After summarizing, proceed to Phase 2.
Build the knowledge graph layer by layer following the strict gnapsis scope hierarchy:
Domain → Feature → Namespace → Component → Unit
services, repositories, mcp::tools).validate(), MAX_RETRIES).create_entity with a Domain-scope category.For each identified domain, briefly log:
Then proceed immediately to build the subgraph.
Build the complete subgraph for each domain:
Register Features under the domain. For each feature:
create_entity with Feature-scope category and parent_ids: [<domain-id>].Register Namespaces under each feature. For each namespace:
create_entity with Namespace-scope category and parent_ids: [<feature-id>].Register Components under each namespace. For each component:
analyze_document(document_path) to discover LSP symbols.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:
create_entity with Unit-scope category and parent_ids: [<component-id>].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.Proceed domain by domain. After completing all domains, summarize progress and proceed to Phase 3.
After all domain subgraphs are built:
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.Run validate_graph() to check for:
Fix any issues found.
Before declaring completion:
validate_graph() and fix all reported issues.analyze_document.find_entities at each scope.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.