git_hooks
Manage Git hooks in local repositories by listing, creating, enabling, or disabling hooks such as pre-commit or post-merge to automate and enforce workflows.
Instructions
Manage git hooks in the repository.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Hook action (list, get, create, enable, disable) | list |
| hook_name | No | Name of the hook (e.g., 'pre-commit', 'post-merge') | |
| repo_path | Yes | The path to the local Git repository | |
| script | No | Script content for the hook (for create action) |
Implementation Reference
- src/handlers/other-operations.js:11-205 (handler)Main handler function implementing git_hooks tool: manages Git hooks with actions list, get, create (list hooks, read hook content, create executable hook).export async function handleGitHooks({ repo_path, action, hook_name = "", script = "", }) { try { // Path to the hooks directory const hooksDir = path.join(repo_path, ".git", "hooks"); switch (action) { case "list": // Get all available hooks const files = await fs.readdir(hooksDir); const hooks = []; for (const file of files) { // Filter out sample hooks if (!file.endsWith(".sample")) { const hookPath = path.join(hooksDir, file); const stats = await fs.stat(hookPath); hooks.push({ name: file, path: hookPath, size: stats.size, executable: (stats.mode & 0o111) !== 0, // Check if executable }); } } return { content: [ { type: "text", text: JSON.stringify( { success: true, hooks: hooks, }, null, 2 ), }, ], }; case "get": if (!hook_name) { return { content: [ { type: "text", text: JSON.stringify( { error: "Hook name is required for get action" }, null, 2 ), }, ], isError: true, }; } const hookPath = path.join(hooksDir, hook_name); // Check if hook exists if (!(await fs.pathExists(hookPath))) { return { content: [ { type: "text", text: JSON.stringify( { error: `Hook '${hook_name}' does not exist` }, null, 2 ), }, ], isError: true, }; } // Read hook content const hookContent = await fs.readFile(hookPath, "utf8"); const stats = await fs.stat(hookPath); return { content: [ { type: "text", text: JSON.stringify( { success: true, name: hook_name, content: hookContent, executable: (stats.mode & 0o111) !== 0, }, null, 2 ), }, ], }; case "create": if (!hook_name) { return { content: [ { type: "text", text: JSON.stringify( { error: "Hook name is required for create action" }, null, 2 ), }, ], isError: true, }; } if (!script) { return { content: [ { type: "text", text: JSON.stringify( { error: "Script content is required for create action" }, null, 2 ), }, ], isError: true, }; } const createHookPath = path.join(hooksDir, hook_name); // Write hook content await fs.writeFile(createHookPath, script); // Make hook executable await fs.chmod(createHookPath, 0o755); return { content: [ { type: "text", text: JSON.stringify( { success: true, message: `Created hook '${hook_name}'`, name: hook_name, executable: true, }, null, 2 ), }, ], }; default: return { content: [ { type: "text", text: JSON.stringify( { error: `Unknown hook action: ${action}` }, null, 2 ), }, ], isError: true, }; } } catch (error) { return { content: [ { type: "text", text: JSON.stringify( { error: `Failed to manage hook: ${error.message}` }, null, 2 ), }, ], isError: true, }; } }
- src/server.js:745-769 (schema)Input schema validation for git_hooks tool parameters.inputSchema: { type: "object", properties: { repo_path: { type: "string", description: "The path to the local Git repository", }, action: { type: "string", description: "Hook action (list, get, create, enable, disable)", default: "list", enum: ["list", "get", "create", "enable", "disable"], }, hook_name: { type: "string", description: "Name of the hook (e.g., 'pre-commit', 'post-merge')", }, script: { type: "string", description: "Script content for the hook (for create action)", }, }, required: ["repo_path", "action"], },
- src/server.js:923-923 (registration)Registration of git_hooks handler in the tools map used by MCP server.git_hooks: handleGitHooks,
- src/handlers/index.js:83-83 (registration)Re-export of handleGitHooks from other-operations.js for centralized imports.handleGitHooks,
- src/server.js:34-34 (registration)Import of handleGitHooks into server.js from handlers/index.js.handleGitHooks,