Skip to content

Commit ba301c4

Browse files
committed
feat(git): add git_diff tool for branch comparison
Added new git_diff tool to allow comparison between branches or commits. This adds the ability to compare branches directly through the MCP interface.
1 parent 2ecb382 commit ba301c4

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/git/README.md

Lines changed: 17 additions & 6 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
@@ -66,7 +73,7 @@ Please note that mcp-server-git is currently in early development. The functiona
6673
### Using uv (recommended)
6774

6875
When using [`uv`](https://docs.astral.sh/uv/) no specific installation is needed. We will
69-
use [`uvx`](https://docs.astral.sh/uv/guides/tools/) to directly run *mcp-server-git*.
76+
use [`uvx`](https://docs.astral.sh/uv/guides/tools/) to directly run _mcp-server-git_.
7077

7178
### Using PIP
7279

@@ -99,6 +106,7 @@ Add this to your `claude_desktop_config.json`:
99106
}
100107
}
101108
```
109+
102110
</details>
103111

104112
<details>
@@ -112,6 +120,7 @@ Add this to your `claude_desktop_config.json`:
112120
}
113121
}
114122
```
123+
115124
</details>
116125

117126
### Usage with [Zed](https://github.com/zed-industries/zed)
@@ -131,6 +140,7 @@ Add to your Zed settings.json:
131140
}
132141
],
133142
```
143+
134144
</details>
135145

136146
<details>
@@ -146,6 +156,7 @@ Add to your Zed settings.json:
146156
}
147157
},
148158
```
159+
149160
</details>
150161

151162
## Debugging

src/git/src/mcp_server_git/server.py

Lines changed: 21 additions & 1 deletion
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(
@@ -254,4 +274,4 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
254274

255275
options = server.create_initialization_options()
256276
async with stdio_server() as (read_stream, write_stream):
257-
await server.run(read_stream, write_stream, options, raise_exceptions=True)
277+
await server.run(read_stream, write_stream, options, raise_exceptions=True)

0 commit comments

Comments
 (0)