Git & VCS Integration
Relevant source files
The following files were used as context for generating this wiki page:
The Git & VCS Integration module in the Memory Context Browser (MCB) provides a robust interface for interacting with version control systems, primarily Git. It enables AI coding agents to perform repository-level operations such as indexing codebases, comparing branches, and analyzing the impact of changes through the Model Context Protocol (MCP).
This integration follows a Clean Architecture approach, where the VcsProvider trait defines the domain ports, and the GitProvider serves as the concrete implementation using libgit2. This structure ensures that VCS operations are decoupled from the core logic, allowing for high-performance indexing and semantic understanding of repositories.
Sources: README.md:21-22, crates/mcb-providers/src/vcs/git.rs:16-25
Architecture and Components
The VCS system is structured into several layers consistent with the project's architecture. The server layer handles MCP tool requests, which are then dispatched to the infrastructure and provider layers for execution.
VCS Provider Architecture
The system utilizes a provider-based pattern where specific VCS operations are abstracted behind the VcsProvider trait. The GitProvider implements this trait, providing methods to open repositories, list branches, retrieve commit history, and perform diffing operations.
The diagram shows the flow from MCP tool actions in the server to the concrete Git implementation.
Sources: crates/mcb-providers/src/vcs/git.rs:194-350, AGENTS.md:237-251
Key Features and Tools
MCB exposes several VCS-specific tools through the MCP protocol. These are grouped into the VCS family of operations.
| Tool Name | Action | Description |
|---|---|---|
list_repos | ListRepositories | Scans the filesystem (up to depth 3) for Git repositories and lists them. |
index_repo | IndexRepository | Indexes a repository, including files and optionally commit history, for semantic search. |
compare_branches | CompareBranches | Generates a diff and impact analysis between two specified branches. |
analyze_impact | AnalyzeImpact | Analyzes the potential impact of changes within the repository context. |
Sources: README.md:95, crates/mcb-server/src/args/vcs.rs:10-22, crates/mcb-providers/src/vcs/git.rs:319-350
Repository Indexing Logic
The indexing process is a critical part of providing context to AI agents. It involves walking the repository tree, filtering files based on ignore patterns, and optionally processing commit history.
Indexing Workflow
When index_repository is called, the system performs the following steps:
- Open Repository: Uses
libgit2to resolve the repository path. - Load Configuration: Looks for a
.mcp-context.tomlfile in the repo root for local settings likedepthandignore_patterns. - Count Files: Walks the Git tree for specified branches (defaulting to the default branch) and applies filter patterns.
- Process Commits: If
include_commitsis true, it retrieves commit history up to the configured depth.
The flowchart illustrates the sequential logic of the indexing handler.
Sources: crates/mcb-server/src/handlers/vcs/index_repo.rs:19-58, crates/mcb-providers/src/vcs/git.rs:252-273
Git Operations Implementation
The GitProvider encapsulates low-level libgit2 calls to provide domain-specific entities.
Core Data Structures
- VcsRepository: Represents an opened repository with its ID, path, and default branch.
- VcsBranch: Contains metadata about a branch, including its name, head commit hash, and upstream status.
- VcsCommit: A domain entity representing a Git commit, including author details and parent hashes.
- RefDiff: Represents the result of a diff operation between two references, containing a list of
FileDiffobjects and total addition/deletion counts.
File and Diff Handling
The provider converts internal git2 status codes to a domain-neutral DiffStatus (Added, Deleted, Renamed, Modified). It uses revwalk to traverse commit history and tree.walk to list files within a specific branch.
// Example of tree walking in git.rs
tree.walk(git2::TreeWalkMode::PreOrder, |dir, entry| {
if entry.kind() == Some(git2::ObjectType::Blob) && let Some(name) = entry.name() {
let path = if dir.is_empty() { PathBuf::from(name) } else { PathBuf::from(dir).join(name) };
files.push(path);
}
git2::TreeWalkResult::Ok
})
Sources: crates/mcb-providers/src/vcs/git.rs:88-100, crates/mcb-providers/src/vcs/git.rs:275-298, crates/mcb-providers/src/vcs/git.rs:114-131
Configuration and Defaults
VCS behavior can be tuned via arguments in the MCP tool call or through a local .mcp-context.toml file.
| Parameter | Default Value | Description |
|---|---|---|
depth | 50 (config) / 1000 (fallback) | The number of commits to traverse during history indexing. |
ignore_patterns | Empty List | Gitignore-style patterns used to skip files during indexing. |
include_commits | false | Boolean flag to determine if commit history should be indexed. |
Sources: crates/mcb-server/src/handlers/vcs/index_repo.rs:125-138, crates/mcb-server/src/args/vcs.rs:59-65
Conclusion
The Git & VCS Integration provides the foundational layer for codebase awareness in MCB. By combining high-performance Git operations with structured MCP tools, it allows AI agents to navigate complex repositories, understand change history, and maintain a semantically indexed view of the source code. This integration ensures that the agent's context is always grounded in the actual state of the version control system.