Skip to content

feat(git): add git worktree tools (list, add, remove) - #4791

Open
karawitan wants to merge 1 commit into
modelcontextprotocol:mainfrom
karawitan:feat/git-worktree-support
Open

feat(git): add git worktree tools (list, add, remove)#4791
karawitan wants to merge 1 commit into
modelcontextprotocol:mainfrom
karawitan:feat/git-worktree-support

Conversation

@karawitan

Copy link
Copy Markdown

Description

Add three new tools to mcp-server-git for managing git worktrees:

  • git_worktree_list — List all worktrees of a repository
  • git_worktree_add — Create a new worktree, optionally creating a new branch for it
  • git_worktree_remove — Remove a worktree, with optional force flag for modified worktrees

Server Details

  • Server: git (mcp-server-git)
  • Changes to: tools

Motivation and Context

Git worktrees are a standard git feature for working on multiple branches simultaneously in separate directories without stashing. They are commonly used in parallel development workflows (e.g., working on a feature branch while keeping the main branch checked out for quick fixes).

The official mcp-server-git currently has no worktree support, forcing LLM agents to fall back to shell commands. Several third-party MCP git servers already implement worktree tools (e.g., mcp-server-git-rs, lileeei/mcp-git-tools, georgeasoto/worktree-tools-mcp), indicating clear demand. Adding this to the reference server provides a consistent, tested, and security-hardened implementation.

How Has This Been Tested?

  • 8 new unit tests covering: list, add (with branch, with base branch, default branch), remove, force remove, and flag injection guards for both add and remove
  • All 55 tests pass (8 new + 47 existing)
  • pyright: 0 errors, 0 warnings
  • ruff: All checks passed

Breaking Changes

None. This is purely additive — three new tools are registered alongside the existing 12.

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the MCP Protocol Documentation
  • My changes follows MCP security best practices
  • I have updated the server's README accordingly
  • I have tested this with an LLM client
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options

Additional context

Security

Each new tool includes defense-in-depth guards rejecting arguments starting with - to prevent flag injection, matching the established pattern in existing tools (git_diff, git_checkout, git_show, git_create_branch, git_log, git_branch). The git_worktree_remove tool is annotated with destructiveHint=true since it deletes a working directory.

Implementation

GitPython does not provide a high-level worktree API (it only has a WorkTreeRepositoryUnsupported exception). The implementation uses GitPython's low-level repo.git.worktree command interface, which wraps the git worktree subcommand directly. This is the same approach the existing git_branch tool uses (repo.git.branch(...)).

Tool annotations

Tool readOnly destructive idempotent
git_worktree_list true false true
git_worktree_add false false false
git_worktree_remove false true false

Add three new tools to mcp-server-git for managing git worktrees:

- git_worktree_list: List all worktrees of a repository
- git_worktree_add: Create a new worktree, optionally with a new branch
- git_worktree_remove: Remove a worktree, with optional force flag

Each tool includes defense-in-depth guards rejecting arguments starting
with '-' to prevent flag injection, matching the pattern used by existing
tools (git_diff, git_checkout, git_show, etc.).

The implementation uses GitPython's low-level `repo.git.worktree` command
interface since GitPython does not provide a high-level worktree API.

All 55 tests pass (8 new worktree tests + 47 existing), pyright clean,
ruff clean.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant