code2prompt

repository·main·Indexed 27 days ago

https://github.com/mufeedvh/code2prompt

A context engineering tool that converts entire codebases into structured, single-prompt formats optimized for Large Language Models (LLMs). It features a high-speed Rust core library, a CLI tool with an interactive TUI, a Python SDK (code2prompt-rs), and an MCP server. The tool supports codebase scanning with include/exclude patterns, Git diff and log extraction, token counting with multiple encodings, and structural code extraction of entities like functions and classes via the entity-map feature.

Tokens
15.8K
Snippets
53
Records
140
Agent score
93%

What's inside code2prompt

  1. Overview of code2prompt capabilities

    main

    code2prompt is a code ingestion tool designed to transform codebases into structured, AI-optimized prompts for LLMs. It traverses directories, builds tree structures, and gathers file information to curate context windows for tasks like code analysis, generation, and refactoring.

    It is available in four primary formats:

    • Core: A high-performance Rust library for code ingestion.
    • CLI: A human-friendly Command Line Interface.
    • SDK: A Python Software Development Kit for automation and AI agents.
    • MCP: A Model Context Protocol server for providing repository access to LLMs.
  2. Overview of code2prompt integration methods

    main

    code2prompt provides multiple ways to integrate context forging into your workflows, depending on your environment and needs:

    • Core (Rust Library): A Rust-based library for loading, filtering, and organizing code context. It acts as a stateful context manager.
    • CLI: A command-line interface for forging context from a codebase and piping it directly into prompts.
    • Python API: A Python wrapper around the CLI, allowing you to use code2prompt within Python scripts and AI agents.
    • MCP Server: A Model Context Protocol (MCP) server that allows LLMs to use code2prompt as a tool to forge their own context.
  3. Overview of the Code2Prompt ecosystem

    main

    Code2Prompt consists of several components depending on your use case:

    • Core Library: High-speed Rust library for file traversal, .gitignore respect, and Git metadata.
    • CLI Tool: Human-centric tool with a minimal CLI and an interactive TUI for prompt generation and token tracking.
    • Python SDK: Fast Python bindings to the Rust Core via PyPI (code2prompt-rs).
    • MCP Server: A local service allowing agentic applications to read local codebases efficiently.
  4. Understand the Code2Prompt Architecture

    main

    Code2Prompt is a modular code ingestion tool designed to help developers and AI agents interact with codebases by providing high-quality, structured context. It is available in four primary forms:

    • Core: The underlying library that traverses directories, builds tree structures, and gathers file information.
    • CLI: A command-line interface for humans to generate prompts, with support for Handlebars templates and clipboard/file output.
    • SDK: A Python binding for the core library, ideal for AI agents and automation scripts.
    • MCP: A Model Context Protocol server that runs as a local service, allowing LLMs to automatically gather codebase context.
  5. Understand the Starlight project structure

    main

    A standard Starlight project follows this directory structure:

    • src/content/docs/: Place your .md or .mdx documentation files here. Each file is automatically exposed as a route based on its filename.
    • src/assets/: Store images here to be embedded in Markdown using relative links.
    • public/: Place static assets like favicons here.
    • src/content.config.ts: Configuration for content collections.
    • astro.config.mjs: Astro configuration file.
    • package.json: Project dependencies and scripts.
  6. Install the code2prompt Python SDK locally

    main

    To install the SDK for local development, clone the repository, set up a virtual environment, install development dependencies, and use maturin to build and install the package.

    # Clone the repository
    git clone https://github.com/mufeedvh/code2prompt.git
    cd code2prompt
    
    # Set up virtual environment and install dependencies
    python3 -m venv .venv
    source .venv/bin/activate
    pip install maturin pytest
    
    # Build and install locally
    maturin develop -r
    git clone https://github.com/mufeedvh/code2prompt.git
    cd code2prompt
    python3 -m venv .venv
    source .venv/bin/activate
    pip install maturin pytest
    maturin develop -r
  7. Generate and export prompts in the TUI Output Tab

    main

    After configuring your files, settings, and templates, press Enter to generate the final prompt in the Output tab.

    Output Actions:

    • Review: Use / , PgUp, or PgDn to scroll through the generated text.
    • Copy: Press c to copy the entire prompt to your system clipboard.
    • Save: Press s to save the prompt to a local Markdown file.
    • Regenerate: If you need to change file selections, press 1 to jump back to the Selection tab, make changes, and press Enter to regenerate.