Skip to content

feat(mcp-server-git): Add context_lines to git_diff* tools #1326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/git/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ Please note that mcp-server-git is currently in early development. The functiona

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

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

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

5. `git_commit`
Expand Down
24 changes: 15 additions & 9 deletions src/git/src/mcp_server_git/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
import git
from pydantic import BaseModel

# Default number of context lines to show in diff output
DEFAULT_CONTEXT_LINES = 3

class GitStatus(BaseModel):
repo_path: str

class GitDiffUnstaged(BaseModel):
repo_path: str
context_lines: int = DEFAULT_CONTEXT_LINES

class GitDiffStaged(BaseModel):
repo_path: str
context_lines: int = DEFAULT_CONTEXT_LINES

class GitDiff(BaseModel):
repo_path: str
target: str
context_lines: int = DEFAULT_CONTEXT_LINES

class GitCommit(BaseModel):
repo_path: str
Expand Down Expand Up @@ -76,14 +82,14 @@ class GitTools(str, Enum):
def git_status(repo: git.Repo) -> str:
return repo.git.status()

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

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

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

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

case GitTools.DIFF_UNSTAGED:
diff = git_diff_unstaged(repo)
diff = git_diff_unstaged(repo, arguments.get("context_lines", DEFAULT_CONTEXT_LINES))
return [TextContent(
type="text",
text=f"Unstaged changes:\n{diff}"
)]

case GitTools.DIFF_STAGED:
diff = git_diff_staged(repo)
diff = git_diff_staged(repo, arguments.get("context_lines", DEFAULT_CONTEXT_LINES))
return [TextContent(
type="text",
text=f"Staged changes:\n{diff}"
)]

case GitTools.DIFF:
diff = git_diff(repo, arguments["target"])
diff = git_diff(repo, arguments["target"], arguments.get("context_lines", DEFAULT_CONTEXT_LINES))
return [TextContent(
type="text",
text=f"Diff with {arguments['target']}:\n{diff}"
Expand Down