Skip to main content
Glama
codetree

codetree

Official
by RingKoAI
README.md
[English](README.md)|[中文](README.zh-CN.md)

# codetree

`tree` shows the shape of a directory. `codetree` shows the shape of the code
inside it: directories → files → classes, functions and methods, one row each
with its line range, visibility and the one-sentence purpose from the doc
comment — and, for a file, the classes it references, rendered as a tree with
the file each one resolves to.

It is read-only, uses tree-sitter instead of a compiler or language server, and
supports TypeScript, TSX, JavaScript, Python, Java and Kotlin. A grep mode covers
the other half of code reading: text lines, with grep flags.

```
src/main/java/com/acme/order/
└─ OrderService.java  (5 syms · 23 L · 9 refs)
   └─ cls public class OrderService extends BaseService implements OrderApi  :9-23  pub  "Places orders."
      ├─ field private final OrderRepo repo  :10  priv
      ├─ field private final Clock clock  :11  priv
      ├─ ctor public OrderService(OrderRepo repo, Clock clock)  :13-16  pub
      └─ meth public Order place(Cart cart, List<Item> items)  :19-22  pub  "Validates the cart, then writes the order."

references (9 total, 8 resolved, 1 external):
  ├─ imp   BaseService com.acme.base.BaseService  -> src/main/java/com/acme/base/BaseService.java
  ├─ imp   Clock com.acme.clock.Clock  -> src/main/java/com/acme/clock/Clock.java
  ├─ imp   List java.util.List  (external)
  ├─ imp   OrderRepo com.acme.repo.OrderRepo  -> src/main/java/com/acme/repo/OrderRepo.java
  ├─ impl  OrderApi  -> src/main/java/com/acme/order/OrderApi.java
  ├─ new   CartValidator  -> src/main/java/com/acme/order/CartValidator.java
  ├─ param Cart  -> src/main/java/com/acme/order/Cart.java
  ├─ param Item  -> src/main/java/com/acme/order/Item.java
  └─ ret   Order  -> src/main/java/com/acme/order/Order.java
[1 file · 5 symbols · java · 53 ms]
```

## Why

An agent has a context budget. `ls` says nothing about purpose, a raw symbol dump
wastes tokens, and `grep` finds one name at a time. codetree answers three
questions in one compact hierarchy: what exists, what each implementation does,
and what it depends on.

## Quick start

Give this to your agent:

```text
Set up codetree for this project: follow AGENTS.md, verify the MCP server, add it to my client with CODETREE_ROOTS set to the project root, reload, and confirm the outline, read_file, search and grep tools.
```

Or run it yourself:

```bash
npx -y @ringko-ai/codetree --print src
```

From a local checkout:

```bash
pnpm install
pnpm build
node dist/main.js --print src
```

## Depth

`--depth` (CLI) and `depth` (MCP) control how many directory levels are expanded:

```bash
codetree --depth 1 --print src      # the immediate children, plus their files
codetree --depth 3 --print src      # three levels down
```

Each opened file carries its reference count, so going deeper exposes more files
and therefore more references. Pass a file instead of a directory to expand those
references into a tree with the resolved target path:

```bash
npx -y @ringko-ai/codetree --print src/main.ts
```

`depth` is clamped to 1–12 (default 3), and every result is bounded by an entry
budget and a character budget; truncation is reported in the footer.

## Grep

The tree answers "what is here"; grep answers "where is this text". It is
line-oriented and repo-wide, with the flags you expect from grep:

```bash
codetree grep "place" src            # regex, case-sensitive by default
codetree grep -i -w "place" src      # case-insensitive, whole word
codetree grep -C 2 "TODO" .          # two lines of context around each match
codetree grep -l "Cart" .            # files with matches
codetree grep -c "Order" src         # matching-line counts per file
codetree grep -F "operator(" src     # literal string (regex still works)
codetree grep --include '*.java' "place" src
```

Output is `path:line: text`; context lines use `path-line- text`, like grep.
`-m <n>` stops after n matching lines, `--exclude <glob>` skips files and
`--hidden` includes dotfiles. Invalid regexes return an actionable error, and
every result is bounded by the same character budget as the tree.

## MCP server

codetree is a stdio MCP server. Any MCP client that supports stdio can launch it:

```json
{
  "mcpServers": {
    "codetree": {
      "command": "npx",
      "args": ["-y", "@ringko-ai/codetree"],
      "env": { "CODETREE_ROOTS": "/absolute/path/to/project" }
    }
  }
}
```

| Tool | Purpose |
|------|---------|
| `outline` | Implementation tree for a directory or a file. Directories expand to `depth` levels; a single file appends the resolved reference tree. |
| `read_file` | Numbered source lines prefixed by the file outline. Use `start_line`/`end_line` from the outline to read one implementation. |
| `search` | Find declarations by name, signature or documentation (default), or search source text lines. |
| `grep` | grep-style text search: literal or regex, case sensitivity, whole words, context lines, counts, files-with-matches. |

Common `outline` parameters: `path`, `depth`, `docs`, `kinds`, `languages`,
`max_entries`, `max_chars`, `references` (single-file reference tree).

## Reference resolution

Extraction is tree-sitter based; resolution is a name index, not a compiler.

| Language | Import form | Resolution |
|----------|-------------|------------|
| TypeScript / JavaScript | `./repo/order.js` | Relative file lookup, including the `.js` → `.ts` mapping and `index.*` |
| TypeScript / JavaScript | `react` | Marked `external` |
| Java | `com.acme.repo.OrderRepo` | Package + class probed from the module's `src/main/java` layout, then the workspace index |
| Kotlin | `com.acme.repo.OrderRepo` | Same probing; supertypes come from the `:` list |
| Python | `from app.models import Order` | Module path against the root and `src/` |

Non-import references (`extends`, `implements`, `field`, `param`, `ret`, `new`,
`type`) are matched against the file's import table first, then against sibling
files and the workspace index. Ambiguous names prefer the same package; names
that cannot be resolved are printed without a target instead of being guessed.

## Supported languages

`typescript` (`.ts`, `.mts`, `.cts`), `tsx` (`.tsx`, `.jsx`), `javascript`
(`.js`, `.mjs`, `.cjs`), `python` (`.py`, `.pyi`), `java` (`.java`), `kotlin`
(`.kt`, `.kts`) — via the WASM grammars shipped inside the package.

## Limits

- Resolution is heuristic: overloads, generics and path aliases are not resolved
  through a compiler.
- Only a doc comment directly above a declaration becomes its purpose; without
  one the row shows the signature alone.
- The workspace class index is built lazily and is not invalidated when files are
  added or removed during a server session.
- Symlinks and junctions are never followed, and every path must stay inside a
  configured root.

## Development

```bash
pnpm typecheck    # src + tests, zero diagnostics expected
bun test          # extractor, resolver and tool tests against tests/fixtures
```

## License

Apache-2.0.