Skip to content

Commit cb8d4e4

Browse files
Merge pull request #249 from monkeydaichan/main
feat(git): add git_diff tool for branch comparison
2 parents f6f7f1f + 768a5af commit cb8d4e4

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/git/README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,41 @@ Please note that mcp-server-git is currently in early development. The functiona
2626
- `repo_path` (string): Path to Git repository
2727
- Returns: Diff output of staged changes
2828

29-
4. `git_commit`
29+
4. `git_diff`
30+
- Shows differences between branches or commits
31+
- Inputs:
32+
- `repo_path` (string): Path to Git repository
33+
- `target` (string): Target branch or commit to compare with
34+
- Returns: Diff output comparing current state with target
35+
36+
5. `git_commit`
3037
- Records changes to the repository
3138
- Inputs:
3239
- `repo_path` (string): Path to Git repository
3340
- `message` (string): Commit message
3441
- Returns: Confirmation with new commit hash
3542

36-
5. `git_add`
43+
6. `git_add`
3744
- Adds file contents to the staging area
3845
- Inputs:
3946
- `repo_path` (string): Path to Git repository
4047
- `files` (string[]): Array of file paths to stage
4148
- Returns: Confirmation of staged files
4249

43-
6. `git_reset`
50+
7. `git_reset`
4451
- Unstages all staged changes
4552
- Input:
4653
- `repo_path` (string): Path to Git repository
4754
- Returns: Confirmation of reset operation
4855

49-
7. `git_log`
56+
8. `git_log`
5057
- Shows the commit logs
5158
- Inputs:
5259
- `repo_path` (string): Path to Git repository
5360
- `max_count` (number, optional): Maximum number of commits to show (default: 10)
5461
- Returns: Array of commit entries with hash, author, date, and message
5562

56-
8. `git_create_branch`
63+
9. `git_create_branch`
5764
- Creates a new branch
5865
- Inputs:
5966
- `repo_path` (string): Path to Git repository

src/git/src/mcp_server_git/server.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class GitDiffUnstaged(BaseModel):
2424
class GitDiffStaged(BaseModel):
2525
repo_path: str
2626

27+
class GitDiff(BaseModel):
28+
repo_path: str
29+
target: str
30+
2731
class GitCommit(BaseModel):
2832
repo_path: str
2933
message: str
@@ -48,6 +52,7 @@ class GitTools(str, Enum):
4852
STATUS = "git_status"
4953
DIFF_UNSTAGED = "git_diff_unstaged"
5054
DIFF_STAGED = "git_diff_staged"
55+
DIFF = "git_diff"
5156
COMMIT = "git_commit"
5257
ADD = "git_add"
5358
RESET = "git_reset"
@@ -63,6 +68,9 @@ def git_diff_unstaged(repo: git.Repo) -> str:
6368
def git_diff_staged(repo: git.Repo) -> str:
6469
return repo.git.diff("--cached")
6570

71+
def git_diff(repo: git.Repo, target: str) -> str:
72+
return repo.git.diff(target)
73+
6674
def git_commit(repo: git.Repo, message: str) -> str:
6775
commit = repo.index.commit(message)
6876
return f"Changes committed successfully with hash {commit.hexsha}"
@@ -127,6 +135,11 @@ async def list_tools() -> list[Tool]:
127135
description="Shows changes that are staged for commit",
128136
inputSchema=GitDiffStaged.schema(),
129137
),
138+
Tool(
139+
name=GitTools.DIFF,
140+
description="Shows differences between branches or commits",
141+
inputSchema=GitDiff.schema(),
142+
),
130143
Tool(
131144
name=GitTools.COMMIT,
132145
description="Records changes to the repository",
@@ -210,6 +223,13 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
210223
text=f"Staged changes:\n{diff}"
211224
)]
212225

226+
case GitTools.DIFF:
227+
diff = git_diff(repo, arguments["target"])
228+
return [TextContent(
229+
type="text",
230+
text=f"Diff with {arguments['target']}:\n{diff}"
231+
)]
232+
213233
case GitTools.COMMIT:
214234
result = git_commit(repo, arguments["message"])
215235
return [TextContent(

0 commit comments

Comments
 (0)