stage_files_and_commit
Stage specific files and commit them with a custom message for selective version control in Git repositories.
Instructions
Stage specific files and commit with provided message.
Useful for selective commits of specific files.
Args: files: List of file paths to stage message: Commit message to use repo_path: Path to git repository sign_off: Whether to add sign-off to commit (default: True) dry_run: Preview only (default True for safety)
Returns: Dict containing staging and commit results
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | ||
| message | Yes | ||
| repo_path | Yes | ||
| sign_off | No | ||
| dry_run | No |
Output Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The core handler function for the 'stage_files_and_commit' tool, decorated with @mcp.tool(). It handles staging specific files, validates the commit message, performs a dry-run preview by default, and executes staging and commit when dry_run=False.
@mcp.tool() def stage_files_and_commit( files: List[str], message: str, repo_path: str, sign_off: bool = True, dry_run: bool = True, ) -> Dict[str, Any]: """ Stage specific files and commit with provided message. Useful for selective commits of specific files. Args: files: List of file paths to stage message: Commit message to use repo_path: Path to git repository sign_off: Whether to add sign-off to commit (default: True) dry_run: Preview only (default True for safety) Returns: Dict containing staging and commit results """ try: # Initialize service for the specified repository try: target_service = CommitzenService(repo_path=repo_path) except Exception as e: return { "git_enabled": False, "error": f"Failed to initialize service for repository '{repo_path}': {e}", "success": False, "files": files, "message": message, "dry_run": dry_run, "repository_path": repo_path, } if not target_service.git_enabled: return { "git_enabled": False, "error": "Git operations not available - not in a git repository", "success": False, "files": files, "message": message, "dry_run": dry_run, "repository_path": repo_path, } # Validate message format first is_valid = target_service.validate_message(message) if not is_valid: return { "error": "Invalid commit message format", "success": False, "files": files, "message": message, "is_valid": False, "dry_run": dry_run, "git_enabled": True, "repository_path": repo_path, } if dry_run: # Preview mode - don't actually stage or commit return { "success": True, "files": files, "message": message, "is_valid": is_valid, "dry_run": True, "git_enabled": True, "repository_path": repo_path, "preview": { "would_stage": files, "would_commit": True, "commit_message": message, }, } else: # Actually stage files and commit if not target_service.git_service: return { "error": "Git service not available", "success": False, "files": files, "message": message, "dry_run": dry_run, "git_enabled": True, "repository_path": repo_path, } # Stage the files stage_result = target_service.git_service.add_files( *files, force_execute=True ) if not stage_result.get("success", False): return { "error": f"Failed to stage files: {stage_result.get('error', 'Unknown error')}", "success": False, "files": files, "message": message, "dry_run": dry_run, "git_enabled": True, "repository_path": repo_path, "stage_result": stage_result, } # Now commit the staged files commit_result = target_service.execute_commit_operation( message=message, force_execute=True, sign_off=sign_off ) return { "success": commit_result.get("success", False), "files": files, "message": message, "is_valid": is_valid, "dry_run": False, "git_enabled": True, "repository_path": repo_path, "stage_result": stage_result, "commit_result": commit_result, } except Exception as e: logger.error(f"Failed to stage files and commit: {e}") return { "error": str(e), "success": False, "files": files, "message": message, "dry_run": dry_run, "git_enabled": False, "repository_path": repo_path, } - src/commit_helper_mcp/mcp_server.py:33-43 (registration)Explicit import of the stage_files_and_commit function from git_tools.py in the main MCP server module, which ensures the tool is available and registered via the @mcp.tool decorator during module initialization.
# Git tools from .server.git_tools import ( get_git_implementation_info, get_enhanced_git_status, get_git_status, preview_git_commit, execute_git_commit, generate_and_commit, validate_commit_readiness, stage_files_and_commit, )