git_add
Stage files for commit in Git repositories. Use this tool to prepare file changes for version control by adding them to the staging area before committing.
Instructions
Stage files for commit. Use "." to stage all changes
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | File(s) to stage. Use "." for all files | |
| cwd | No | Repository directory |
Implementation Reference
- src/tools/git.ts:217-220 (handler)The core handler function that processes input arguments, formats the git add command, and executes it via the shared executeGitCommand helper.export async function gitAdd(args: z.infer<typeof gitAddSchema>): Promise<ToolResponse> { const files = Array.isArray(args.files) ? args.files.join(' ') : args.files; return executeGitCommand(`git add ${files}`, args.cwd); }
- src/tools/git.ts:70-73 (schema)Zod schema used for input validation in the git_add handler and dispatch.export const gitAddSchema = z.object({ files: z.union([z.string(), z.array(z.string())]).describe('File(s) to stage. Use "." for all files'), cwd: z.string().optional().describe('Repository directory') });
- src/index.ts:361-364 (registration)Dispatch logic in the main MCP server that routes 'git_add' tool calls to the handler after schema validation.if (name === 'git_add') { const validated = gitAddSchema.parse(args); return await gitAdd(validated); }
- src/tools/git.ts:502-519 (registration)MCP tool registration object defining name, description, and JSON input schema for tool listing.{ name: 'git_add', description: 'Stage files for commit. Use "." to stage all changes', inputSchema: { type: 'object', properties: { files: { oneOf: [ { type: 'string' }, { type: 'array', items: { type: 'string' } } ], description: 'File(s) to stage. Use "." for all files' }, cwd: { type: 'string', description: 'Repository directory' } }, required: ['files'] } },
- src/tools/git.ts:21-61 (helper)Shared helper function that executes git commands asynchronously and formats success/error 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 }; } }