Skip to main content

A navigable graph of your codebase for LLMs. Supports Python, JavaScript/TypeScript, Go, Rust, Java, C/C++, C#, PHP, and Ruby.

Project description

Kontexto

PyPI version Python 3.10+ License: MIT PRs Welcome

A CLI tool to explore codebases efficiently. Designed for LLMs and coding agents as a smarter alternative to ls, grep, and find.

All output is JSON for easy parsing by LLMs and programmatic consumption.

๐ŸŒ Supported Languages

Language Extensions Entities Extracted
Python .py functions, classes, methods
JavaScript .js, .jsx, .mjs functions, classes, methods, arrow functions
TypeScript .ts, .tsx functions, classes, methods, interfaces, types
Go .go functions, methods, structs, interfaces
Rust .rs functions, structs, enums, traits, impl blocks, methods
Java .java classes, interfaces, enums, methods, constructors
C/C++ .c, .h, .cpp, .hpp, .cc functions, structs, enums, classes, methods, typedefs
C# .cs classes, interfaces, structs, enums, methods, constructors, properties
PHP .php functions, classes, interfaces, traits, methods, enums
Ruby .rb, .rake classes, modules, methods, singleton methods

All parsers use tree-sitter for fast, accurate AST-based parsing.

๐Ÿ“ฆ Installation

pip install kontexto

๐Ÿš€ Quick Start

# 1. Index your project
cd /path/to/your/project
kontexto index

# 2. Explore the codebase
kontexto map                          # See project structure
kontexto expand src/api               # Expand a directory
kontexto search "authentication"      # Search for code
kontexto inspect src/api:UserController   # Inspect entity details
kontexto hierarchy BaseModel          # Find all subclasses
kontexto read src/api/users.py 10 50  # Read specific lines

๐Ÿ“– Commands

kontexto index [path]

Index a project and build the navigation graph. Automatically detects and parses all supported languages.

kontexto index                    # Index current directory
kontexto index /path/to/project   # Index specific project
kontexto index -i                 # Incremental update (faster)

Creates a .kontexto/index.db database with:

  • File and directory structure
  • Classes, methods, functions (and language-specific entities)
  • Signatures and docstrings
  • Call relationships
  • Class inheritance (base classes)
  • TF-IDF search index
  • Language metadata for each entity

kontexto map [path]

Show a compact map of the project structure.

$ kontexto map
{
  "command": "map",
  "project": "myapp",
  "root": "/path/to/myapp",
  "stats": {"files": 20, "classes": 8, "functions": 45, "methods": 32},
  "children": [
    {"id": "src", "stats": {"files": 12, "classes": 8, "functions": 45}},
    {"id": "tests", "stats": {"files": 8, "classes": 0, "functions": 24}}
  ]
}

kontexto expand <path>

Expand a node to see its children.

$ kontexto expand src/api/users.py
{
  "command": "expand",
  "node": {
    "id": "src/api/users.py",
    "name": "users.py",
    "type": "file",
    "line_end": 95
  },
  "children": [
    {
      "id": "src/api/users.py:UserController",
      "name": "UserController",
      "type": "class",
      "line_start": 10,
      "line_end": 89,
      "signature": "class UserController",
      "docstring": "Handles user API endpoints",
      "base_classes": ["BaseController"]
    }
  ]
}

kontexto inspect <entity>

Show detailed info about an entity: signature, docstring, relationships.

$ kontexto inspect src/api/users.py:UserController.get_user
{
  "command": "inspect",
  "node": {
    "id": "src/api/users.py:UserController.get_user",
    "name": "get_user",
    "type": "method",
    "file_path": "src/api/users.py",
    "line_start": 15,
    "line_end": 25,
    "signature": "def get_user(self, user_id: int) -> User",
    "docstring": "Retrieve user by ID from database."
  },
  "calls": ["find_by_id"],
  "called_by": ["src/api/routes.py:user_routes"]
}

kontexto search <query>

Search for entities by keyword (names, docstrings, signatures).

$ kontexto search "authentication"
{
  "command": "search",
  "query": "authentication",
  "count": 3,
  "results": [
    {
      "node": {
        "id": "src/api/auth.py:require_auth",
        "name": "require_auth",
        "type": "function",
        "signature": "def require_auth(func: Callable) -> Callable"
      },
      "score": 0.8521
    }
  ]
}

Options:

  • --limit, -l: Maximum number of results (default: 10)

kontexto hierarchy <base_class>

Find all classes that inherit from a given base class.

$ kontexto hierarchy BaseModel
{
  "command": "hierarchy",
  "base_class": "BaseModel",
  "count": 5,
  "subclasses": [
    {
      "id": "src/models/user.py:User",
      "name": "User",
      "type": "class",
      "base_classes": ["BaseModel"],
      "signature": "class User(BaseModel)"
    },
    {
      "id": "src/models/product.py:Product",
      "name": "Product",
      "type": "class",
      "base_classes": ["BaseModel"],
      "signature": "class Product(BaseModel)"
    }
  ]
}

kontexto read <file> [start] [end]

Read source code from a file. Outputs raw code (not JSON).

$ kontexto read src/api/users.py 15 20
    def get_user(self, user_id: int) -> User:
        """Retrieve user by ID from database."""
        user = self.user_service.find_by_id(user_id)
        if not user:
            raise NotFoundError(f"User {user_id} not found")
        return user

Use line ranges from expand or inspect to read specific functions.

๐Ÿค– Use with LLMs

Kontexto is designed for coding agents like Claude Code. Instead of using ls, grep, and find:

# Before: multiple commands, unstructured output
ls -la src/
grep -r "authenticate" src/
cat src/api/auth.py

# After: JSON output, easy to parse
kontexto map
kontexto search "authenticate"
kontexto expand src/api/auth.py

โœจ Benefits for LLMs

Tool Output Structure Relationships
ls File names only None None
grep Matching lines None None
find File paths None None
kontexto Structured JSON Classes, functions, methods Calls, called-by, inheritance

Features

  • JSON output - All commands return structured JSON for easy parsing
  • Class hierarchy - Track inheritance with base_classes field and hierarchy command
  • Search caching - Repeated searches are cached for faster response
  • Incremental indexing - Only changed files are re-indexed, with incremental search index updates

โš™๏ธ How It Works

Kontexto uses tree-sitter to parse source files and builds a navigable graph:

Project Root
โ”œโ”€โ”€ Directories
โ”‚   โ””โ”€โ”€ Files (.py, .js, .ts, .go, .rs, .java)
โ”‚       โ”œโ”€โ”€ Classes/Structs/Traits (with base_classes)
โ”‚       โ”‚   โ””โ”€โ”€ Methods
โ”‚       โ”œโ”€โ”€ Functions
โ”‚       โ”œโ”€โ”€ Interfaces
โ”‚       โ””โ”€โ”€ Enums

The graph is stored in SQLite with:

  • TF-IDF search index for keyword search
  • Call relationships tracking who calls what
  • Class inheritance tracking base classes
  • Language metadata for each entity
  • Incremental updates for large codebases (both graph and search index)

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  CLI Commands   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   CodeGraph     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ ParserRegistry  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ–ผ         โ–ผ        โ–ผ        โ–ผ        โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚Python โ”‚ โ”‚  JS  โ”‚ โ”‚  Go  โ”‚ โ”‚ Rust โ”‚ โ”‚ Java โ”‚
โ”‚Parser โ”‚ โ”‚Parserโ”‚ โ”‚Parserโ”‚ โ”‚Parserโ”‚ โ”‚Parserโ”‚
โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜
    โ”‚        โ”‚        โ”‚        โ”‚        โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                      โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚  tree-sitter  โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Development

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run linter
ruff check src/ tests/

๐Ÿ“„ License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kontexto-0.2.0.tar.gz (64.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kontexto-0.2.0-py3-none-any.whl (56.2 kB view details)

Uploaded Python 3

File details

Details for the file kontexto-0.2.0.tar.gz.

File metadata

  • Download URL: kontexto-0.2.0.tar.gz
  • Upload date:
  • Size: 64.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for kontexto-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c46c9b97f3688bd7e6ac4f75f13cec813ccf96b08619e77e8887bc2dc8f0611c
MD5 d91c27f7f8464638f3382697fdd133e4
BLAKE2b-256 0ca0e8bdac023c3f6dcc32e1ebba930128dc7dce3f1365591637d848f7898dc8

See more details on using hashes here.

File details

Details for the file kontexto-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: kontexto-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 56.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for kontexto-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 177462ec73cac7cf85f2e6d988507718633762caa9e38ac79f72eca041d26455
MD5 0c3a08b8995c50cd990814fa463157c6
BLAKE2b-256 3357551c4be689b2b3ba06b8d9c7e8f366b2b9c6b7ef094c84eb92c433a14f75

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page