git_branch
List local, remote, or all Git branches from a repository. Optionally filter branches that contain or do not contain a specific commit.
Instructions
List Git branches
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | The path to the Git repository. | |
| branch_type | Yes | Whether to list local branches ('local'), remote branches ('remote') or all branches('all'). | |
| contains | No | The commit sha that branch should contain. Do not pass anything to this param if no commit sha is specified | |
| not_contains | No | The commit sha that branch should NOT contain. Do not pass anything to this param if no commit sha is specified |
Implementation Reference
- Pydantic model defining the input schema for the git_branch tool: repo_path, branch_type, optional contains/not_contains commit SHAs.
class GitBranch(BaseModel): repo_path: str = Field( ..., description="The path to the Git repository.", ) branch_type: str = Field( ..., description="Whether to list local branches ('local'), remote branches ('remote') or all branches('all').", ) contains: Optional[str] = Field( None, description="The commit sha that branch should contain. Do not pass anything to this param if no commit sha is specified", ) not_contains: Optional[str] = Field( None, description="The commit sha that branch should NOT contain. Do not pass anything to this param if no commit sha is specified", ) - src/git/src/mcp_server_git/server.py:96-109 (registration)Enum registration of 'git_branch' as GitTools.BRANCH.
class GitTools(str, Enum): STATUS = "git_status" DIFF_UNSTAGED = "git_diff_unstaged" DIFF_STAGED = "git_diff_staged" DIFF = "git_diff" COMMIT = "git_commit" ADD = "git_add" RESET = "git_reset" LOG = "git_log" CREATE_BRANCH = "git_create_branch" CHECKOUT = "git_checkout" SHOW = "git_show" BRANCH = "git_branch" - src/git/src/mcp_server_git/server.py:430-441 (registration)Tool registration in list_tools() listing GitTools.BRANCH with its schema.
Tool( name=GitTools.BRANCH, description="List Git branches", inputSchema=GitBranch.model_json_schema(), annotations=ToolAnnotations( readOnlyHint=True, destructiveHint=False, idempotentHint=True, openWorldHint=False, ), ) ] - Handler function that executes the 'git_branch' tool logic: validates inputs, then calls repo.git.branch() with appropriate flags.
def git_branch(repo: git.Repo, branch_type: str, contains: str | None = None, not_contains: str | None = None) -> str: # Defense in depth: reject values starting with '-' to prevent flag injection if contains and contains.startswith("-"): raise BadName(f"Invalid contains value: '{contains}' - cannot start with '-'") if not_contains and not_contains.startswith("-"): raise BadName(f"Invalid not_contains value: '{not_contains}' - cannot start with '-'") match contains: case None: contains_sha = (None,) case _: contains_sha = ("--contains", contains) match not_contains: case None: not_contains_sha = (None,) case _: not_contains_sha = ("--no-contains", not_contains) match branch_type: case 'local': b_type = None case 'remote': b_type = "-r" case 'all': b_type = "-a" case _: return f"Invalid branch type: {branch_type}" # None value will be auto deleted by GitPython branch_info = repo.git.branch(b_type, *contains_sha, *not_contains_sha) return branch_info - src/git/src/mcp_server_git/server.py:570-580 (registration)Dispatch in call_tool() that maps 'git_branch' name to the git_branch() handler function, passing extracted arguments.
case GitTools.BRANCH: result = git_branch( repo, arguments.get("branch_type", 'local'), arguments.get("contains", None), arguments.get("not_contains", None), ) return [TextContent( type="text", text=result )]