kg-extract

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

KG Extract

KG Extract

Extract entities (classes, functions, modules, types, concepts) and their relations (imports, extends, implements, depends-on, calls) from source files, then store them as a knowledge graph in AgentDB.
从源文件中提取实体(类、函数、模块、类型、概念)及其关系(导入、继承、实现、依赖、调用),然后将它们作为知识图谱存储在AgentDB中。

When to use

适用场景

When you need to build or update a knowledge graph from source code or documentation. Useful for understanding codebase structure, dependency analysis, and impact assessment.
当你需要从源代码或文档中构建或更新知识图谱时。适用于理解代码库结构、依赖分析和影响评估。

Steps

步骤

  1. Scan files -- use
    Glob
    and
    Read
    to enumerate and read source files at the given path
  2. Identify entities -- extract classes, functions, modules, types, and config references from each file
  3. Map relations -- for each entity, determine its relations to other entities:
    • imports
      : follow import/require statements
    • extends
      : class inheritance
    • implements
      : interface implementations
    • depends-on
      : constructor dependencies, injected services
    • calls
      : function/method invocations
    • references
      : documentation mentions, comments
  4. Store in AgentDB -- call
    mcp__claude-flow__agentdb_hierarchical-store
    for each entity with metadata (name, type, file, line, description)
  5. Create edges -- call
    mcp__claude-flow__agentdb_causal-edge
    for each relation with source, target, relation type, and weight
  6. Report -- summarize: total entities by type, total relations by type, files scanned
  1. 扫描文件——使用
    Glob
    Read
    枚举并读取指定路径下的源文件
  2. 识别实体——从每个文件中提取类、函数、模块、类型和配置引用
  3. 映射关系——为每个实体确定其与其他实体的关系:
    • imports
      :追踪import/require语句
    • extends
      :类继承关系
    • implements
      :接口实现关系
    • depends-on
      :构造函数依赖、注入的服务
    • calls
      :函数/方法调用
    • references
      :文档提及、注释引用
  4. 存储到AgentDB——为每个实体调用
    mcp__claude-flow__agentdb_hierarchical-store
    ,并附带元数据(名称、类型、文件、行号、描述)
  5. 创建边——为每个关系调用
    mcp__claude-flow__agentdb_causal-edge
    ,包含源实体、目标实体、关系类型和权重
  6. 生成报告——总结:按类型统计的实体总数、按类型统计的关系总数、已扫描文件数

CLI alternative

CLI替代方案

bash
npx @claude-flow/cli@latest memory store --namespace knowledge-graph --key "entity-NAME" --value "METADATA_JSON"
npx @claude-flow/cli@latest memory search --query "entities in auth module" --namespace knowledge-graph
bash
npx @claude-flow/cli@latest memory store --namespace knowledge-graph --key "entity-NAME" --value "METADATA_JSON"
npx @claude-flow/cli@latest memory search --query "entities in auth module" --namespace knowledge-graph