Skip to main content

Professional Rust code analysis tool using tree-sitter — extracts, indexes, and analyzes Rust source code with call graphs, complexity metrics, and dependency tracking.

Project description

rust-analyzer-db

A professional Python tool that uses tree-sitter to parse Rust source code, extracts code entities, computes static metrics, and stores everything in a SQLite database for query, search, call-graph analysis, and reporting.

Features

  • Full extraction: structs, enums, traits, impls, functions/methods, consts, statics, mods, macros, type aliases, unions, use declarations, extern crate
  • Static metrics: cyclomatic complexity, cognitive complexity, nesting depth, branch count, lines of code per function
  • Generic parameters & lifetimes: extracted from structs, functions, impls
  • Call graph: whole-project or focused subgraph with Graphviz DOT/SVG/PNG/HTML rendering and interactive vis-network visualization
  • Public API surface: identify all public items in a project
  • Dependency tracking: use declarations and extern crate declarations
  • Complexity report: find the most complex functions in your codebase
  • Full-text search: SQLite FTS5 over name, signature, doc, and source
  • JSON output: all commands support --json for machine-readable output
  • Incremental scanning: unchanged files skipped by content hash
  • Structured logging: configurable verbosity and format
  • Web frontend: master-panel dashboard with interactive call graph, complexity report, API surface, dependency analysis, and search
  • MCP server: expose all analysis tools via Model Context Protocol for AI assistants (Claude Code, Claude Desktop, etc.)

Install

pip install rust-analyzer-db
pip install -e ".[dev]"

Or with just dependencies:

pip install tree-sitter tree-sitter-rust fastapi uvicorn jinja2 python-multipart "mcp>=1.0,<2"

Python 3.10+ required.

Usage

1. Scan a project

rust-analyzer-db scan /path/to/rust/project --db rust_code.db
  • Recursively finds all *.rs files (skips target/, .git/, node_modules/)
  • Works on a single file: rust-analyzer-db scan src/lib.rs
  • Re-running is incremental (unchanged files skipped)
  • Use --force to re-parse everything, -q to suppress progress

2. List items

rust-analyzer-db list --kind struct --db rust_code.db
rust-analyzer-db list --kind function --name parse --db rust_code.db
rust-analyzer-db list --json --db rust_code.db

3. Show full source

rust-analyzer-db show new --db rust_code.db --kind method

4. List methods on a type

rust-analyzer-db methods Point --db rust_code.db --full

5. Full-text search

rust-analyzer-db search "distance" --db rust_code.db
rust-analyzer-db search "parse OR tokenize" --db rust_code.db --full

6. Project statistics

rust-analyzer-db stats --db rust_code.db
rust-analyzer-db stats --json --db rust_code.db

7. Complexity report

rust-analyzer-db complexity --db rust_code.db
rust-analyzer-db complexity --min 10 --db rust_code.db

Shows cyclomatic complexity, cognitive complexity, nesting depth, and LOC for every function/method above the threshold.

8. Public API surface

rust-analyzer-db api --db rust_code.db
rust-analyzer-db api --json --db rust_code.db

Lists all pub items grouped by kind.

9. Dependency analysis

rust-analyzer-db deps --db rust_code.db
rust-analyzer-db deps --full --db rust_code.db  # show all use paths

10. Call graph

# Whole project
rust-analyzer-db graph --db rust_code.db -o callgraph.svg

# Focused on one function
rust-analyzer-db graph --db rust_code.db --root run_demo --depth 3 -o run_demo.svg

# JSON output
rust-analyzer-db graph --db rust_code.db --json

Flags: --direction callees|callers|both, --no-unresolved, --format svg|png|pdf|dot|html.

11. Web frontend

# Start the web UI (default: http://localhost:8000)
rust-analyzer-db serve --db rust_code.db

# Custom host/port
rust-analyzer-db serve --db rust_code.db --host 0.0.0.0 --port 8080

The web frontend provides a master-panel style dashboard with:

  • Dashboard — project stats, top complex functions, item kind distribution, complexity histogram, largest files
  • Items — filterable/sortable/paginated browser for all extracted code entities, with source view modals
  • Complexity — ranked complexity report with adjustable threshold and progress-bar indicators
  • Call graph — interactive vis-network visualization with root/depth/direction controls
  • API surface — public items grouped by kind
  • Dependencies — extern crates and use declaration tree
  • Search — full-text search across names, signatures, docs, and source

All pages also expose JSON endpoints at /api/stats, /api/item/{id}, and /api/graph-data.

12. MCP server (Model Context Protocol)

# Start the MCP server over stdio (for AI assistants)
rust-analyzer-db mcp --db rust_code.db

Exposes Rust code analysis as MCP tools that AI assistants can call via the Model Context Protocol.

Available tools:

Tool Description
scan_project Scan a Rust directory/file into the database
list_items List code items with filters (kind, name, target, file)
get_item Get full source and metadata for an item by ID
search_code Full-text search across names, signatures, docs, source
get_stats Project statistics (files, items, call edges)
complexity_report Find complex functions above a threshold
api_surface List all public API items
dependencies Show extern crates and use declarations
call_graph_info Trace callers/callees for a function, or project stats
methods_of List methods on a type or trait

Register with Claude Code:

claude mcp add rust-analyzer-db -- rust-analyzer-db mcp --db /path/to/rust_code.db

Register with Claude Desktop — add to claude_desktop_config.json:

{
  "mcpServers": {
    "rust-analyzer-db": {
      "command": "rust-analyzer-db",
      "args": ["mcp", "--db", "/path/to/rust_code.db"]
    }
  }
}

What gets captured per item

  • kind, name, visibility, target, trait_name
  • signature (functions/methods, without body)
  • doc (doc comments), attributes (#[derive(...)] etc.)
  • start_line / end_line, file_path
  • source — exact original source text
  • is_pub, is_const_fn, is_async, is_unsafe
  • cyclomatic_complexity, cognitive_complexity, nesting_depth, num_branches, num_function_calls, lines_of_code
  • Generic parameters and lifetime parameters
  • Methods linked to parent impl/trait via parent_id

Database schema

Two main tables (files, items) plus calls, use_declarations, extern_crates, generic_params, lifetime_params, and items_fts (FTS5 index). See rust_analyzer/db.py for the full schema.

Query directly:

sqlite3 rust_code.db "SELECT kind, name, cyclomatic_complexity FROM items WHERE cyclomatic_complexity > 5;"

Development

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

# Run tests
pytest tests/ -v

# Type checking
mypy rust_analyzer/

# Linting
ruff check rust_analyzer/

Architecture

rust_analyzer/
  __init__.py       - Package version
  cli.py            - CLI commands and argument parsing
  db.py             - SQLite storage layer (schema + queries)
  extractor.py      - tree-sitter extraction + metrics
  graph.py          - Call graph construction + rendering
  web.py            - FastAPI web frontend + JSON API
  mcp_server.py     - MCP server (Model Context Protocol tools)
  exceptions.py     - Exception hierarchy
  logging.py        - Structured logging configuration
  static/
    style.css       - Dark master-panel theme
  templates/
    base.html       - Shared layout with sidebar navigation
    dashboard.html  - Stats cards + charts
    items.html      - Filterable item browser
    complexity.html - Complexity report
    graph.html      - Interactive vis-network call graph
    api_surface.html - Public API grouped by kind
    deps.html       - Dependencies tree
    search.html     - Full-text search

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

rust_analyzer_db-0.3.3.tar.gz (330.0 kB view details)

Uploaded Source

Built Distribution

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

rust_analyzer_db-0.3.3-py3-none-any.whl (222.7 kB view details)

Uploaded Python 3

File details

Details for the file rust_analyzer_db-0.3.3.tar.gz.

File metadata

  • Download URL: rust_analyzer_db-0.3.3.tar.gz
  • Upload date:
  • Size: 330.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for rust_analyzer_db-0.3.3.tar.gz
Algorithm Hash digest
SHA256 3cbade1769c387b650e20062282789c12a7a0a69f65979a701a979fbf0363911
MD5 3f29c605cc7f5d22cb535785b31c6d1e
BLAKE2b-256 4c234bba6194a1573355fff25fab88e38d02bd58113bc0cc209a5ebf9e87007f

See more details on using hashes here.

Provenance

The following attestation bundles were made for rust_analyzer_db-0.3.3.tar.gz:

Publisher: workflow.yml on jihoo12/rustanalyser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rust_analyzer_db-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for rust_analyzer_db-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a61c059c9707c1f2c26ca10103f13f8baff361d802d1098dd0a55761ce5ed2df
MD5 13fd12c95fa52afe2a34a51d53e6b914
BLAKE2b-256 77eb727ba20180b683c8839d6bf5ea696b5da5975479a79dfb10822c13c25435

See more details on using hashes here.

Provenance

The following attestation bundles were made for rust_analyzer_db-0.3.3-py3-none-any.whl:

Publisher: workflow.yml on jihoo12/rustanalyser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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