Skip to content

Commit 1252f48

Browse files
authored
Merge pull request #1326 from mceachen/main
feat(mcp-server-git): Add context_lines to git_diff* tools
2 parents 9898e5a + c6eb49b commit 1252f48

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

src/git/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@ Please note that mcp-server-git is currently in early development. The functiona
1616

1717
2. `git_diff_unstaged`
1818
- Shows changes in working directory not yet staged
19-
- Input:
19+
- Inputs:
2020
- `repo_path` (string): Path to Git repository
21+
- `context_lines` (number, optional): Number of context lines to show (default: 3)
2122
- Returns: Diff output of unstaged changes
2223

2324
3. `git_diff_staged`
2425
- Shows changes that are staged for commit
25-
- Input:
26+
- Inputs:
2627
- `repo_path` (string): Path to Git repository
28+
- `context_lines` (number, optional): Number of context lines to show (default: 3)
2729
- Returns: Diff output of staged changes
2830

2931
4. `git_diff`
3032
- Shows differences between branches or commits
3133
- Inputs:
3234
- `repo_path` (string): Path to Git repository
3335
- `target` (string): Target branch or commit to compare with
36+
- `context_lines` (number, optional): Number of context lines to show (default: 3)
3437
- Returns: Diff output comparing current state with target
3538

3639
5. `git_commit`

src/git/src/mcp_server_git/server.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
1515
import git
1616
from pydantic import BaseModel
1717

18+
# Default number of context lines to show in diff output
19+
DEFAULT_CONTEXT_LINES = 3
20+
1821
class GitStatus(BaseModel):
1922
repo_path: str
2023

2124
class GitDiffUnstaged(BaseModel):
2225
repo_path: str
26+
context_lines: int = DEFAULT_CONTEXT_LINES
2327

2428
class GitDiffStaged(BaseModel):
2529
repo_path: str
30+
context_lines: int = DEFAULT_CONTEXT_LINES
2631

2732
class GitDiff(BaseModel):
2833
repo_path: str
2934
target: str
35+
context_lines: int = DEFAULT_CONTEXT_LINES
3036

3137
class GitCommit(BaseModel):
3238
repo_path: str
@@ -76,14 +82,14 @@ class GitTools(str, Enum):
7682
def git_status(repo: git.Repo) -> str:
7783
return repo.git.status()
7884

79-
def git_diff_unstaged(repo: git.Repo) -> str:
80-
return repo.git.diff()
85+
def git_diff_unstaged(repo: git.Repo, context_lines: int = DEFAULT_CONTEXT_LINES) -> str:
86+
return repo.git.diff(f"--unified={context_lines}")
8187

82-
def git_diff_staged(repo: git.Repo) -> str:
83-
return repo.git.diff("--cached")
88+
def git_diff_staged(repo: git.Repo, context_lines: int = DEFAULT_CONTEXT_LINES) -> str:
89+
return repo.git.diff(f"--unified={context_lines}", "--cached")
8490

85-
def git_diff(repo: git.Repo, target: str) -> str:
86-
return repo.git.diff(target)
91+
def git_diff(repo: git.Repo, target: str, context_lines: int = DEFAULT_CONTEXT_LINES) -> str:
92+
return repo.git.diff(f"--unified={context_lines}", target)
8793

8894
def git_commit(repo: git.Repo, message: str) -> str:
8995
commit = repo.index.commit(message)
@@ -278,21 +284,21 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
278284
)]
279285

280286
case GitTools.DIFF_UNSTAGED:
281-
diff = git_diff_unstaged(repo)
287+
diff = git_diff_unstaged(repo, arguments.get("context_lines", DEFAULT_CONTEXT_LINES))
282288
return [TextContent(
283289
type="text",
284290
text=f"Unstaged changes:\n{diff}"
285291
)]
286292

287293
case GitTools.DIFF_STAGED:
288-
diff = git_diff_staged(repo)
294+
diff = git_diff_staged(repo, arguments.get("context_lines", DEFAULT_CONTEXT_LINES))
289295
return [TextContent(
290296
type="text",
291297
text=f"Staged changes:\n{diff}"
292298
)]
293299

294300
case GitTools.DIFF:
295-
diff = git_diff(repo, arguments["target"])
301+
diff = git_diff(repo, arguments["target"], arguments.get("context_lines", DEFAULT_CONTEXT_LINES))
296302
return [TextContent(
297303
type="text",
298304
text=f"Diff with {arguments['target']}:\n{diff}"

0 commit comments

Comments
 (0)