---
title: Getting Started with Code2Prompt
description: A comprehensive tutorial introducing Code2Prompt's core functionality and its use across CLI, SDK, and MCP integrations.
---

import { Aside } from "@astrojs/starlight/components";
import { Tabs, TabItem } from "@astrojs/starlight/components";
import { Card, CardGrid } from "@astrojs/starlight/components";
import { LinkCard } from "@astrojs/starlight/components";

<Card title="Tutorial Overview">
  Welcome to Code2Prompt! This tutorial provides an introduction to
  using Code2Prompt to generate AI-ready prompts from your codebases. We'll
  explore its core functionality and demonstrate its usage across different
  integration methods.
</Card>

## What is Code2Prompt?

`Code2Prompt` is a system tool designed to extract codebase informations for LLMs. It extracts relevant code snippets, applies filtering, and formats the information into structured prompts optimized for LLM. This simplifies tasks like code documentation, bug detection, refactoring, and more.

Code2Prompt offers different flavors to suit various use cases:

<Tabs>
  <TabItem label="Core" icon="seti:rust">
    <Card>
    A core rust library that provides the foundation for code ingestion and
    prompt
    </Card>
  </TabItem>
  <TabItem label="CLI" icon="seti:powershell">
    <Card>
    A user-friendly command-line interface for quick prompt generation. Ideal
    for interactive use and one-off tasks.
    </Card>
  </TabItem>
  <TabItem label="SDK" icon="seti:python">
    <Card>
    A powerful Software Development Kit (SDK) for seamless integration into your
    Python projects. Perfect for automating prompt generation within larger
    workflows.
    </Card>
  </TabItem>
  <TabItem label="MCP" icon="seti:db">
    <Card>
    A Model Context Protocol (MCP) server for integration with LLM
    agents. Enables sophisticated, real-time interactions with your codebase.
    </Card>
  </TabItem>
</Tabs>

## 📥 Installation

For detailed installation instructions for all methods (CLI, SDK, MCP), please refer to the  
<LinkCard
  title="Installation Guide"
  href="/docs/how_to/install"
/>

## 🏁 Generating Prompts: A CLI Example

Let's start with a simple example using the CLI. Create a sample project:

```bash
mkdir -p my_project/{src,tests}
touch my_project/src/main.rs my_project/tests/test_1.rs
echo 'fn main() { println!("Hello, world!"); }' > my_project/src/main.rs
```

Now, generate a prompt:

```bash
code2prompt my_project
```

This copies a prompt to your clipboard. You can customize this:

- **Filtering:** `code2prompt my_project --include="*.rs" --exclude="tests/*"` (includes only `.rs` files, excludes `tests` directory)
- **Output File:** `code2prompt my_project --output-file=my_prompt.txt`
- **JSON Output:** `code2prompt my_project -O json` (structured JSON output)
- **Custom Templates:** `code2prompt my_project -t my_template.hbs` (requires creating `my_template.hbs`)

To learn more about `code2prompt`, check out the following tutorials:
<LinkCard
  title="Learn about Context Filtering"
  href="/docs/tutorials/learn_filters"
/>
<LinkCard
  title="Learn about Templating with Handlebars"
  href="/docs/tutorials/learn_templates"
/>
<LinkCard
  title="Learn More about CLI Usage Examples"
  href="/docs/how_to/cli"
/>


## 🐍 SDK Integration (Python)

For programmatic control, use the Python SDK:

```python
from code2prompt_rs import Code2Prompt

config = {
    "path": "my_project",
    "include_patterns": ["*.rs"],
    "exclude_patterns": ["tests/*"],
}

c2p = Code2Prompt(**config)
prompt = c2p.generate_prompt()
print(prompt)
```

This requires installing the SDK (`pip install code2prompt_rs`). Refer to the SDK documentation for more details.

## 🤖 Harness Integration

To reduce token usage of your harness, you can let your agent run `code2prompt` as a MCP server or as a skill. This allows agents to request code context dynamically in a way that replaces multiple `find`, `glob` and `grep` calls.

