list_commits
Retrieve GitHub repository commits by specifying owner, repo, SHA, or pagination. Returns a structured MCP response for tracking or analyzing commit history.
Instructions
List commits in a GitHub repository.
Args:
params: Dictionary with commit parameters
- owner: Repository owner (username or organization)
- repo: Repository name
- page: Page number (optional)
- per_page: Results per page (optional)
- sha: Branch name or commit SHA (optional)
Returns:
MCP response with list of commits
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| params | Yes |
Implementation Reference
- The main handler function for the list_commits tool, decorated with @tool(). It validates input parameters using ListCommitsParams, calls the repositories.list_commits operation, formats the result as JSON in an MCP response, and handles various errors.@tool() def list_commits(params: Dict) -> Dict: """List commits in a GitHub repository. Args: params: Dictionary with commit parameters - owner: Repository owner (username or organization) - repo: Repository name - page: Page number (optional) - per_page: Results per page (optional) - sha: Branch name or commit SHA (optional) Returns: MCP response with list of commits """ try: logger.debug(f"list_commits called with params: {params}") # Convert dict to Pydantic model commits_params = ListCommitsParams(**params) # Call operation result = repositories.list_commits(commits_params) logger.debug(f"Got {len(result)} commits") return { "content": [{"type": "text", "text": json.dumps(result, indent=2)}] } except ValidationError as e: logger.error(f"Validation error: {e}") return { "content": [{"type": "error", "text": f"Validation error: {str(e)}"}], "is_error": True } except GitHubError as e: logger.error(f"GitHub error: {e}") return { "content": [{"type": "error", "text": format_github_error(e)}], "is_error": True } except Exception as e: logger.error(f"Unexpected error: {e}") logger.error(traceback.format_exc()) error_msg = str(e) if str(e) else "An unexpected error occurred" return { "content": [{"type": "error", "text": f"Internal server error: {error_msg}"}], "is_error": True }
- Pydantic schema model ListCommitsParams for input validation of the list_commits tool. Inherits from RepositoryRef (owner/repo) and adds optional pagination and SHA fields with validators.class ListCommitsParams(RepositoryRef): """Parameters for listing commits.""" model_config = ConfigDict(strict=True) page: Optional[int] = Field(None, description="Page number") per_page: Optional[int] = Field(None, description="Results per page") sha: Optional[str] = Field(None, description="Branch name or commit SHA") @field_validator('page') @classmethod def validate_page(cls, v): """Validate that page is a positive integer.""" if v is not None and v < 1: raise ValueError("page must be a positive integer") return v @field_validator('per_page') @classmethod def validate_per_page(cls, v): """Validate that per_page is within allowed range.""" if v is not None: if v < 1: raise ValueError("per_page must be a positive integer") if v > 100: raise ValueError("per_page cannot exceed 100") return v
- src/pygithub_mcp_server/tools/repositories/__init__.py:9-40 (registration)The register function that imports the list_commits handler from tools.py and registers it (along with other repository tools) to the MCP server using register_tools.def register(mcp: FastMCP) -> None: """Register all repository tools with the MCP server. Args: mcp: The MCP server instance """ from pygithub_mcp_server.tools import register_tools from .tools import ( get_repository, create_repository, fork_repository, search_repositories, get_file_contents, create_or_update_file, push_files, create_branch, list_commits ) # Register all repository tools register_tools(mcp, [ get_repository, create_repository, fork_repository, search_repositories, get_file_contents, create_or_update_file, push_files, create_branch, list_commits ])