Skip to content

Commit fac5000

Browse files
committed
add git_show tool
1 parent 642af4d commit fac5000

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/git/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ Please note that mcp-server-git is currently in early development. The functiona
7373
- `repo_path` (string): Path to Git repository
7474
- `branch_name` (string): Name of branch to checkout
7575
- Returns: Confirmation of branch switch
76+
9. `git_show`
77+
- Shows the contents of a commit
78+
- Inputs:
79+
- `repo_path` (string): Path to Git repository
80+
- `revision` (string): The revision (commit hash, branch name, tag) to show
81+
- Returns: Contents of the specified commit
7682

7783
## Installation
7884

src/git/src/mcp_server_git/server.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class GitCheckout(BaseModel):
5252
repo_path: str
5353
branch_name: str
5454

55+
class GitShow(BaseModel):
56+
repo_path: str
57+
revision: str
58+
5559
class GitTools(str, Enum):
5660
STATUS = "git_status"
5761
DIFF_UNSTAGED = "git_diff_unstaged"
@@ -63,6 +67,7 @@ class GitTools(str, Enum):
6367
LOG = "git_log"
6468
CREATE_BRANCH = "git_create_branch"
6569
CHECKOUT = "git_checkout"
70+
SHOW = "git_show"
6671

6772
def git_status(repo: git.Repo) -> str:
6873
return repo.git.status()
@@ -113,6 +118,24 @@ def git_checkout(repo: git.Repo, branch_name: str) -> str:
113118
repo.git.checkout(branch_name)
114119
return f"Switched to branch '{branch_name}'"
115120

121+
def git_show(repo: git.Repo, revision: str) -> str:
122+
commit = repo.commit(revision)
123+
output = [
124+
f"Commit: {commit.hexsha}\n"
125+
f"Author: {commit.author}\n"
126+
f"Date: {commit.authored_datetime}\n"
127+
f"Message: {commit.message}\n"
128+
]
129+
if commit.parents:
130+
parent = commit.parents[0]
131+
diff = parent.diff(commit, create_patch=True)
132+
else:
133+
diff = commit.diff(git.NULL_TREE, create_patch=True)
134+
for d in diff:
135+
output.append(f"\n--- {d.a_path}\n+++ {d.b_path}\n")
136+
output.append(d.diff.decode('utf-8'))
137+
return "".join(output)
138+
116139
async def serve(repository: Path | None) -> None:
117140
logger = logging.getLogger(__name__)
118141

@@ -179,6 +202,11 @@ async def list_tools() -> list[Tool]:
179202
description="Switches branches",
180203
inputSchema=GitCheckout.schema(),
181204
),
205+
Tool(
206+
name=GitTools.SHOW,
207+
description="Shows the contents of a commit",
208+
inputSchema=GitShow.schema(),
209+
)
182210
]
183211

184212
async def list_repos() -> Sequence[str]:
@@ -290,6 +318,13 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
290318
text=result
291319
)]
292320

321+
case GitTools.SHOW:
322+
result = git_show(repo, arguments["revision"])
323+
return [TextContent(
324+
type="text",
325+
text=result
326+
)]
327+
293328
case _:
294329
raise ValueError(f"Unknown tool: {name}")
295330

0 commit comments

Comments
 (0)