generate_commit_message
Create and validate conventional commit messages by specifying type, subject, scope, and optional details. Includes validation and optional git preview for accurate and structured commits.
Instructions
Generate a commit message with validation using provided parameters.
Args: type: The commit type (e.g., 'feat', 'fix', 'docs') subject: The commit subject/description body: Optional commit body with detailed description scope: Optional scope of the change breaking: Whether this is a breaking change footer: Optional footer (e.g., issue references) include_git_preview: Whether to include git preview in response
Returns: Dict containing the generated message and validation status
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| body | No | ||
| breaking | No | ||
| footer | No | ||
| include_git_preview | No | ||
| scope | No | ||
| subject | Yes | ||
| type | Yes |
Implementation Reference
- Core handler function for the 'generate_commit_message' MCP tool. Takes input parameters, builds answers dict, generates and validates commit message using CommitzenService, handles git preview optionally, and returns structured response.@mcp.tool() @handle_errors(log_errors=True) def generate_commit_message( type: str, subject: str, body: Optional[str] = None, scope: Optional[str] = None, breaking: Optional[bool] = False, footer: Optional[str] = None, include_git_preview: bool = False, ) -> Dict[str, Any]: """ Generate a commit message with validation using provided parameters. Args: type: The commit type (e.g., 'feat', 'fix', 'docs') subject: The commit subject/description body: Optional commit body with detailed description scope: Optional scope of the change breaking: Whether this is a breaking change footer: Optional footer (e.g., issue references) include_git_preview: Whether to include git preview in response Returns: Dict containing the generated message and validation status """ # Build answers dictionary with all fields (adapter will handle mapping) answers = { "type": type, "prefix": type, # Some plugins expect 'prefix' "subject": subject, "body": body or "", "scope": scope or "", "breaking": breaking or False, "is_breaking_change": breaking or False, # Some plugins expect this name "footer": footer or "", } # Generate the message message = service.generate_message(answers) # Validate the generated message is_valid = service.validate_message(message) if not is_valid: raise create_validation_error( "Invalid commit message format", validation_type="commit_message", invalid_value=f"{type}: {subject}", ) result = { "message": message, "is_valid": is_valid, "parameters": { "type": type, "subject": subject, "body": body, "scope": scope, "breaking": breaking, "footer": footer, }, } # Add git preview if requested and available if include_git_preview and service.git_enabled: try: # Import preview function from git_tools from .git_tools import preview_git_commit # Use the current service's repository path for preview repo_path = ( str(service.git_service.repo_path) if service.git_service else None ) if repo_path: git_preview = preview_git_commit(message, repo_path) result["git_preview"] = git_preview else: result["git_preview_error"] = ( "No repository path available for git preview" ) except Exception as e: logger.warning(f"Failed to include git preview: {e}") result["git_preview_error"] = str(e) elif include_git_preview and not service.git_enabled: result["git_preview_error"] = ( "Git operations not available - not in a git repository" ) return create_success_response(result)
- src/commit_helper_mcp/mcp_server.py:12-17 (registration)Imports the 'message_tools' module (line 13), which executes the @mcp.tool() decorator on generate_commit_message, thereby registering it as an MCP tool. Other server modules are also imported here.from .server.base_server import mcp from .server import message_tools from .server import git_tools from .server import workflow_tools from .server import enhanced_tools from .server import resources
- src/commit_helper_mcp/mcp_server.py:26-31 (registration)Explicit import of the generate_commit_message function from message_tools for re-export and backward compatibility with tests, confirming its availability in the server module.from .server.message_tools import ( generate_commit_message, create_commit_message, validate_commit_message, get_commit_types, )
- Usage of generate_commit_message within the commit_workflow_step tool as part of the multi-step workflow (generate step).from .message_tools import generate_commit_message message_result = generate_commit_message( type=workflow_data["type"], subject=workflow_data["subject"], body=workflow_data.get("body"), scope=workflow_data.get("scope"), breaking=workflow_data.get("breaking", False), footer=workflow_data.get("footer"), )
- Usage of generate_commit_message in the generate_and_commit tool to generate message before git operations.from .message_tools import generate_commit_message # First generate the commit message message_result = generate_commit_message( type=type, subject=subject, body=body, scope=scope, breaking=breaking, footer=footer, )