Skip to content

pierre-cheneau/knowledge-tree-mcp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Code Knowledge Tree MCP Server

A specialized MCP server for recursive code analysis and dependency mapping. Designed to help understand complex codebases by building structured knowledge trees of functions, modules, constants, and their interdependencies.

🎯 Purpose

This server was created to systematically analyze and understand complex code like the hr() function in fp.umd.original.js. It provides tools to:

  • Build incremental understanding of code elements
  • Track dependencies between functions, modules, and constants
  • Identify missing dependencies that need further analysis
  • Visualize code structure as dependency trees
  • Import existing analysis work

πŸ›  Installation & Setup

Install in Claude Desktop

# Install the server in Claude Desktop (creates knowledge-tree in current directory)
mcp install code_knowledge_server.py --name "Code Knowledge Tree"

# Install with custom base directory (creates knowledge-tree inside the specified directory)
mcp install code_knowledge_server.py --name "Code Knowledge Tree" -- --working-dir ./my_project_analysis

Development Testing

# Test with MCP Inspector (creates knowledge-tree in current directory)
mcp dev code_knowledge_server.py

# Test with custom base directory
mcp dev code_knowledge_server.py -- --working-dir ./my_project_analysis

# Test with absolute path
mcp dev code_knowledge_server.py -- --working-dir /absolute/path/to/base

Working Directory Configuration

The server creates a knowledge-tree folder inside your specified working directory:

  • Default: ./knowledge-tree/ (in current directory)
  • Custom relative: ./my_analysis/knowledge-tree/
  • Custom absolute: /home/user/projects/my_analysis/knowledge-tree/

This allows you to:

  • Keep knowledge trees organized within project directories
  • Share knowledge trees across team members using consistent paths
  • Organize multiple analysis projects in separate base directories
  • Use centralized storage locations while maintaining the knowledge-tree structure

πŸ”§ Available Tools

Core Tools

  1. add_code_element - Add functions, modules, constants, or variables

    # Example: Add the main hr() function
    add_code_element(
        element_id="hr",
        element_type="function", 
        code="function hr(t, e) { ... }",
        description="Main hybrid cryptography function",
        source_file="fp.umd.original.js",
        line_range="33490-33511"
    )
  2. add_dependency - Link dependencies between elements

    # Link hr() function to its dependency ge()
    add_dependency("hr", "ge")
  3. get_element - Retrieve detailed information about any element

    get_element("hr")  # Returns code, dependencies, metadata
  4. find_missing_dependencies - Identify unresolved dependencies

    find_missing_dependencies("hr")  # Shows what needs to be analyzed next
  5. get_tree_view - Generate visual dependency trees

    get_tree_view("hr", max_depth=3)  # ASCII tree visualization

Utility Tools

  1. list_all_elements - Overview of all elements in the knowledge tree

  2. update_code_element - Modify existing elements

  3. remove_element - Remove elements and clean up references

  4. import_from_analysis_file - Import from existing analysis files

  5. get_knowledge_tree_stats - Health metrics and statistics

  6. get_working_directory_info - Show current working directory configuration

πŸ“Š Example Workflow

Understanding the hr() Function

  1. Start with the main function:

    add_code_element("hr", "function", "function hr(t, e) {...}", "Main crypto function")
  2. Add known dependencies:

    add_dependency("hr", "ge")  # RSA key selector
    add_dependency("hr", "ze")  # AES encryption
    add_dependency("hr", "Fe")  # Crypto module
  3. Find what's missing:

    find_missing_dependencies("hr")
    # Returns: ["ge", "ze", "Fe"] - need to analyze these
  4. Add the missing pieces:

    add_code_element("ge", "function", "function ge() {...}", "RSA key selector")
    add_code_element("ze", "function", "function ze(t, e) {...}", "AES encryption")
    add_code_element("Fe", "module", "r(5634)", "Crypto module")
  5. Visualize progress:

    get_tree_view("hr")

    Output:

    hr [function] - Main crypto function
    β”œβ”€β”€ ge [function] - RSA key selector
    β”œβ”€β”€ ze [function] - AES encryption  
    └── Fe [module] - Crypto module
    
  6. Continue recursively until all dependencies are understood

πŸ“ Data Storage

The server stores knowledge trees in a simple JSON-based format inside a knowledge-tree subfolder:

<working-dir>/                 # Configurable via --working-dir parameter
└── knowledge-tree/            # Always named 'knowledge-tree'
    β”œβ”€β”€ metadata.json          # Global metadata
    └── elements/              # Individual element files
        β”œβ”€β”€ hr.json           # Main function
        β”œβ”€β”€ ge.json           # Dependencies
        β”œβ”€β”€ ze.json
        └── Fe.json

Directory Examples:

  • Default: ./knowledge-tree/
  • Custom: ./my_project_analysis/knowledge-tree/
  • Absolute: /home/user/crypto_analysis/knowledge-tree/

Each element file contains:

{
  "id": "hr",
  "type": "function", 
  "code": "function hr(t, e) { ... }",
  "description": "Main hybrid cryptography function",
  "dependencies": ["ge", "ze", "Fe"],
  "dependents": [],
  "source_file": "fp.umd.original.js",
  "line_range": "33490-33511",
  "created_at": "2025-07-02T16:25:00Z",
  "updated_at": "2025-07-02T16:25:00Z"
}

🎯 Use Cases

Perfect for analyzing:

  • βœ… Complex JavaScript libraries with obfuscated code
  • βœ… Functions with many interdependent calls
  • βœ… Module systems with r(nnnn) style imports
  • βœ… Cryptographic implementations
  • βœ… Any codebase requiring systematic understanding

Example Knowledge Tree:

hr [function] - Main hybrid cryptography function
β”œβ”€β”€ ge [function] - RSA key selector
β”‚   β”œβ”€β”€ fe [function] - Environment detection
β”‚   β”œβ”€β”€ be [constant] - Production RSA key
β”‚   β”œβ”€β”€ me [constant] - Pre-prod RSA key
β”‚   └── ve [constant] - Mobile RSA key
β”œβ”€β”€ ze [function] - AES encryption wrapper
β”‚   β”œβ”€β”€ Fe.O6 [function] - Random bytes generator
β”‚   β”œβ”€β”€ Fe.CW [function] - AES cipher creation
β”‚   └── qe [constant] - AES algorithm constant  
β”œβ”€β”€ Fe [module] - Crypto module (r5634)
β”‚   β”œβ”€β”€ Fe.O6 [function] - Random bytes
β”‚   β”œβ”€β”€ Fe.r0 [function] - RSA encryption
β”‚   └── Fe._G [constant] - Crypto constants
└── Ue [module] - Buffer module (r8764)
    β”œβ”€β”€ Ue.from [function] - Buffer constructor
    └── buffer methods [function] - toString, slice, etc.

πŸ’‘ Benefits

  • Incremental Understanding: Build knowledge piece by piece
  • Dependency Tracking: See exactly what depends on what
  • Missing Element ID: Know what to analyze next
  • Visual Structure: Understand code architecture at a glance
  • Import Existing Work: Leverage previous analysis
  • Tool-Only Design: Perfect compatibility with Claude Desktop

πŸ” Integration with Existing Analysis

This server can import your existing analysis files from the analysis/ directory:

import_from_analysis_file("analysis/01_hr_main.js")

This automatically extracts functions and dependencies from your previous work.

πŸš€ Getting Started

  1. Install the server in Claude Desktop
  2. Start with your main function of interest
  3. Add dependencies as you discover them
  4. Use find_missing_dependencies to guide your analysis
  5. Visualize progress with get_tree_view
  6. Build complete understanding recursively

The server turns code analysis from a linear process into a systematic, visual, and manageable knowledge-building experience.

About

Small Python MCP to structure information extraction

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages