feat(git): add git_diff tool for branch comparison

Added new git_diff tool to allow comparison between branches or commits.
This adds the ability to compare branches directly through the MCP interface.
This commit is contained in:
monkeydaichan
2024-12-07 01:25:33 +09:00
parent 2ecb382a02
commit ba301c4a66
2 changed files with 38 additions and 7 deletions

View File

@@ -26,34 +26,41 @@ Please note that mcp-server-git is currently in early development. The functiona
- `repo_path` (string): Path to Git repository
- Returns: Diff output of staged changes
4. `git_commit`
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
- Returns: Diff output comparing current state with target
5. `git_commit`
- Records changes to the repository
- Inputs:
- `repo_path` (string): Path to Git repository
- `message` (string): Commit message
- Returns: Confirmation with new commit hash
5. `git_add`
6. `git_add`
- Adds file contents to the staging area
- Inputs:
- `repo_path` (string): Path to Git repository
- `files` (string[]): Array of file paths to stage
- Returns: Confirmation of staged files
6. `git_reset`
7. `git_reset`
- Unstages all staged changes
- Input:
- `repo_path` (string): Path to Git repository
- Returns: Confirmation of reset operation
7. `git_log`
8. `git_log`
- Shows the commit logs
- Inputs:
- `repo_path` (string): Path to Git repository
- `max_count` (number, optional): Maximum number of commits to show (default: 10)
- Returns: Array of commit entries with hash, author, date, and message
8. `git_create_branch`
9. `git_create_branch`
- Creates a new branch
- Inputs:
- `repo_path` (string): Path to Git repository
@@ -66,7 +73,7 @@ Please note that mcp-server-git is currently in early development. The functiona
### Using uv (recommended)
When using [`uv`](https://docs.astral.sh/uv/) no specific installation is needed. We will
use [`uvx`](https://docs.astral.sh/uv/guides/tools/) to directly run *mcp-server-git*.
use [`uvx`](https://docs.astral.sh/uv/guides/tools/) to directly run _mcp-server-git_.
### Using PIP
@@ -99,6 +106,7 @@ Add this to your `claude_desktop_config.json`:
}
}
```
</details>
<details>
@@ -112,6 +120,7 @@ Add this to your `claude_desktop_config.json`:
}
}
```
</details>
### Usage with [Zed](https://github.com/zed-industries/zed)
@@ -131,6 +140,7 @@ Add to your Zed settings.json:
}
],
```
</details>
<details>
@@ -146,6 +156,7 @@ Add to your Zed settings.json:
}
},
```
</details>
## Debugging

View File

@@ -24,6 +24,10 @@ class GitDiffUnstaged(BaseModel):
class GitDiffStaged(BaseModel):
repo_path: str
class GitDiff(BaseModel):
repo_path: str
target: str
class GitCommit(BaseModel):
repo_path: str
message: str
@@ -48,6 +52,7 @@ class GitTools(str, Enum):
STATUS = "git_status"
DIFF_UNSTAGED = "git_diff_unstaged"
DIFF_STAGED = "git_diff_staged"
DIFF = "git_diff"
COMMIT = "git_commit"
ADD = "git_add"
RESET = "git_reset"
@@ -63,6 +68,9 @@ def git_diff_unstaged(repo: git.Repo) -> str:
def git_diff_staged(repo: git.Repo) -> str:
return repo.git.diff("--cached")
def git_diff(repo: git.Repo, target: str) -> str:
return repo.git.diff(target)
def git_commit(repo: git.Repo, message: str) -> str:
commit = repo.index.commit(message)
return f"Changes committed successfully with hash {commit.hexsha}"
@@ -127,6 +135,11 @@ async def serve(repository: Path | None) -> None:
description="Shows changes that are staged for commit",
inputSchema=GitDiffStaged.schema(),
),
Tool(
name=GitTools.DIFF,
description="Shows differences between branches or commits",
inputSchema=GitDiff.schema(),
),
Tool(
name=GitTools.COMMIT,
description="Records changes to the repository",
@@ -210,6 +223,13 @@ async def serve(repository: Path | None) -> None:
text=f"Staged changes:\n{diff}"
)]
case GitTools.DIFF:
diff = git_diff(repo, arguments["target"])
return [TextContent(
type="text",
text=f"Diff with {arguments['target']}:\n{diff}"
)]
case GitTools.COMMIT:
result = git_commit(repo, arguments["message"])
return [TextContent(
@@ -254,4 +274,4 @@ async def serve(repository: Path | None) -> None:
options = server.create_initialization_options()
async with stdio_server() as (read_stream, write_stream):
await server.run(read_stream, write_stream, options, raise_exceptions=True)
await server.run(read_stream, write_stream, options, raise_exceptions=True)