refactor(mcp-server-git): Change from environment variable to proper parameter

This commit is contained in:
Matthew McEachen 2025-04-08 17:53:03 -07:00
commit 4c77b64518
No known key found for this signature in database
GPG key ID: CFD5233EAB7B1685
2 changed files with 21 additions and 32 deletions

View file

@ -6,12 +6,6 @@ A Model Context Protocol server for Git repository interaction and automation. T
Please note that mcp-server-git is currently in early development. The functionality and available tools are subject to change and expansion as we continue to develop and improve the server. Please note that mcp-server-git is currently in early development. The functionality and available tools are subject to change and expansion as we continue to develop and improve the server.
### Environment Variables
- `GIT_DIFF_CONTEXT_LINES`: Number of context lines to show in git diff output (default: 3). Increasing this value shows more surrounding code in diffs.
> **⚠️ Warning:** Large context line values will consume more tokens and may reduce response relevance. Set this value carefully to balance context needs with token limits.
### Tools ### Tools
1. `git_status` 1. `git_status`
@ -22,14 +16,16 @@ Please note that mcp-server-git is currently in early development. The functiona
2. `git_diff_unstaged` 2. `git_diff_unstaged`
- Shows changes in working directory not yet staged - Shows changes in working directory not yet staged
- Input: - Inputs:
- `repo_path` (string): Path to Git repository - `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 - Returns: Diff output of unstaged changes
3. `git_diff_staged` 3. `git_diff_staged`
- Shows changes that are staged for commit - Shows changes that are staged for commit
- Input: - Inputs:
- `repo_path` (string): Path to Git repository - `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 - Returns: Diff output of staged changes
4. `git_diff` 4. `git_diff`
@ -37,6 +33,7 @@ Please note that mcp-server-git is currently in early development. The functiona
- Inputs: - Inputs:
- `repo_path` (string): Path to Git repository - `repo_path` (string): Path to Git repository
- `target` (string): Target branch or commit to compare with - `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 - Returns: Diff output comparing current state with target
5. `git_commit` 5. `git_commit`
@ -125,10 +122,7 @@ Add this to your `claude_desktop_config.json`:
"mcpServers": { "mcpServers": {
"git": { "git": {
"command": "uvx", "command": "uvx",
"args": ["mcp-server-git", "--repository", "path/to/git/repo"], "args": ["mcp-server-git", "--repository", "path/to/git/repo"]
"env": {
"GIT_DIFF_CONTEXT_LINES": "10"
}
} }
} }
``` ```

View file

@ -16,18 +16,24 @@ from enum import Enum
import git import git
from pydantic import BaseModel from pydantic import BaseModel
# Default number of context lines to show in diff output
DEFAULT_CONTEXT_LINES = 3
class GitStatus(BaseModel): class GitStatus(BaseModel):
repo_path: str repo_path: str
class GitDiffUnstaged(BaseModel): class GitDiffUnstaged(BaseModel):
repo_path: str repo_path: str
context_lines: int = DEFAULT_CONTEXT_LINES
class GitDiffStaged(BaseModel): class GitDiffStaged(BaseModel):
repo_path: str repo_path: str
context_lines: int = DEFAULT_CONTEXT_LINES
class GitDiff(BaseModel): class GitDiff(BaseModel):
repo_path: str repo_path: str
target: str target: str
context_lines: int = DEFAULT_CONTEXT_LINES
class GitCommit(BaseModel): class GitCommit(BaseModel):
repo_path: str repo_path: str
@ -74,28 +80,17 @@ class GitTools(str, Enum):
SHOW = "git_show" SHOW = "git_show"
INIT = "git_init" INIT = "git_init"
# Default number of context lines if not specified in environment
DEFAULT_CONTEXT_LINES = 3
def get_context_lines() -> str:
"""Get the number of context lines from environment variable or use default"""
try:
context_lines = int(os.environ.get('GIT_DIFF_CONTEXT_LINES', DEFAULT_CONTEXT_LINES))
return f"--unified={context_lines}"
except ValueError:
return f"--unified={DEFAULT_CONTEXT_LINES}"
def git_status(repo: git.Repo) -> str: def git_status(repo: git.Repo) -> str:
return repo.git.status() return repo.git.status()
def git_diff_unstaged(repo: git.Repo) -> str: def git_diff_unstaged(repo: git.Repo, context_lines: int = DEFAULT_CONTEXT_LINES) -> str:
return repo.git.diff(get_context_lines()) return repo.git.diff(f"--unified={context_lines}")
def git_diff_staged(repo: git.Repo) -> str: def git_diff_staged(repo: git.Repo, context_lines: int = DEFAULT_CONTEXT_LINES) -> str:
return repo.git.diff(get_context_lines(), "--cached") return repo.git.diff(f"--unified={context_lines}", "--cached")
def git_diff(repo: git.Repo, target: str) -> str: def git_diff(repo: git.Repo, target: str, context_lines: int = DEFAULT_CONTEXT_LINES) -> str:
return repo.git.diff(get_context_lines(), target) return repo.git.diff(f"--unified={context_lines}", target)
def git_commit(repo: git.Repo, message: str) -> str: def git_commit(repo: git.Repo, message: str) -> str:
commit = repo.index.commit(message) commit = repo.index.commit(message)
@ -290,21 +285,21 @@ async def serve(repository: Path | None) -> None:
)] )]
case GitTools.DIFF_UNSTAGED: case GitTools.DIFF_UNSTAGED:
diff = git_diff_unstaged(repo) diff = git_diff_unstaged(repo, arguments.get("context_lines", DEFAULT_CONTEXT_LINES))
return [TextContent( return [TextContent(
type="text", type="text",
text=f"Unstaged changes:\n{diff}" text=f"Unstaged changes:\n{diff}"
)] )]
case GitTools.DIFF_STAGED: case GitTools.DIFF_STAGED:
diff = git_diff_staged(repo) diff = git_diff_staged(repo, arguments.get("context_lines", DEFAULT_CONTEXT_LINES))
return [TextContent( return [TextContent(
type="text", type="text",
text=f"Staged changes:\n{diff}" text=f"Staged changes:\n{diff}"
)] )]
case GitTools.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( return [TextContent(
type="text", type="text",
text=f"Diff with {arguments['target']}:\n{diff}" text=f"Diff with {arguments['target']}:\n{diff}"