From fac500018b6f9631cb4d6b7057bc4e3b571fc281 Mon Sep 17 00:00:00 2001 From: Ilia Choly Date: Tue, 17 Dec 2024 08:03:41 -0500 Subject: [PATCH] add git_show tool --- src/git/README.md | 6 +++++ src/git/src/mcp_server_git/server.py | 35 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/git/README.md b/src/git/README.md index 31e4edbb..8f3afdc7 100644 --- a/src/git/README.md +++ b/src/git/README.md @@ -73,6 +73,12 @@ Please note that mcp-server-git is currently in early development. The functiona - `repo_path` (string): Path to Git repository - `branch_name` (string): Name of branch to checkout - Returns: Confirmation of branch switch +9. `git_show` + - Shows the contents of a commit + - Inputs: + - `repo_path` (string): Path to Git repository + - `revision` (string): The revision (commit hash, branch name, tag) to show + - Returns: Contents of the specified commit ## Installation diff --git a/src/git/src/mcp_server_git/server.py b/src/git/src/mcp_server_git/server.py index dd796ee8..9b204c6e 100644 --- a/src/git/src/mcp_server_git/server.py +++ b/src/git/src/mcp_server_git/server.py @@ -52,6 +52,10 @@ class GitCheckout(BaseModel): repo_path: str branch_name: str +class GitShow(BaseModel): + repo_path: str + revision: str + class GitTools(str, Enum): STATUS = "git_status" DIFF_UNSTAGED = "git_diff_unstaged" @@ -63,6 +67,7 @@ class GitTools(str, Enum): LOG = "git_log" CREATE_BRANCH = "git_create_branch" CHECKOUT = "git_checkout" + SHOW = "git_show" def git_status(repo: git.Repo) -> str: return repo.git.status() @@ -113,6 +118,24 @@ def git_checkout(repo: git.Repo, branch_name: str) -> str: repo.git.checkout(branch_name) return f"Switched to branch '{branch_name}'" +def git_show(repo: git.Repo, revision: str) -> str: + commit = repo.commit(revision) + output = [ + f"Commit: {commit.hexsha}\n" + f"Author: {commit.author}\n" + f"Date: {commit.authored_datetime}\n" + f"Message: {commit.message}\n" + ] + if commit.parents: + parent = commit.parents[0] + diff = parent.diff(commit, create_patch=True) + else: + diff = commit.diff(git.NULL_TREE, create_patch=True) + for d in diff: + output.append(f"\n--- {d.a_path}\n+++ {d.b_path}\n") + output.append(d.diff.decode('utf-8')) + return "".join(output) + async def serve(repository: Path | None) -> None: logger = logging.getLogger(__name__) @@ -179,6 +202,11 @@ async def list_tools() -> list[Tool]: description="Switches branches", inputSchema=GitCheckout.schema(), ), + Tool( + name=GitTools.SHOW, + description="Shows the contents of a commit", + inputSchema=GitShow.schema(), + ) ] async def list_repos() -> Sequence[str]: @@ -290,6 +318,13 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]: text=result )] + case GitTools.SHOW: + result = git_show(repo, arguments["revision"]) + return [TextContent( + type="text", + text=result + )] + case _: raise ValueError(f"Unknown tool: {name}")