RepoMap

repository·main·Indexed 41 days ago

https://github.com/pdavis68/repomapper

A tool that generates high-importance maps of codebases to help LLMs navigate complex repositories. Available as a standalone CLI or an MCP server, it uses Tree-sitter for parsing and the PageRank algorithm to rank files and symbols. It supports any language with a Tree-sitter grammar and provides tools like `repo_map` for structural overviews and `search_identifiers` for finding specific code elements.

Tokens
2.3K
Snippets
9
Records
13
Agent score
3%

What's inside RepoMap

  1. Prioritize files in RepoMap

    main

    RepoMap uses a priority system to decide which files to include in the map when token limits are reached. You can control this using the following flags:

    1. --chat-files: Highest priority. Use this for files you are currently working on.
    2. --mentioned-files: High priority. Use this for files explicitly mentioned in your current context.
    3. --other-files: Lowest priority. Used to provide additional context.

    Example of specifying priority:

    python repomap.py --chat-files main.py --other-files src/
  2. How RepoMap works

    main

    RepoMap generates a codebase map through the following pipeline:

    1. File Discovery: Scans the repository for source files.
    2. Code Parsing: Uses Tree-sitter to extract definitions and references.
    3. Graph Building: Creates a graph where files are nodes and symbol references are edges.
    4. Ranking: Applies the PageRank algorithm to rank files and symbols by importance.
    5. Token Optimization: Uses binary search to fit the most important content within the specified token limits.
    6. Output Generation: Formats the results into a readable code map.
  3. Setup RepoMap as an MCP Server

    main

    RepoMap can run as a Model Context Protocol (MCP) server using STDIO. To use it with tools like Cline, add the following configuration to your cline_mcp_settings.json:

    Note: You must replace the command and args path with the absolute path to your Python executable and the repomap_server.py file.

    {
      "mcpServers": {
        "RepoMapper": {
          "disabled": false,
          "timeout": 60,
          "type": "stdio",
          "command": "/usr/bin/python3",
          "args": [
            "/absolute/path/to/repomap_server.py"
          ]
        }
      }
    }
  4. Use RepoMap MCP Server

    main

    To start the MCP server, run the server script directly:

    python repomap_server.py

    Once running, the server listens via STDIO. Clients can call the repo_map tool, which requires a project_root parameter (an absolute path to the project being mapped).

  5. Basic Usage of RepoMap CLI

    main

    RepoMap can be used via the command line to generate a map of your codebase. Use python repomap.py followed by the target directory or files.

    Common tasks:

    • Map the current directory: python repomap.py .
    • Map a specific directory with a custom token limit: python repomap.py src/ --map-tokens 2048
    • Map specific files: python repomap.py file1.py file2.py
    # Map current directory
    python repomap.py .
    
    # Map specific directory with custom token limit
    python repomap.py src/ --map-tokens 2048
    
    # Map specific files
    python repomap.py file1.py file2.py
  6. Configure RepoMap CLI options

    main

    Use these flags to fine-tune the mapping process:

    • --mentioned-files <path>: Specify files that should have high priority.
    • --mentioned-idents <identifier>: Specify specific identifiers (like function names) for higher priority.
    • --verbose: Enable verbose output.
    • --force-refresh: Force refresh of the persistent caches.
    • --model <model_name>: Specify the model used for token counting (e.g., gpt-3.5-turbo).
    • --max-context-window <int>: Set the maximum context window size.
    • --exclude-unranked: Exclude files that have a PageRank of 0.
    # Example of advanced usage
    python repomap.py . --mentioned-files config.py --mentioned-idents "main_function" --max-context-window 8192 --exclude-unranked
  7. Search for identifiers with `search_identifiers`

    main

    The search_identifiers tool performs a case-insensitive search for identifier names (functions, classes, variables) across the project. It returns the identifier's location and a snippet of surrounding code context.

    Parameters:

    • project_root (str): Required. The absolute path to the project root.
    • query (str): Required. The identifier name to search for (use plain names without special characters).
    • max_results (int, default: 50): Maximum number of results to return.
    • context_lines (int, default: 2): Number of lines of code to show above and below the match.
    • include_definitions (bool, default: True): Whether to include where the identifier is defined.
    • include_references (bool, default: True): Whether to include where the identifier is used.

    Returns:

    • A dictionary containing results (a list of objects with file, line, name, kind, and context) or an error key.
    # Search for a class definition and see 3 lines of context
    await search_identifiers(
        project_root="/home/user/my_project",
        query="DatabaseConnection",
        context_lines=3,
        include_definitions=True
    )
  8. Use the RepoMap MCP Server tools

    main

    The RepoMapServer provides two primary tools via the Model Context Protocol (MCP) to help LLMs understand a codebase: repo_map for generating a high-level structural overview and search_identifiers for finding specific code elements. Both tools require an absolute path to the project_root.

    # Example conceptual usage of the tools via an MCP client
    
    # 1. Generate a repository map
    await repo_map(
        project_root="/abs/path/to/project",
        chat_files=["src/main.py"],
        token_limit=8192
    )
    
    # 2. Search for a specific identifier
    await search_identifiers(
        project_root="/abs/path/to/project",
        query="MyClassName"
    )
  9. Generate a repository map with `repo_map`

    main

    The repo_map tool generates a condensed map of the repository containing function prototypes, variables, and related file information. It uses a ranking system to prioritize files based on their relevance to the current conversation.

    Parameters:

    • project_root (str): Required. The absolute path to the project root.
    • chat_files (List[str], optional): Files currently in the chat context. These receive the highest ranking.
    • mentioned_files (List[str], optional): Files explicitly mentioned in the conversation. These receive a mid-level ranking boost.
    • other_files (List[str], optional): Other relevant files to consider. These receive a lower ranking boost than mentioned_files or chat_files.
    • token_limit (Any, default: 8192): The maximum number of tokens the map should occupy.
    • exclude_unranked (bool, default: False): If True, files with a PageRank of 0.0 are excluded.
    • force_refresh (bool, default: False): If True, forces a refresh of the repository map cache.
    • mentioned_idents (List[str], optional): Identifiers mentioned in the conversation to boost their ranking.
    • verbose (bool, default: False): Enables verbose logging.
    • max_context_window (int, optional): Adjusts map token limit when no chat files are provided.

    Returns:

    • A dictionary containing map (the generated string) and report (details on included/excluded files and match counts).
    • An error key if the operation fails.
    # Call repo_map with specific context prioritization
    await repo_map(
        project_root="/home/user/my_project",
        chat_files=["utils/helper.py"],
        mentioned_files=["models/user.py"],
        token_limit=4096
    )
  10. Reference: `search_identifiers` result schema

    main

    When search_identifiers succeeds, it returns a dictionary with the following structure:

    {
      "results": [
        {
          "file": "path/to/file.py",
          "line": 42,
          "name": "identifier_name",
          "kind": "def" | "ref",
          "context": "string containing the code snippet with context lines"
        }
      ]
    }