Skip to content

Commit fc32e87

Browse files
committed
Add git init command support to mcp-git-server and update README
1 parent 4b9e267 commit fc32e87

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/git/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,23 @@ Please note that mcp-server-git is currently in early development. The functiona
6767
- `branch_name` (string): Name of the new branch
6868
- `start_point` (string, optional): Starting point for the new branch
6969
- Returns: Confirmation of branch creation
70-
8. `git_checkout`
70+
10. `git_checkout`
7171
- Switches branches
7272
- Inputs:
7373
- `repo_path` (string): Path to Git repository
7474
- `branch_name` (string): Name of branch to checkout
7575
- Returns: Confirmation of branch switch
76-
9. `git_show`
76+
11. `git_show`
7777
- Shows the contents of a commit
7878
- Inputs:
7979
- `repo_path` (string): Path to Git repository
8080
- `revision` (string): The revision (commit hash, branch name, tag) to show
8181
- Returns: Contents of the specified commit
82+
12. `git_init`
83+
- Initializes a Git repository
84+
- Inputs:
85+
- `repo_path` (string): Path to directory to initialize git repo
86+
- Returns: Confirmation of repository initialization
8287

8388
## Installation
8489

src/git/src/mcp_server_git/server.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class GitShow(BaseModel):
5656
repo_path: str
5757
revision: str
5858

59+
class GitInit(BaseModel):
60+
repo_path: str
61+
5962
class GitTools(str, Enum):
6063
STATUS = "git_status"
6164
DIFF_UNSTAGED = "git_diff_unstaged"
@@ -68,6 +71,7 @@ class GitTools(str, Enum):
6871
CREATE_BRANCH = "git_create_branch"
6972
CHECKOUT = "git_checkout"
7073
SHOW = "git_show"
74+
INIT = "git_init"
7175

7276
def git_status(repo: git.Repo) -> str:
7377
return repo.git.status()
@@ -118,6 +122,13 @@ def git_checkout(repo: git.Repo, branch_name: str) -> str:
118122
repo.git.checkout(branch_name)
119123
return f"Switched to branch '{branch_name}'"
120124

125+
def git_init(repo_path: str) -> str:
126+
try:
127+
repo = git.Repo.init(path=repo_path, mkdir=True)
128+
return f"Initialized empty Git repository in {repo.git_dir}"
129+
except Exception as e:
130+
return f"Error initializing repository: {str(e)}"
131+
121132
def git_show(repo: git.Repo, revision: str) -> str:
122133
commit = repo.commit(revision)
123134
output = [
@@ -206,6 +217,11 @@ async def list_tools() -> list[Tool]:
206217
name=GitTools.SHOW,
207218
description="Shows the contents of a commit",
208219
inputSchema=GitShow.schema(),
220+
),
221+
Tool(
222+
name=GitTools.INIT,
223+
description="Initialize a new Git repository",
224+
inputSchema=GitInit.schema(),
209225
)
210226
]
211227

@@ -241,6 +257,16 @@ def by_commandline() -> Sequence[str]:
241257
@server.call_tool()
242258
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
243259
repo_path = Path(arguments["repo_path"])
260+
261+
# Handle git init separately since it doesn't require an existing repo
262+
if name == GitTools.INIT:
263+
result = git_init(str(repo_path))
264+
return [TextContent(
265+
type="text",
266+
text=result
267+
)]
268+
269+
# For all other commands, we need an existing repo
244270
repo = git.Repo(repo_path)
245271

246272
match name:

0 commit comments

Comments
 (0)