git_branch
Manage Git branches by listing existing ones, creating new branches, deleting unwanted branches, or renaming branches to organize your repository workflow.
Instructions
List, create, delete, or rename branches
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cwd | No | Repository directory | |
| action | No | Branch action | list |
| name | No | Branch name (required for create/delete/rename) | |
| newName | No | New branch name (required for rename) | |
| force | No | Force delete or creation | |
| remote | No | List remote branches |
Implementation Reference
- src/tools/git.ts:245-281 (handler)The main handler function that implements the git_branch tool logic. It handles list, create, delete, and rename actions for git branches using the executeGitCommand helper.export async function gitBranch(args: z.infer<typeof gitBranchSchema>): Promise<ToolResponse> { switch (args.action) { case 'list': const remoteFlag = args.remote ? '-r' : '-a'; return executeGitCommand(`git branch ${remoteFlag}`, args.cwd); case 'create': if (!args.name) { return { content: [{ type: "text", text: JSON.stringify({ success: false, error: 'Branch name required for create action' }, null, 2) }], isError: true }; } return executeGitCommand(`git branch ${args.name}`, args.cwd); case 'delete': if (!args.name) { return { content: [{ type: "text", text: JSON.stringify({ success: false, error: 'Branch name required for delete action' }, null, 2) }], isError: true }; } const deleteFlag = args.force ? '-D' : '-d'; return executeGitCommand(`git branch ${deleteFlag} ${args.name}`, args.cwd); case 'rename': if (!args.name || !args.newName) { return { content: [{ type: "text", text: JSON.stringify({ success: false, error: 'Both name and newName required for rename action' }, null, 2) }], isError: true }; } return executeGitCommand(`git branch -m ${args.name} ${args.newName}`, args.cwd); default: return { content: [{ type: "text", text: JSON.stringify({ success: false, error: 'Invalid branch action' }, null, 2) }], isError: true }; } }
- src/tools/git.ts:97-104 (schema)Zod schema used for input validation of the git_branch tool parameters.export const gitBranchSchema = z.object({ cwd: z.string().optional().describe('Repository directory'), action: z.enum(['list', 'create', 'delete', 'rename']).optional().default('list').describe('Branch action'), name: z.string().optional().describe('Branch name (required for create/delete/rename)'), newName: z.string().optional().describe('New branch name (required for rename)'), force: z.boolean().optional().default(false).describe('Force delete or creation'), remote: z.boolean().optional().default(false).describe('List remote branches') });
- src/tools/git.ts:561-575 (registration)MCP tool registration definition in the gitTools array, including the tool name, description, and input schema advertised to clients.{ name: 'git_branch', description: 'List, create, delete, or rename branches', inputSchema: { type: 'object', properties: { cwd: { type: 'string', description: 'Repository directory' }, action: { type: 'string', enum: ['list', 'create', 'delete', 'rename'], default: 'list', description: 'Branch action' }, name: { type: 'string', description: 'Branch name (required for create/delete/rename)' }, newName: { type: 'string', description: 'New branch name (required for rename)' }, force: { type: 'boolean', default: false, description: 'Force delete or creation' }, remote: { type: 'boolean', default: false, description: 'List remote branches' } } } },
- src/index.ts:377-379 (registration)Dispatch/registration logic in the main MCP server handler that routes calls to the git_branch tool by name, validates input, and invokes the handler.if (name === 'git_branch') { const validated = gitBranchSchema.parse(args); return await gitBranch(validated);
- src/tools/git.ts:21-61 (helper)Shared helper function used by gitBranch (and all git tools) to execute git commands and format responses.async function executeGitCommand(command: string, cwd?: string): Promise<ToolResponse> { try { const { stdout, stderr } = await execAsync(command, { cwd: cwd || process.cwd(), shell: '/bin/bash', maxBuffer: 10 * 1024 * 1024 // 10MB buffer }); return { content: [ { type: "text" as const, text: JSON.stringify({ success: true, command: command, stdout: stdout.trim(), stderr: stderr.trim(), cwd: cwd || process.cwd() }, null, 2) } ] }; } catch (error: any) { return { content: [ { type: "text" as const, text: JSON.stringify({ success: false, command: command, stdout: error.stdout?.trim() || '', stderr: error.stderr?.trim() || error.message, exitCode: error.code || 1, cwd: cwd || process.cwd() }, null, 2) } ], isError: true }; } }