Skip to content

Commit 0b16177

Browse files
authored
Merge pull request #5626 from modelcontextprotocol/v2/feat/5552-git-log-description
feat(git): say git_log is newest-first and its timestamp filters are optional
2 parents 53d4408 + 7d24637 commit 0b16177

4 files changed

Lines changed: 81 additions & 12 deletions

File tree

‎src/git/README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ Please note that mcp-server-git is currently in early development. The functiona
6363
- Returns: Confirmation of reset operation
6464

6565
8. `git_log`
66-
- Shows the commit logs with optional date filtering
66+
- Shows the commit log, newest commit first. With only `repo_path` it returns the latest commits; the two timestamps are optional filters for a date range
6767
- Inputs:
6868
- `repo_path` (string): Path to Git repository
6969
- `max_count` (number, optional): Maximum number of commits to show, at least 1 (default: 10)
70-
- `start_timestamp` (string, optional): 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')
71-
- `end_timestamp` (string, optional): 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')
72-
- Returns: Array of commit entries with hash, author, date, and message
70+
- `start_timestamp` (string, optional): Only show commits at or after this time; omit for no lower bound. 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')
71+
- `end_timestamp` (string, optional): Only show commits at or before this time; omit for no upper bound. 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')
72+
- Returns: `Commit history:` followed by one entry per commit (hash, author, date and message), newest first. When a timestamp filter matches no commits, the text says so and suggests omitting both timestamps, rather than returning an empty history
7373

7474
9. `git_create_branch`
7575
- Creates a new branch

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,16 @@ def reset(repo_path: str) -> str:
16211621

16221622
@server.tool(
16231623
name=GitTools.LOG.value,
1624-
description="Shows the commit logs",
1624+
# #5552: small models read the old one-line description as "a time
1625+
# window is required" and filtered to an empty one. Say the order and
1626+
# that the filters are optional, where the model picks its arguments.
1627+
description=(
1628+
"Shows the commit log, newest commit first. For the latest "
1629+
"commits, pass only repo_path (and optionally max_count). "
1630+
"start_timestamp and end_timestamp are optional filters for a "
1631+
"date range: omit them unless a specific period is asked for, "
1632+
"because a range that holds no commits returns none."
1633+
),
16251634
annotations=READ_ONLY,
16261635
structured_output=False,
16271636
)
@@ -1638,19 +1647,33 @@ def log(
16381647
start_timestamp: Annotated[
16391648
str | None,
16401649
Field(
1641-
description=f"Start timestamp for filtering commits. {TIMESTAMP_FORMATS}"
1650+
description=(
1651+
"Optional: only show commits at or after this time. "
1652+
f"Omit for no lower bound. {TIMESTAMP_FORMATS}"
1653+
)
16421654
),
16431655
] = None,
16441656
end_timestamp: Annotated[
16451657
str | None,
16461658
Field(
1647-
description=f"End timestamp for filtering commits. {TIMESTAMP_FORMATS}"
1659+
description=(
1660+
"Optional: only show commits at or before this time. "
1661+
f"Omit for no upper bound. {TIMESTAMP_FORMATS}"
1662+
)
16481663
),
16491664
] = None,
16501665
) -> str:
16511666
entries = git_log(
16521667
open_repo(repo_path), max_count, start_timestamp, end_timestamp
16531668
)
1669+
if not entries and (start_timestamp or end_timestamp):
1670+
# #5552: an empty history under a filter reads as "the repository
1671+
# has no commits". Name the filter as the cause and the way out.
1672+
return (
1673+
"Commit history:\n"
1674+
"No commits match the given start_timestamp/end_timestamp. "
1675+
"Omit both to see the latest commits."
1676+
)
16541677
return "Commit history:\n" + "\n".join(entries)
16551678

16561679
@server.tool(

‎src/git/tests/snapshots/list_tools.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
},
198198
{
199199
"name": "git_log",
200-
"description": "Shows the commit logs",
200+
"description": "Shows the commit log, newest commit first. For the latest commits, pass only repo_path (and optionally max_count). start_timestamp and end_timestamp are optional filters for a date range: omit them unless a specific period is asked for, because a range that holds no commits returns none.",
201201
"inputSchema": {
202202
"properties": {
203203
"repo_path": {
@@ -221,7 +221,7 @@
221221
}
222222
],
223223
"default": null,
224-
"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')",
224+
"description": "Optional: only show commits at or after this time. Omit for no lower bound. 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')",
225225
"title": "Start Timestamp"
226226
},
227227
"end_timestamp": {
@@ -234,7 +234,7 @@
234234
}
235235
],
236236
"default": null,
237-
"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')",
237+
"description": "Optional: only show commits at or before this time. Omit for no upper bound. 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')",
238238
"title": "End Timestamp"
239239
}
240240
},

‎src/git/tests/test_protocol.py‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,14 @@ def add_commits(repo: git.Repo, n: int) -> list[git.Commit]:
635635
return commits
636636

637637

638+
# #5552: what git_log returns when a timestamp filter matches nothing.
639+
NO_COMMITS_IN_RANGE = (
640+
"Commit history:\n"
641+
"No commits match the given start_timestamp/end_timestamp. "
642+
"Omit both to see the latest commits."
643+
)
644+
645+
638646
async def test_git_log_default(repo: git.Repo):
639647
commits = add_commits(repo, 2)
640648
initial = repo.head.commit.parents[0].parents[0]
@@ -691,11 +699,11 @@ async def test_git_log_timestamps_filter(repo: git.Repo):
691699
future = await call(
692700
None, "git_log", {"repo_path": root, "start_timestamp": "2099-01-01"}
693701
)
694-
assert future == text_result("Commit history:\n")
702+
assert future == text_result(NO_COMMITS_IN_RANGE)
695703
past = await call(
696704
None, "git_log", {"repo_path": root, "end_timestamp": "2000-01-01"}
697705
)
698-
assert past == text_result("Commit history:\n")
706+
assert past == text_result(NO_COMMITS_IN_RANGE)
699707
both = await call(
700708
None,
701709
"git_log",
@@ -708,6 +716,44 @@ async def test_git_log_timestamps_filter(repo: git.Repo):
708716
assert both == text_result("Commit history:\n" + log_entry(repo.head.commit))
709717

710718

719+
async def test_git_log_empty_window_names_the_filter(repo: git.Repo):
720+
# #5552: a small model asked for the latest commit passed "now" for both
721+
# timestamps, got a bare "Commit history:" and concluded the repository
722+
# had no commits. The reply now says the filter matched nothing.
723+
add_commits(repo, 2)
724+
root = str(root_of(repo))
725+
# Committed a moment ago, so the window that ends before them is empty.
726+
result = await call(
727+
None,
728+
"git_log",
729+
{
730+
"repo_path": root,
731+
"max_count": 1,
732+
"start_timestamp": "2000-01-01",
733+
"end_timestamp": "2000-01-02",
734+
},
735+
)
736+
assert result == text_result(NO_COMMITS_IN_RANGE)
737+
# The same call with no filter returns the newest commit, with no hint.
738+
latest = await call(None, "git_log", {"repo_path": root, "max_count": 1})
739+
assert latest == text_result("Commit history:\n" + log_entry(repo.head.commit))
740+
741+
742+
async def test_git_log_description_states_order_and_optional_filters():
743+
# #5552: the description is where a model decides its arguments, so it
744+
# says the order and that both timestamps can be left out.
745+
async with connect(None) as session:
746+
tools = wire(await session.list_tools())["tools"]
747+
log = next(t for t in tools if t["name"] == "git_log")
748+
assert "newest commit first" in log["description"]
749+
assert "optional" in log["description"]
750+
assert log["inputSchema"]["required"] == ["repo_path"]
751+
for name in ("start_timestamp", "end_timestamp"):
752+
assert log["inputSchema"]["properties"][name]["description"].startswith(
753+
"Optional: "
754+
)
755+
756+
711757
async def test_git_log_rejects_timestamp_flag_injection(repo: git.Repo):
712758
root = str(root_of(repo))
713759
assert await call(

0 commit comments

Comments
 (0)