Local Git MCP Server

Local Git MCP Server

A Model Context Protocol server for interacting with Git repositories, extending mcp_server_git with custom tools to list repository directories and view file contents for easier codebase navigation.

Category
访问服务器

README

Local Git MCP Server

A local Model Context Protocol (MCP) server for interacting with Git repositories. This server extends the mcp_server_git with custom tools for listing repository directories and viewing file contents, making it easier to navigate and inspect codebases programmatically.


✅ Features

  • Custom Tools:

    • list_repo_dir: Recursively list files and directories in a Git repository. If no path is provided, it lists the root directory.
    • view_repo_file: Read and return the contents of a specific file in the repository.
  • Git Integration: Inherits all standard Git operations from mcp_server_git (e.g., git_status, git_diff, git_log, etc.).

  • Flexible Configuration: Supports environment variables for repository path configuration.

  • MCP Compatibility: Works with any MCP-compatible client (e.g., Model Context Protocol clients).


🛠️ Prerequisites

Before running the server, ensure the following are installed:

1. Python 3.8+

2. pip (Python Package Manager)

  • Ensure pip is installed and up-to-date:
    python -m pip install --upgrade pip
    

3. Required Python Packages

Install the dependencies using pip:

pip install starlette uvicorn mcp-server-git

4. Git

5. Target Git Repository

  • Ensure the repository you want to interact with is cloned locally on your machine.
  • By default, the server uses the path specified in the TARGET_REPO environment variable. If not set, it defaults to:
    C:\Users\bpweathe\code\SCIP\scipai
    

🚀 Installation & Setup

Step 1: Clone the Repository

git clone https://github.com/bpweatherill/local-git-mcp.git
cd local-git-mcp

Step 2: Install Dependencies

pip install -r requirements.txt

Note: If requirements.txt does not exist, install the required packages manually:

pip install starlette uvicorn mcp-server-git

Step 3: Configure the Target Repository

Set the TARGET_REPO environment variable to point to your local Git repository:

Windows (PowerShell)

$env:TARGET_REPO = "C:\path\to\your\repo"

macOS/Linux (Terminal)

export TARGET_REPO="/path/to/your/repo"

Tip: If you don’t set TARGET_REPO, the server will default to C:\Users\bpweathe\code\SCIP\scipai.


🏃 Running the Server

Start the MCP server using the following command:

python mcp_server.py

The server will start on:

  • Host: localhost
  • Port: 8808

You should see output similar to:

INFO:     Uvicorn running on http://localhost:8808 (Press CTRL+C to quit)

🛠️ Using the Server

Connecting with an MCP Client

Once the server is running, you can connect to it using any MCP-compatible client. Here’s how to test it:

1. List Root Directory

Call the list_repo_dir tool with no arguments to list the root of the repository:

{
  "method": "tools/call",
  "params": {
    "name": "list_repo_dir",
    "arguments": {}
  }
}

2. List a Subdirectory

Pass a relative path to list the contents of a subdirectory:

{
  "method": "tools/call",
  "params": {
    "name": "list_repo_dir",
    "arguments": {
      "path": "docs/clients"
    }
  }
}

3. View a File

Use the view_repo_file tool to read a file:

{
  "method": "tools/call",
  "params": {
    "name": "view_repo_file",
    "arguments": {
      "path": "README.md"
    }
  }
}

4. Standard Git Tools

The server also supports all standard Git tools from mcp_server_git. For example:

{
  "method": "tools/call",
  "params": {
    "name": "git_status",
    "arguments": {}
  }
}

📡 API Endpoints

The server exposes the following endpoints:

Endpoint Method Description
/ GET SSE endpoint for streaming server events
/ POST Messages endpoint for MCP tool calls
/sse GET Alternative SSE endpoint
/messages POST Alternative messages endpoint
/mcp GET/POST Unified MCP endpoint (SSE for GET, messages for POST)

🔧 Custom Tools

1. list_repo_dir

Description: Lists files and directories in a repository path.

Parameters:

  • path (optional): Relative path of the subdirectory to list. If omitted, lists the root directory.

Example Output:

Items inside 'docs/clients':
- copilot-byok.md
- pageassist.md
- openai-api.md

2. view_repo_file

Description: Reads and returns the complete text contents of a file.

Parameters:

  • path (required): Relative path of the file to view.

Example Output:

# README.md
This is the content of the README file...

📜 Standard Git Tools

The server inherits all tools from mcp_server_git, including:

  • git_status: Show working tree status.
  • git_diff: Show changes between branches or commits.
  • git_log: Show commit history.
  • git_add: Stage files for commit.
  • git_commit: Commit changes to the repository.
  • git_branch: List branches.
  • git_reset: Unstage changes.

For a full list, call the tools/list method after connecting to the server.


🧪 Testing

To verify the server is working correctly, you can use a tool like curl or Postman to send a test request:

Example curl Request

curl -X POST http://localhost:8808/messages \
  -H "Content-Type: application/json" \
  -d '{
    "id": 1,
    "method": "tools/list",
    "params": {}
  }'

Expected Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "tools": [
      {
        "name": "list_repo_dir",
        "description": "Lists files and items inside a specific sub-folder of the repository.",
        "inputSchema": { ... }
      },
      {
        "name": "view_repo_file",
        "description": "Reads and returns the complete text contents of a specific file.",
        "inputSchema": { ... }
      },
      {
        "name": "git_status",
        "description": "Shows the working tree status.",
        "inputSchema": { ... }
      }
    ]
  }
}

🐛 Troubleshooting

Common Issues

1. Server Fails to Start

  • Cause: Missing dependencies or incorrect Python version.
  • Solution: Ensure Python 3.8+ and all required packages are installed:
    pip install starlette uvicorn mcp-server-git
    

2. Repository Not Found

  • Cause: The TARGET_REPO path is incorrect or the repository does not exist.
  • Solution: Set TARGET_REPO to the correct local path:
    # Windows (PowerShell)
    $env:TARGET_REPO = "C:\path\to\your\repo"
    
    # macOS/Linux
    export TARGET_REPO="/path/to/your/repo"
    

3. Permission Denied

  • Cause: The server does not have read access to the repository.
  • Solution: Ensure the repository is accessible and the server has read permissions.

4. Timeouts

  • Cause: The server takes too long to respond.
  • Solution: Check if the mcp_server_git subprocess is running correctly. Restart the server if needed.

📚 Example Use Cases

1. Navigate a Codebase

// List the root directory
{
  "method": "tools/call",
  "params": {
    "name": "list_repo_dir",
    "arguments": {}
  }
}

// List the 'src' directory
{
  "method": "tools/call",
  "params": {
    "name": "list_repo_dir",
    "arguments": {
      "path": "src"
    }
  }
}

// View a file
{
  "method": "tools/call",
  "params": {
    "name": "view_repo_file",
    "arguments": {
      "path": "src/main.py"
    }
  }
}

2. Check Git Status and Diffs

// Check Git status
{
  "method": "tools/call",
  "params": {
    "name": "git_status",
    "arguments": {}
  }
}

// Show unstaged changes
{
  "method": "tools/call",
  "params": {
    "name": "git_diff_unstaged",
    "arguments": {}
  }
}

🔒 Security Notes

  • The server only allows access to files within the TARGET_REPO directory. Attempts to access files outside this path will result in a "Path out of bounds" error.
  • Never expose this server to untrusted networks without proper authentication and authorization.

📜 License

This project is open-source and licensed under the MIT License.


🤝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs or suggest features by opening an issue.
  • Submit pull requests with improvements.

📞 Support

For questions or issues:

  • Check the GitHub Issues for known problems.
  • Open a new issue if you encounter unexpected behavior.

🏷️ Tags

mcp-server, git, python, starlette, uvicorn, model-context-protocol

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选