git

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 13 Imported by: 0

README

Local Git Tools for GitHub MCP Server

This package provides local git repository operations for the GitHub MCP Server. It enables AI assistants to interact with local git repositories using standard git commands.

Attribution

This code is adapted from the git-mcp-go project by Gero Posmyk-Leinemann.

The code has been adapted to integrate with the omnigit-mcp architecture while preserving the original functionality and design.

Overview

The git tools package provides 23 tools for working with local git repositories:

Read-Only Tools
  • git_status - Show the working tree status
  • git_diff_unstaged - Show changes in the working directory
  • git_diff_staged - Show changes staged for commit
  • git_diff - Show differences between commits, branches, or files
  • git_log - Show commit history
  • git_show - Show contents of a specific commit
  • git_list_repositories - List all configured repositories
  • git_worktree_list - List all worktrees in the repository
Write Tools
  • git_add - Add file contents to the staging area
  • git_commit - Record changes to the repository
  • git_reset - Unstage all staged changes
  • git_create_branch - Create a new branch and automatically check it out
  • git_checkout - Switch branches
  • git_init - Initialize a new git repository
  • git_push - Push local commits to remote repository with automatic upstream tracking
  • git_pull - Pull changes from remote repository with automatic rebase and prune
  • git_apply_patch_string - Apply a patch from a string
  • git_apply_patch_file - Apply a patch from a file
Worktree Tools
  • git_worktree_add - Create a new worktree at a specified path
  • git_worktree_remove - Remove an existing worktree
  • git_worktree_lock - Lock a worktree to prevent pruning
  • git_worktree_unlock - Unlock a previously locked worktree
  • git_worktree_prune - Remove stale worktree administrative files

Architecture

Package Structure
pkg/git/
├── README.md                    # This file
├── tools.go                     # MCP tool definitions
└── gitops/
    ├── interface.go             # GitOperations interface
    ├── utils.go                 # Shared utilities
    └── shell/
        └── operations.go        # Shell-based git implementation
Key Components
  1. GitOperations Interface (gitops/interface.go)

    • Defines the contract for all git operations
    • Allows for different implementations (shell-based, go-git, etc.)
  2. Shell Implementation (gitops/shell/operations.go)

    • Implements GitOperations using git CLI commands
    • Executes git commands via shell
  3. MCP Tools (tools.go)

    • Wraps git operations as MCP tools
    • Handles parameter validation and error handling
    • Integrates with the inventory system
  4. GitToolDependencies Interface

    • Provides dependency injection for tools
    • Supplies GitOperations implementation and repository paths
Security

The package includes path validation to ensure:

  • Only configured repositories can be accessed
  • Paths are validated as actual git repositories (contain .git directory)
  • Paths are converted to absolute paths for consistency

Integration

To integrate these tools into the GitHub MCP Server:

  1. Implement the GitToolDependencies interface
  2. Register tools using AllGitTools(translationHelper)
  3. Configure allowed repository paths
  4. Add tools to the server's inventory

Differences from Original

The main adaptations from git-mcp-go include:

  1. Tool Registration Pattern: Uses inventory.NewServerToolFromHandler instead of direct MCP tool registration
  2. Dependency Injection: Uses GitToolDependencies interface for cleaner separation
  3. Toolset Metadata: Tools are grouped under ToolsetMetadataLocalGit toolset
  4. Translation Support: Integrated with omnigit-mcp's translation system
  5. Error Handling: Uses utils.NewToolResultError and utils.NewToolResultText helpers

License

This adapted code maintains the original MIT License from git-mcp-go. See the original repository for full license details.

Credits

Special thanks to Gero Posmyk-Leinemann for creating the original git-mcp-go project, which provided the foundation for these local git tools.

Documentation

Overview

Package git provides local Git repository tools for the GitHub MCP Server.

This code is adapted from git-mcp-go by Gero Posmyk-Leinemann and contributors. Original source: https://github.com/geropl/git-mcp-go Copyright (c) Gero Posmyk-Leinemann <gero@gitpod.io>

Index

Constants

This section is empty.

Variables

View Source
var ErrGitDepsNotInContext = errors.New("ToolDependencies not found in context; use ContextWithGitDeps to inject")

ErrGitDepsNotInContext is returned when ToolDependencies is not found in context.

View Source
var ToolsetMetadataLocalGit = inventory.ToolsetMetadata{
	ID:          "local_git",
	Description: "Local Git repository operations - work with git repositories on your local machine (status, diff, commit, push, pull, branches, etc.)",
	Icon:        "git-branch",
}

ToolsetMetadataLocalGit defines the local git toolset metadata

Functions

func Add

Add creates a tool to add files to staging area

func AddWorktree added in v0.2.0

AddWorktree creates a tool to add a new worktree

func AllGitTools

AllGitTools returns all git tools

func ApplyPatchFile

ApplyPatchFile creates a tool to apply a patch from a file

func ApplyPatchString

ApplyPatchString creates a tool to apply a patch from a string

func Checkout

Checkout creates a tool to switch branches

func Commit

Commit creates a tool to commit changes

func ContextWithGitDeps

func ContextWithGitDeps(ctx context.Context, deps ToolDependencies) context.Context

ContextWithGitDeps returns a new context with the ToolDependencies stored in it. This is used to inject dependencies at request time rather than at registration time, avoiding expensive closure creation during server initialization.

func CreateBranch

CreateBranch creates a tool to create a new branch

func Diff

Diff creates a tool to show differences between branches or commits

func DiffStaged

DiffStaged creates a tool to show staged changes

func DiffUnstaged

DiffUnstaged creates a tool to show unstaged changes

func Init

Init creates a tool to initialize a new repository

func ListRepositories

ListRepositories creates a tool to list all available repositories

func ListWorktrees added in v0.2.0

ListWorktrees creates a tool to list all worktrees

func LockWorktree added in v0.2.0

LockWorktree creates a tool to lock a worktree

func Log

Log creates a tool to show commit logs

func PruneWorktrees added in v0.2.0

PruneWorktrees creates a tool to prune worktree information

func Pull

Pull creates a tool to pull changes from remote

func Push

Push creates a tool to push changes to remote

func RemoveWorktree added in v0.2.0

RemoveWorktree creates a tool to remove a worktree

func Reset

Reset creates a tool to unstage changes

func Show

Show creates a tool to show commit contents

func Status

Status creates a tool to show the working tree status

func UnlockWorktree added in v0.2.0

UnlockWorktree creates a tool to unlock a worktree

Types

type ToolDependencies

type ToolDependencies interface {
	GetGitOps() gitops.GitOperations
	GetRepoPaths() []string
}

ToolDependencies defines the dependencies needed by git tools

func MustGitDepsFromContext

func MustGitDepsFromContext(ctx context.Context) ToolDependencies

MustGitDepsFromContext extracts ToolDependencies from context. Panics if deps are not found - callers must ensure ContextWithGitDeps was called.

Directories

Path Synopsis
Package gitops provides Git operations interface and implementations.
Package gitops provides Git operations interface and implementations.
shell
Package shell provides Git operations using git CLI commands.
Package shell provides Git operations using git CLI commands.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL