MCP HubMCP Hub
adhikasp

mcp-git-ingest

by: adhikasp

A Model Context Protocol (MCP) server that helps read GitHub repository structure and important files.

133created 15/12/2024
Visit
GitHub
protocol

📌Overview

Purpose: The mcp-git-ingest is designed to help users read the structure and important files of GitHub repositories through the Model Context Protocol (MCP).

Overview: This framework acts as a server for extracting and analyzing GitHub repository information, providing functionalities to visualize directory structures and access specified files within repositories. It enhances interaction with GitHub repositories by integrating essential tools for repository exploration.

Key Features:

  • github_directory_structure: This function generates a tree-like representation of a GitHub repository's directory structure while skipping .git directories. It facilitates users in understanding the layout of repositories at a glance.

  • github_read_important_files: This feature reads and returns the contents of important files specified by users. It carefully handles potential errors during file access and ensures clean-up of temporary directories to maintain system performance.


MCP Git Ingest

A Model Context Protocol (MCP) server that helps read GitHub repository structure and important files.

Inspired by gitingest.

Configuration

{
    "mcpServers": {
        "mcp-git-ingest": {
            "command": "uvx",
            "args": ["--from", "git+https://github.com/adhikasp/mcp-git-ingest", "mcp-git-ingest"]
        }
    }
}

Usage

Using mcp-client-cli:

$ llm read https://github.com/adhikasp/mcp-git-ingest and determine how the code technically works

The tool first fetches the directory structure and then reads important files to analyze how the code works.

Purpose

The mcp-git-ingest is a Model Context Protocol (MCP) server designed to help read GitHub repository structures and important files. It provides two main tools:

  • github_directory_structure: Returns a tree-like representation of a repository's directory structure
  • github_read_important_files: Reads and returns the contents of specified files in a repository

Technical Implementation

Dependencies

  • Uses fastmcp for creating an MCP server
  • Uses gitpython for Git repository operations
  • Requires Python 3.8+

Key Functions

  • clone_repo(repo_url: str) -> str:

    • Creates a deterministic temporary directory based on the repository URL's hash
    • Checks if the repository is already cloned
    • Clones the repository if not present
    • Handles errors and cleanup
    • Returns the path to the cloned repository
  • get_directory_tree(path: str, prefix: str = "") -> str:

    • Recursively generates a tree-like directory structure
    • Skips .git directories
    • Uses Unicode box-drawing characters for visual representation
    • Sorts entries for consistent output
  • github_directory_structure(repo_url: str) -> str:

    • Clones the repository
    • Generates directory tree structure
    • Cleans up the temporary repository after processing
    • Returns the tree structure or an error message
  • github_read_important_files(repo_url: str, file_paths: List[str]) -> dict[str, str]:

    • Clones the repository
    • Reads specified files and returns their contents
    • Handles file reading errors
    • Cleans up temporary repository

Error Handling

  • Uses try-except blocks for repository cloning and file reading errors
  • Ensures temporary directories are cleaned up using finally blocks
  • Returns descriptive error messages on failure

Performance Optimizations

  • Uses hash-based temporary directory naming to potentially reuse cloned repositories
  • Checks for existing repositories before cloning
  • Implements cleanup to avoid accumulation of temporary files

Unique Features

  • Deterministic temporary directory creation based on repo URL
  • Unicode tree structure representation
  • Flexible file reading with robust error handling

Execution

  • Can be run as a CLI tool via mcp-git-ingest
  • Configured through pyproject.toml
  • Depends on fastmcp for MCP server functionality

This code provides a robust and flexible tool for programmatically exploring and reading GitHub repositories, emphasizing error handling and clean implementation.