Skip to content

Commit 15eabc8

Browse files
jayzhou2309claude
authored andcommitted
fix(git): show blobs and trees for object specs in git_show
repo.commit() peels every revision with ^0, so HEAD:path/to/file failed with "Blob or Tree named 'file^0' not found". Resolve the revision with rev_parse first, return file content for a blob and a listing for a tree, and keep the commit output for everything else. Fixes modelcontextprotocol#1682 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 6df2727 commit 15eabc8

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎src/git/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ Please note that mcp-server-git is currently in early development. The functiona
8585
- `branch_name` (string): Name of branch to checkout
8686
- Returns: Confirmation of branch switch
8787
11. `git_show`
88-
- Shows the contents of a commit
88+
- Shows the contents of a commit, or of a file or directory at a revision
8989
- Inputs:
9090
- `repo_path` (string): Path to Git repository
91-
- `revision` (string): The revision (commit hash, branch name, tag) to show
92-
- Returns: Contents of the specified commit
91+
- `revision` (string): The revision (commit hash, branch name, tag) or object spec (`HEAD:path/to/file`) to show
92+
- Returns: Contents of the specified commit, file content, or directory listing
9393

9494
12. `git_branch`
9595
- List Git branches

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ def git_show(repo: git.Repo, revision: str) -> str:
240240
# even if a malicious ref with that name exists (e.g. via filesystem manipulation)
241241
if revision.startswith("-"):
242242
raise BadName(f"Invalid revision: '{revision}' - cannot start with '-'")
243+
obj = repo.rev_parse(revision)
244+
if isinstance(obj, git.Blob):
245+
return obj.data_stream.read().decode("utf-8", errors="replace")
246+
if isinstance(obj, git.Tree):
247+
return "\n".join(
248+
f"{item.name}/" if isinstance(item, git.Tree) else item.name for item in obj
249+
)
243250
commit = repo.commit(revision)
244251
output = [
245252
f"Commit: {commit.hexsha!r}\n"
@@ -454,7 +461,7 @@ async def list_tools() -> list[Tool]:
454461
),
455462
Tool(
456463
name=GitTools.SHOW,
457-
description="Shows the contents of a commit",
464+
description="Shows the contents of a commit, or of a file or directory given as <revision>:<path>",
458465
inputSchema=GitShow.model_json_schema(),
459466
annotations=ToolAnnotations(
460467
readOnlyHint=True,

0 commit comments

Comments
 (0)