
Git工具
STDIOGit操作工具与模型上下文协议集成
Git操作工具与模型上下文协议集成
Git tool integration library for the Model Context Protocol (MCP).
This library provides a set of Git operations that can be called through the Model Context Protocol:
git_status
- Get the status of a repositorygit_branches
- List branch informationgit_log
- Get commit historygit_time_filtered_log
- Get commits within a specific time rangegit_commit
- Create a new commitgit_pull
- Pull changes from remotegit_push
- Push changes to remotegit_diff
- View file differencesgit_add
- Add file contents to the staging areagit_reset
- Reset the staging area or working tree to a specified state# Clone the repository git clone https://github.com/lileeei/mcp-git-tools.git # Navigate to the directory cd mcp-git-tools # Build cargo build
cargo run --bin mcp-git-server
This starts an MCP server that interacts with clients through standard input/output.
use mcp_client::{ client::{ClientCapabilities, ClientInfo, McpClient}, StdioTransport, Transport, McpService, }; use std::collections::HashMap; use std::time::Duration; // Create a connection to the Git tools server let transport = StdioTransport::new( "path/to/mcp-git-server", vec![], HashMap::new() ); // Start the transport let handle = transport.start().await?; let service = McpService::with_timeout(handle, Duration::from_secs(10)); let mut client = McpClient::new(service); // Initialize the client client.initialize( ClientInfo { name: "my-client".into(), version: "1.0.0".into(), }, ClientCapabilities::default(), ).await?; // Call the git_status tool let status = client .call_tool("git_status", serde_json::json!({ "repo_path": "/path/to/repo" })) .await?; println!("Git status: {:?}", status);
use mcp_git_tools::register_git_tools; use mcp_server::McpServerBuilder; // Create a server let mut builder = McpServerBuilder::new("my-server", "1.0.0"); // Register Git tools register_git_tools(&mut builder); // Add other tools... // Build the server let server = builder.build();
Get the status of a repository.
Parameters:
repo_path
- Path to the Git repositoryReturns:
{ "status": ["M file1.txt", "?? file2.txt"], "is_clean": false }
List all branches.
Parameters:
repo_path
- Path to the Git repositoryReturns:
{ "branches": ["* main", "develop", "feature/new-feature"], "current": "main" }
Get commit history.
Parameters:
repo_path
- Path to the Git repositorymax_count
- (optional) Maximum number of commits to returnbranch
- (optional) Branch nameReturns:
{ "commits": [ { "hash": "abcd1234", "author": "User Name", "date": "Mon Aug 1 10:00:00 2023 +0800", "message": "Initial commit" } ] }
Get commits within a specified time range, optionally filtered by author and branch.
Parameters:
repo_path
- Path to the Git repositorysince
- Start date (e.g., "2023-01-01", "1 week ago", "yesterday")until
- (optional) End date (e.g., "2023-01-31", "today")author
- (optional) Filter by specific authorbranch
- (optional) Branch nameReturns:
{ "commits": [ { "hash": "abcd1234", "author": "User Name", "date": "Mon Aug 1 10:00:00 2023 +0800", "message": "Initial commit" } ], "filters": { "since": "1 week ago", "until": "today", "author": "User Name", "branch": "main" } }
Create a new commit.
Parameters:
repo_path
- Path to the Git repositorymessage
- Commit messageall
- (optional) Whether to automatically stage modified filesReturns:
{ "success": true, "hash": "abcd1234", "message": "feat: Add new feature", "output": "[main abcd1234] feat: Add new feature\n 1 file changed, 10 insertions(+), 2 deletions(-)" }
Pull changes from remote.
Parameters:
repo_path
- Path to the Git repositoryremote
- (optional) Remote name, defaults to "origin"branch
- (optional) Branch nameReturns:
{ "success": true, "remote": "origin", "output": "Updating abcd1234..efgh5678\nFast-forward\n file1.txt | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)" }
Push changes to remote.
Parameters:
repo_path
- Path to the Git repositoryremote
- (optional) Remote name, defaults to "origin"branch
- (optional) Branch nameforce
- (optional) Whether to force pushReturns:
{ "success": true, "remote": "origin", "output": "To github.com:user/repo.git\n abcd1234..efgh5678 main -> main" }
View file differences.
Parameters:
repo_path
- Path to the Git repositorypath
- (optional) Path to file or directorystaged
- (optional) Whether to show staged changescommit
- (optional) Commit to compare againstReturns:
{ "diff": "diff --git a/file.txt b/file.txt\nindex 1234567..abcdefg 100644\n--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,4 @@\n Line 1\n Line 2\n+New line\n Line 3" }
Add file contents to the staging area.
Parameters:
repo_path
- Path to the Git repositorypath
- Path(s) to add, or patterns to match. Use '.' for all files.update
- (optional) Whether to update, rather than addall
- (optional) Whether to add all changes, including untracked filesReturns:
{ "success": true, "message": "Files staged successfully", "status": ["M file1.txt", "A file2.txt"] }
Reset the staging area or working tree to a specified state.
Parameters:
repo_path
- Path to the Git repositorypath
- Path(s) to reset, or patterns to match. Use '.' for all files.hard
- (optional) Whether to perform a hard reset (WARNING: discards all local changes)target
- (optional) The commit or branch to reset to (defaults to HEAD)Returns:
{ "success": true, "message": "Files unstaged successfully", "status": ["?? file1.txt", "?? file2.txt"] }
MIT License