Skip to content

feat(git): add git_current_branch, git_default_branch, and git_remote tools#3396

Open
cblecker wants to merge 1 commit intomodelcontextprotocol:mainfrom
cblecker:cblecker/git-current-default-branch-tools
Open

feat(git): add git_current_branch, git_default_branch, and git_remote tools#3396
cblecker wants to merge 1 commit intomodelcontextprotocol:mainfrom
cblecker:cblecker/git-current-default-branch-tools

Conversation

@cblecker
Copy link
Contributor

@cblecker cblecker commented Feb 24, 2026

Description

Add three new tools to the Git MCP server for querying branch and remote information:

  • git_current_branch — Returns the name of the currently checked out branch, or the short commit SHA with a "HEAD detached" indicator when HEAD is detached.
  • git_default_branch — Determines the default branch for a remote, returning in remote/branch format (e.g. origin/main). Uses git ls-remote --symref (parsed via regex) to query the remote directly, with fallback to rev-parse for local ref resolution, then common branch name detection (main/master). Accepts an optional remote parameter (defaults to "origin").
  • git_remote — Lists all configured remotes with their fetch and push URLs (wraps git remote -v).

Server Details

  • Server: git
  • Changes to: tools (three new tools added)

Motivation and Context

LLMs working with repositories frequently need to know the current branch, the default/mainline branch, and what remotes are configured. Currently this requires parsing git_status output or listing all branches, which is inefficient. These dedicated tools provide direct, unambiguous answers.

The remote/branch format for git_default_branch (e.g. origin/main) is more useful than a bare branch name, as it unambiguously identifies the remote tracking ref and can be used directly in git commands like git diff origin/main.

How Has This Been Tested?

  • All 44 tests pass (35 existing + 9 new), confirming no regressions
  • New tests cover:
    • test_git_current_branch — normal branch detection
    • test_git_current_branch_detached_head — detached HEAD state
    • test_git_default_branch_fallback_local — fallback to local main/master
    • test_git_default_branch_with_remote — ls-remote detection with bare remote
    • test_git_default_branch_custom_remote — non-"origin" remote parameter
    • test_git_default_branch_undetectable — raises ValueError when undetectable
    • test_git_default_branch_revparse_fallback — rev-parse path when ls-remote fails
    • test_git_remote_no_remotes — empty output for repos with no remotes
    • test_git_remote_with_remote — remote name and URL appear in output

Breaking Changes

None. Three new tools are added; existing tools are unchanged.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

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

@cblecker cblecker force-pushed the cblecker/git-current-default-branch-tools branch from 4c886e0 to 5f4a2ca Compare February 24, 2026 21:38
@cblecker cblecker changed the title feat(git): add git_current_branch and git_default_branch tools feat(git): add git_current_branch, git_default_branch, and git_remote tools Feb 24, 2026
… tools

Add three new tools to the Git MCP server:

- git_current_branch: Returns the active branch name, or the short
  SHA with a detached HEAD indicator when HEAD is detached.
- git_default_branch: Determines the default branch for a remote,
  returning in 'remote/branch' format (e.g. 'origin/main'). Uses
  git ls-remote --symref (parsed via regex), with fallback to
  rev-parse for local ref resolution, then common branch name
  detection. Accepts an optional remote parameter (defaults to
  "origin").
- git_remote: Lists all configured remotes with their fetch and
  push URLs (wraps git remote -v).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@cblecker cblecker force-pushed the cblecker/git-current-default-branch-tools branch from 5f4a2ca to 7322280 Compare February 25, 2026 16:18
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