git_log
Retrieve commit logs from a Git repository. Filter by maximum number of commits or time range using start and end timestamps.
Instructions
Shows the commit logs
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | ||
| max_count | No | ||
| start_timestamp | No | Start timestamp for filtering commits. Accepts: ISO 8601 format (e.g., '2024-01-15T14:30:25'), relative dates (e.g., '2 weeks ago', 'yesterday'), or absolute dates (e.g., '2024-01-15', 'Jan 15 2024') | |
| end_timestamp | No | End timestamp for filtering commits. Accepts: ISO 8601 format (e.g., '2024-01-15T14:30:25'), relative dates (e.g., '2 weeks ago', 'yesterday'), or absolute dates (e.g., '2024-01-15', 'Jan 15 2024') |
Implementation Reference
- The git_log function that executes the tool logic. It accepts a repo, max_count (default 10), optional start_timestamp and end_timestamp for filtering commits by date. Without timestamps, it uses repo.iter_commits; with timestamps, it uses git log with --since/--until flags. Returns a list of formatted commit strings.
def git_log(repo: git.Repo, max_count: int = 10, start_timestamp: Optional[str] = None, end_timestamp: Optional[str] = None) -> list[str]: if start_timestamp or end_timestamp: # Defense in depth: reject timestamps starting with '-' to prevent flag injection if start_timestamp and start_timestamp.startswith("-"): raise ValueError(f"Invalid start_timestamp: '{start_timestamp}' - cannot start with '-'") if end_timestamp and end_timestamp.startswith("-"): raise ValueError(f"Invalid end_timestamp: '{end_timestamp}' - cannot start with '-'") # Use git log command with date filtering args = [] if start_timestamp: args.extend(['--since', start_timestamp]) if end_timestamp: args.extend(['--until', end_timestamp]) args.extend(['--format=%H%n%an%n%ad%n%s%n']) log_output = repo.git.log(*args).split('\n') log = [] # Process commits in groups of 4 (hash, author, date, message) for i in range(0, len(log_output), 4): if i + 3 < len(log_output) and len(log) < max_count: log.append( f"Commit: {log_output[i]}\n" f"Author: {log_output[i+1]}\n" f"Date: {log_output[i+2]}\n" f"Message: {log_output[i+3]}\n" ) return log else: # Use existing logic for simple log without date filtering commits = list(repo.iter_commits(max_count=max_count)) log = [] for commit in commits: log.append( f"Commit: {commit.hexsha!r}\n" f"Author: {commit.author!r}\n" f"Date: {commit.authored_datetime}\n" f"Message: {commit.message!r}\n" ) return log - GitLog Pydantic model defining the input schema for the git_log tool. Fields: repo_path (str), max_count (int, default 10), start_timestamp (Optional[str] with ISO 8601/relative date description), end_timestamp (Optional[str] with same description).
class GitLog(BaseModel): repo_path: str max_count: int = 10 start_timestamp: Optional[str] = Field( None, description="Start timestamp for filtering commits. Accepts: ISO 8601 format (e.g., '2024-01-15T14:30:25'), relative dates (e.g., '2 weeks ago', 'yesterday'), or absolute dates (e.g., '2024-01-15', 'Jan 15 2024')" ) end_timestamp: Optional[str] = Field( None, description="End timestamp for filtering commits. Accepts: ISO 8601 format (e.g., '2024-01-15T14:30:25'), relative dates (e.g., '2 weeks ago', 'yesterday'), or absolute dates (e.g., '2024-01-15', 'Jan 15 2024')" ) - src/git/src/mcp_server_git/server.py:386-396 (registration)Registration of the git_log tool in the list_tools handler via GitTools.LOG enum value 'git_log', with description 'Shows the commit logs' and inputSchema from GitLog.model_json_schema().
Tool( name=GitTools.LOG, description="Shows the commit logs", inputSchema=GitLog.model_json_schema(), annotations=ToolAnnotations( readOnlyHint=True, destructiveHint=False, idempotentHint=True, openWorldHint=False, ), ), - src/git/src/mcp_server_git/server.py:96-108 (registration)GitTools enum defining LOG = 'git_log' as the tool name identifier used in both registration and call routing.
class GitTools(str, Enum): STATUS = "git_status" DIFF_UNSTAGED = "git_diff_unstaged" DIFF_STAGED = "git_diff_staged" DIFF = "git_diff" COMMIT = "git_commit" ADD = "git_add" RESET = "git_reset" LOG = "git_log" CREATE_BRANCH = "git_create_branch" CHECKOUT = "git_checkout" SHOW = "git_show" - src/git/src/mcp_server_git/server.py:532-543 (registration)The call_tool handler that dispatches GitTools.LOG to the git_log function, extracting arguments for max_count, start_timestamp, and end_timestamp from the request.
# Update the LOG case: case GitTools.LOG: log = git_log( repo, arguments.get("max_count", 10), arguments.get("start_timestamp"), arguments.get("end_timestamp") ) return [TextContent( type="text", text="Commit history:\n" + "\n".join(log) )]