feat(git): add git_current_branch, git_default_branch, and git_remote tools#3396
Open
cblecker wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
4c886e0 to
5f4a2ca
Compare
… 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>
5f4a2ca to
7322280
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inremote/branchformat (e.g.origin/main). Usesgit ls-remote --symref(parsed via regex) to query the remote directly, with fallback torev-parsefor local ref resolution, then common branch name detection (main/master). Accepts an optionalremoteparameter (defaults to"origin").git_remote— Lists all configured remotes with their fetch and push URLs (wrapsgit remote -v).Server Details
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_statusoutput or listing all branches, which is inefficient. These dedicated tools provide direct, unambiguous answers.The
remote/branchformat forgit_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 likegit diff origin/main.How Has This Been Tested?
test_git_current_branch— normal branch detectiontest_git_current_branch_detached_head— detached HEAD statetest_git_default_branch_fallback_local— fallback to local main/mastertest_git_default_branch_with_remote— ls-remote detection with bare remotetest_git_default_branch_custom_remote— non-"origin" remote parametertest_git_default_branch_undetectable— raises ValueError when undetectabletest_git_default_branch_revparse_fallback— rev-parse path when ls-remote failstest_git_remote_no_remotes— empty output for repos with no remotestest_git_remote_with_remote— remote name and URL appear in outputBreaking Changes
None. Three new tools are added; existing tools are unchanged.
Types of changes
Checklist