jscpd Documentation

repository·master·Indexed 27 days ago

https://github.com/kucherenko/jscpd

A high-performance copy/paste detector for programming source code supporting over 220 formats. It utilizes the Rabin-Karp algorithm and is available as a TypeScript-based engine (v4) and a Rust-based engine (v5). The project includes a standalone REST API server (jscpd-server) for on-demand duplication detection, support for the Model Context Protocol (MCP) for AI assistants, and optional persistent storage via LevelDB.

Tokens
30.2K
Snippets
61
Records
208
Agent score
90%

What's inside jscpd

  1. Overview of cpd-tokenizer

    master

    The cpd-tokenizer crate provides language-aware tokenization for over 200 programming languages. It generates token streams designed specifically for duplicate detection in copy/paste detection workflows.

    Key features include:

    • Generic tokenizer: Handles comments, strings, keywords, and punctuation.
    • JS/TS/JSX/TSX tokenizer: Uses an Oxc-based implementation.
    • Markdown tokenizer: Supports code fences, front matter, and embedded languages.
    • SFC tokenizer: Supports Vue, Svelte, and Astro files.
    • Embedded language detection: Enables cross-format tokenization for languages embedded within other formats.
  2. Overview of cpd-finder

    master

    The cpd-finder crate is a specialized component of the cpd project designed for file walking and clone detection orchestration. It manages directory traversal using ignore patterns (such as .gitignore and glob exclusions), dispatches tokenization based on file formats, performs clone detection and matching, enriches results with Git blame data to identify duplicate origins, and aggregates statistics.

    Note: This crate is an internal component and is not intended for direct use. For the full command-line interface, use the jscpd crate.

  3. Overview of cpd-core

    master

    The cpd-core crate provides the foundational data models and hashing utilities for the cpd copy/paste detector. It implements the core hash-based detection algorithm used to identify duplicated code.

    Note: This crate is intended as a dependency for the cpd tool and is not designed for direct end-user consumption. For the full CLI experience, use the jscpd crate.

  4. Compare TypeScript (v4) and Rust (v5) engines

    master

    jscpd offers two distinct engines. Choose based on your performance and feature requirements.

    FeatureTypeScript (v4)Rust (v5)
    npm packagejscpd@4jscpd@5 or cpd
    CLI commandjscpdjscpd or cpd
    SpeedBaseline24-37x faster
    Node.js requiredYesNo
    Programming APITypeScriptRust (cpd-finder crate)
    LevelDB/Redis storeYesNo
    Reporters1313

    Note: v5 is a drop-in replacement for v4 CLI usage (same options, config file, and reporters), but it lacks the Node.js programming API and persistent stores.

  5. Use jscpd-server for REST API and MCP

    master
    The jscpd-server package is a standalone server application. It provides a REST API and an MCP (Model Context Protocol) server, enabling on-demand code duplication detection, which is particularly useful for AI-integrated workflows.
  6. Understand jscpd supported formats and detection

    master

    jscpd recognizes 223 language formats. These are categorized into three types:

    1. Auto-detected formats (130): These are automatically recognized based on the file extension.
    2. Registered formats (88): These formats have a grammar defined but no built-in extension mapping. To use these, you must explicitly activate them using the --formats-exts or --formats-names CLI flags.
    3. Internal grammar dependencies (5): The formats clike, comments, hpkp, hsts, and keymap are internal dependencies used by other formats and are not intended for direct user selection.

    Note: The url format is excluded from automatic detection.

  7. Configure JSCPD Server for MCP (Claude Desktop)

    master

    The JSCPD Server supports the Model Context Protocol (MCP) via the POST /mcp endpoint. To use it with Claude Desktop, add the following configuration to your claude_desktop_config.json. Ensure the server is running (e.g., via npx jscpd-server . --port 3000) before starting Claude Desktop.

    {
      "mcpServers": {
        "jscpd": {
          "type": "streamable-http",
          "url": "http://localhost:3000/mcp"
        }
      }
    }
  8. Start the JSCPD Server

    master

    The JSCPD Server is a standalone REST API server for on-demand code duplication detection. It scans a codebase on startup and provides endpoints for checking snippets and retrieving statistics. It also supports the Model Context Protocol (MCP) for AI assistants.

    Use the jscpd-server command to start the server. You can specify the target directory, port, host, and storage engine.

    # Start server in current directory
    jscpd-server
    
    # Start server in specific directory
    jscpd-server /path/to/project
    
    # Start server on specific port
    jscpd-server . --port 8080
    
    # Start server with custom host
    jscpd-server . --host localhost --port 3000
    
    # Start server with persistent storage (recommended for production)
    jscpd-server . --store leveldb