From 1ac3c1da5987de17dcf5cc84a7c708da0a649156 Mon Sep 17 00:00:00 2001 From: Matthew McEachen Date: Tue, 8 Apr 2025 15:41:41 -0700 Subject: [PATCH 1/3] 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 827d58faa9..8f79a55a2e 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 c6a346cfef..c5ff011e69 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) From 4c77b645181a7f1958932b15234fd7225720b55c Mon Sep 17 00:00:00 2001 From: Matthew McEachen Date: Tue, 8 Apr 2025 17:53:03 -0700 Subject: [PATCH 2/3] refactor(mcp-server-git): Change from environment variable to proper parameter --- src/git/README.md | 18 +++++--------- src/git/src/mcp_server_git/server.py | 35 ++++++++++++---------------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/git/README.md b/src/git/README.md index 8f79a55a2e..8ef54b1732 100644 --- a/src/git/README.md +++ b/src/git/README.md @@ -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. -### 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` @@ -22,14 +16,16 @@ 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` @@ -37,6 +33,7 @@ Please note that mcp-server-git is currently in early development. The functiona - 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` @@ -125,10 +122,7 @@ Add this to your `claude_desktop_config.json`: "mcpServers": { "git": { "command": "uvx", - "args": ["mcp-server-git", "--repository", "path/to/git/repo"], - "env": { - "GIT_DIFF_CONTEXT_LINES": "10" - } + "args": ["mcp-server-git", "--repository", "path/to/git/repo"] } } ``` diff --git a/src/git/src/mcp_server_git/server.py b/src/git/src/mcp_server_git/server.py index c5ff011e69..ed1f714b10 100644 --- a/src/git/src/mcp_server_git/server.py +++ b/src/git/src/mcp_server_git/server.py @@ -16,18 +16,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 @@ -74,28 +80,17 @@ 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(get_context_lines()) +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(get_context_lines(), "--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(get_context_lines(), 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) @@ -290,21 +285,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}" From ba54c9d374d96e6c8fdf823f339d3f78dd745ed8 Mon Sep 17 00:00:00 2001 From: Matthew McEachen Date: Tue, 29 Apr 2025 16:01:50 -0700 Subject: [PATCH 3/3] fix(server.py): remove unused import 'os' --- src/git/src/mcp_server_git/server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/git/src/mcp_server_git/server.py b/src/git/src/mcp_server_git/server.py index ed1f714b10..a1b5687132 100644 --- a/src/git/src/mcp_server_git/server.py +++ b/src/git/src/mcp_server_git/server.py @@ -1,6 +1,5 @@ import logging from pathlib import Path -import os from typing import Sequence from mcp.server import Server from mcp.server.session import ServerSession