From 1ac3c1da5987de17dcf5cc84a7c708da0a649156 Mon Sep 17 00:00:00 2001 From: Matthew McEachen Date: Tue, 8 Apr 2025 15:41:41 -0700 Subject: [PATCH] feat(mcp_server_git): Add support for configurable GIT_DIFF_CONTEXT_LINES - Introduced `GIT_DIFF_CONTEXT_LINES` environment variable to customize the number of context lines shown in git diff output. - Updated `git_diff_unstaged`, `git_diff_staged`, and `git_diff` functions to utilize the new context line setting. - Enhanced README.md to document the new environment variable and its implications. --- src/git/README.md | 11 ++++++++++- src/git/src/mcp_server_git/server.py | 18 +++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/git/README.md b/src/git/README.md index 827d58f..8f79a55 100644 --- a/src/git/README.md +++ b/src/git/README.md @@ -6,6 +6,12 @@ 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. +### 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 1. `git_status` @@ -119,7 +125,10 @@ Add this to your `claude_desktop_config.json`: "mcpServers": { "git": { "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" + } } } ``` diff --git a/src/git/src/mcp_server_git/server.py b/src/git/src/mcp_server_git/server.py index c6a346c..c5ff011 100644 --- a/src/git/src/mcp_server_git/server.py +++ b/src/git/src/mcp_server_git/server.py @@ -1,5 +1,6 @@ import logging from pathlib import Path +import os from typing import Sequence from mcp.server import Server from mcp.server.session import ServerSession @@ -73,17 +74,28 @@ class GitTools(str, Enum): SHOW = "git_show" 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: return repo.git.status() def git_diff_unstaged(repo: git.Repo) -> str: - return repo.git.diff() + return repo.git.diff(get_context_lines()) def git_diff_staged(repo: git.Repo) -> str: - return repo.git.diff("--cached") + return repo.git.diff(get_context_lines(), "--cached") def git_diff(repo: git.Repo, target: str) -> str: - return repo.git.diff(target) + return repo.git.diff(get_context_lines(), target) def git_commit(repo: git.Repo, message: str) -> str: commit = repo.index.commit(message)