MCP ExplorerExplorer

Mcp Git Tools

@lileeeion 9 months ago
5 MIT
FreeCommunity
AI Systems
#git#mcp#mcp-server#rust
Git tool integration library for the Model Context Protocol (MCP).

Overview

What is Mcp Git Tools

mcp-git-tools is a Git tool integration library designed for the Model Context Protocol (MCP), enabling various Git operations to be performed through MCP.

Use cases

Use cases include automating Git operations in CI/CD pipelines, integrating version control features into applications, and building tools that require Git functionalities within an MCP framework.

How to use

To use mcp-git-tools, you can run it as a standalone server using ‘cargo run --bin mcp-git-server’ or integrate it into your own MCP client by establishing a connection and calling the desired Git operations.

Key features

Key features include retrieving repository status, listing branches, accessing commit history, creating commits, pulling and pushing changes, viewing file differences, adding files to staging, and resetting the staging area.

Where to use

mcp-git-tools can be used in software development environments where Git operations need to be integrated into applications that communicate using the Model Context Protocol.

Content

MCP Git Tools

Git tool integration library for the Model Context Protocol (MCP).

中文文档

Features

This library provides a set of Git operations that can be called through the Model Context Protocol:

  • git_status - Get the status of a repository
  • git_branches - List branch information
  • git_log - Get commit history
  • git_time_filtered_log - Get commits within a specific time range
  • git_commit - Create a new commit
  • git_pull - Pull changes from remote
  • git_push - Push changes to remote
  • git_diff - View file differences
  • git_add - Add file contents to the staging area
  • git_reset - Reset the staging area or working tree to a specified state

Installation

# Clone the repository
git clone https://github.com/lileeei/mcp-git-tools.git

# Navigate to the directory
cd mcp-git-tools

# Build
cargo build

Usage

Run as a standalone server

cargo run --bin mcp-git-server

This starts an MCP server that interacts with clients through standard input/output.

Use in an MCP client

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);

Integrate into your own MCP server

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();

Tool Details

git_status

Get the status of a repository.

Parameters:

  • repo_path - Path to the Git repository

Returns:

{
  "status": [
    "M file1.txt",
    "?? file2.txt"
  ],
  "is_clean": false
}

git_branches

List all branches.

Parameters:

  • repo_path - Path to the Git repository

Returns:

{
  "branches": [
    "* main",
    "develop",
    "feature/new-feature"
  ],
  "current": "main"
}

git_log

Get commit history.

Parameters:

  • repo_path - Path to the Git repository
  • max_count - (optional) Maximum number of commits to return
  • branch - (optional) Branch name

Returns:

{
  "commits": [
    {
      "hash": "abcd1234",
      "author": "User Name",
      "date": "Mon Aug 1 10:00:00 2023 +0800",
      "message": "Initial commit"
    }
  ]
}

git_time_filtered_log

Get commits within a specified time range, optionally filtered by author and branch.

Parameters:

  • repo_path - Path to the Git repository
  • since - 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 author
  • branch - (optional) Branch name

Returns:

{
  "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"
  }
}

git_commit

Create a new commit.

Parameters:

  • repo_path - Path to the Git repository
  • message - Commit message
  • all - (optional) Whether to automatically stage modified files

Returns:

{
  "success": true,
  "hash": "abcd1234",
  "message": "feat: Add new feature",
  "output": "[main abcd1234] feat: Add new feature\n 1 file changed, 10 insertions(+), 2 deletions(-)"
}

git_pull

Pull changes from remote.

Parameters:

  • repo_path - Path to the Git repository
  • remote - (optional) Remote name, defaults to “origin”
  • branch - (optional) Branch name

Returns:

{
  "success": true,
  "remote": "origin",
  "output": "Updating abcd1234..efgh5678\nFast-forward\n file1.txt | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)"
}

git_push

Push changes to remote.

Parameters:

  • repo_path - Path to the Git repository
  • remote - (optional) Remote name, defaults to “origin”
  • branch - (optional) Branch name
  • force - (optional) Whether to force push

Returns:

{
  "success": true,
  "remote": "origin",
  "output": "To github.com:user/repo.git\n   abcd1234..efgh5678  main -> main"
}

git_diff

View file differences.

Parameters:

  • repo_path - Path to the Git repository
  • path - (optional) Path to file or directory
  • staged - (optional) Whether to show staged changes
  • commit - (optional) Commit to compare against

Returns:

{
  "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"
}

git_add

Add file contents to the staging area.

Parameters:

  • repo_path - Path to the Git repository
  • path - Path(s) to add, or patterns to match. Use ‘.’ for all files.
  • update - (optional) Whether to update, rather than add
  • all - (optional) Whether to add all changes, including untracked files

Returns:

{
  "success": true,
  "message": "Files staged successfully",
  "status": [
    "M file1.txt",
    "A file2.txt"
  ]
}

git_reset

Reset the staging area or working tree to a specified state.

Parameters:

  • repo_path - Path to the Git repository
  • path - 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"
  ]
}

License

MIT License

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers