diffsitter

repository·main·Indexed 25 days ago

https://github.com/afnanenayet/diffsitter

An AST-based difftool for meaningful diffs, version 0.9.0. The project includes tree-sitter-mcp, a Claude Code plugin and MCP server that provides structural code understanding via tree-sitter AST navigation for over 14 languages, including Rust, Python, TypeScript, and Go. It exposes tools for parsing files, listing symbols, retrieving definitions, and running S-expression queries.

Tokens
9.4K
Snippets
17
Records
78
Agent score
82%

What's inside diffsitter

  1. What is tree-sitter-mcp?

    main

    tree-sitter-mcp is a Claude Code plugin that provides Claude with structural understanding of code by leveraging tree-sitter AST (Abstract Syntax Tree) navigation. Instead of treating code as flat text, it allows Claude to interact with the code's actual structure.

    It supports over 14 languages, including:

    • Rust, Python, TypeScript, TSX, JavaScript, Go, Java, C, C++, C#, Ruby, Bash, PHP, OCaml, CSS, and HCL.
  2. Common usage patterns for AST navigation

    main

    Exploring an unfamiliar codebase

    1. Use list_symbols on key files to understand the high-level structure.
    2. Use get_definition to read specific symbols required for understanding.
    3. Use get_children_of to drill down into classes or modules.

    Investigating a specific location

    1. Use get_node_at_position to identify the syntax construct at a specific line/column.
    2. Use get_scope to understand the nesting context.
    3. Use navigate with the parent direction to walk up the AST tree.
    1. Use query with S-expressions to perform pattern matching across the AST.
  3. Develop and reload tree-sitter-mcp

    main

    When developing or manually updating the tool, use the following commands:

    • Manual Rebuild: To rebuild the binary with MCP server support:

      cargo build --release --features mcp-server --bin tree-sitter-mcp
    • Reloading in Claude Code: After making changes and rebuilding, you can pick up the new plugin version without restarting your Claude Code session by running the following command inside the Claude Code interface: /reload-plugins

    cargo build --release --features mcp-server --bin tree-sitter-mcp
  4. Install required development tools

    main

    For a full development experience, especially when running CI checks or reviewing tests, install the following tools:

    ToolInstall CommandPurpose
    cargo-nextestcargo install cargo-nextestTest runner used in CI
    cargo-instacargo install cargo-instaSnapshot test review TUI
  5. Run local CI checks

    main

    Before submitting a Pull Request, run the following suite of commands to ensure formatting, linting, and tests pass. This matches the requirements of the CI pipeline.

    cargo fmt --all -- --check
    cargo clippy --all-targets --all-features -- -D warnings
    cargo nextest run --all-features
    cargo test --doc --all-features
  6. Use tree-sitter-mcp as a standalone MCP server

    main

    If you prefer not to use the plugin hook or already have a pre-built binary, you can register the tree-sitter-mcp binary directly with Claude Code as a standalone MCP server.

    1. Build the binary with the mcp-server feature enabled:

      cargo build --release --features mcp-server --bin tree-sitter-mcp
    2. Register with Claude Code:

      claude mcp add tree-sitter-mcp -- ./target/release/tree-sitter-mcp
    # Build
    cargo build --release --features mcp-server --bin tree-sitter-mcp
    
    # Register with Claude Code
    claude mcp add tree-sitter-mcp -- ./target/release/tree-sitter-mcp
  7. Initialize development environment with pre-commit

    main

    This project uses pre-commit to automatically apply linters and formatters before every commit. To ensure your files are automatically formatted and to prevent commit failures due to linting errors, initialize the git hooks in your local repository.

    pre-commit install
  8. Use tree-sitter-mcp as a Claude Code plugin

    main

    You can load the plugin directly into your Claude Code session. This method includes a SessionStart hook that automatically builds the tree-sitter-mcp binary on its first use.

    Prerequisites for auto-build:

    • Rust toolchain (version 1.85.1 or higher)
    • A C/C++ compiler (required for tree-sitter grammars)
    • Git submodules must be initialized: git submodule update --init --recursive
    claude --plugin-dir ./plugins/tree-sitter-mcp
  9. Build with dynamic grammar library support

    main

    By default, diffsitter compiles tree-sitter grammars statically. If you prefer to use dynamic libraries instead of cloning submodules, you can build the binary with the dynamic-grammar-libs feature enabled. This will look for shared libraries in the user's default library lookup path (e.g., .so on Linux, .dylib on MacOS, or .dll on Windows) following the pattern libtree-sitter-{lang}.{ext}.

    cargo build --no-default-features --features dynamic-grammar-libs
  10. Build diffsitter using the Rust toolchain

    main

    The project uses Cargo for building. Because it includes bindings to tree-sitter, you must have a C compiler that supports C99 or later and a C++ compiler that supports C++14 or later.

    If you are cloning for the first time, ensure you include submodules to provide the necessary tree-sitter grammars:

    git clone --recurse-submodules https://github.com/afnanenayet/diffsitter.git

    If you have already cloned the repository, initialize or update the submodules using:

    git submodule update --init --recursive
  11. Use the tree-sitter-mcp tools

    main
    The tree-sitter-mcp server provides eight tools for interacting with the Abstract Syntax Tree (AST) of source files using tree-sitter. These tools allow for parsing, navigating, querying, and symbol resolution. Most tools accept an optional language parameter to override the default language inference based on file extensions.