Skip to content

Commit 551777f

Browse files
committed
Enhance Git server with repository path security and improved configuration
1 parent e055e18 commit 551777f

4 files changed

Lines changed: 589 additions & 289 deletions

File tree

src/git/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ A Model Context Protocol server for Git repository interaction and automation. T
66

77
Please note that mcp-server-git is currently in early development. The functionality and available tools are subject to change and expansion as we continue to develop and improve the server.
88

9+
### Repository Path Security
10+
11+
When running the server with the `--repository` flag, all Git operations are restricted to that specific repository path, regardless of any `repo_path` values provided in tool calls. This provides an important security boundary:
12+
13+
- The server will reject any attempts to access paths outside the configured repository
14+
- All tool operations that accept a `repo_path` parameter will ignore it and use the configured repository instead
15+
- Path traversal attempts (e.g., using `../`) are blocked
16+
- If no repository is configured, the server requires explicit repository paths for each operation
17+
18+
This makes it safe to expose the server to untrusted clients while maintaining control over which repositories they can access.
19+
920
### Tools
1021

1122
1. `git_status`
@@ -65,7 +76,7 @@ Please note that mcp-server-git is currently in early development. The functiona
6576
- Inputs:
6677
- `repo_path` (string): Path to Git repository
6778
- `branch_name` (string): Name of the new branch
68-
- `start_point` (string, optional): Starting point for the new branch
79+
- `base_branch` (string, optional): Starting point (branch name or commit hash) for the new branch. Defaults to the current active branch.
6980
- Returns: Confirmation of branch creation
7081
10. `git_checkout`
7182
- Switches branches
Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
1+
import asyncio
12
import click
2-
from pathlib import Path
33
import logging
4+
from pathlib import Path
45
import sys
6+
import signal
57
from .server import serve
68

9+
def handle_sigint():
10+
for task in asyncio.all_tasks():
11+
task.cancel()
12+
713
@click.command()
8-
@click.option("--repository", "-r", type=Path, help="Git repository path")
9-
@click.option("-v", "--verbose", count=True)
10-
def main(repository: Path | None, verbose: bool) -> None:
14+
@click.option(
15+
"--repository",
16+
type=click.Path(exists=True, file_okay=False, dir_okay=True, path_type=Path),
17+
help="Path to Git repository to operate on",
18+
)
19+
@click.option("-v", "--verbose", count=True, default=0)
20+
def main(repository: Path | None, verbose: int = 0) -> None:
1121
"""MCP Git Server - Git functionality for MCP"""
12-
import asyncio
13-
1422
logging_level = logging.WARN
1523
if verbose == 1:
1624
logging_level = logging.INFO
17-
elif verbose >= 2:
25+
elif verbose > 1:
1826
logging_level = logging.DEBUG
19-
20-
logging.basicConfig(level=logging_level, stream=sys.stderr)
21-
asyncio.run(serve(repository))
27+
logging.basicConfig(level=logging_level)
28+
29+
loop = asyncio.new_event_loop()
30+
asyncio.set_event_loop(loop)
31+
32+
# Set up signal handlers
33+
loop.add_signal_handler(signal.SIGINT, handle_sigint)
34+
loop.add_signal_handler(signal.SIGTERM, handle_sigint)
35+
36+
try:
37+
loop.run_until_complete(serve(repository))
38+
except asyncio.CancelledError:
39+
logging.info("Server shutdown initiated")
40+
finally:
41+
loop.close()
2242

2343
if __name__ == "__main__":
2444
main()

0 commit comments

Comments
 (0)