Skip to content

Commit 3b4c97b

Browse files
committed
Add git branch creation functionality
1 parent fb33c8a commit 3b4c97b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/git/src/mcp_server_git/server.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class GitLog(BaseModel):
3939
repo_path: str
4040
max_count: int = 10
4141

42+
class GitCreateBranch(BaseModel):
43+
repo_path: str
44+
branch_name: str
45+
base_branch: str | None = None
46+
4247
class GitTools(str, Enum):
4348
STATUS = "git_status"
4449
DIFF_UNSTAGED = "git_diff_unstaged"
@@ -47,6 +52,7 @@ class GitTools(str, Enum):
4752
ADD = "git_add"
4853
RESET = "git_reset"
4954
LOG = "git_log"
55+
CREATE_BRANCH = "git_create_branch"
5056

5157
def git_status(repo: git.Repo) -> str:
5258
return repo.git.status()
@@ -81,6 +87,15 @@ def git_log(repo: git.Repo, max_count: int = 10) -> list[str]:
8187
)
8288
return log
8389

90+
def git_create_branch(repo: git.Repo, branch_name: str, base_branch: str | None = None) -> str:
91+
if base_branch:
92+
base = repo.refs[base_branch]
93+
else:
94+
base = repo.active_branch
95+
96+
repo.create_head(branch_name, base)
97+
return f"Created branch '{branch_name}' from {base.name}"
98+
8499
async def serve(repository: Path | None) -> None:
85100
logger = logging.getLogger(__name__)
86101

@@ -132,6 +147,11 @@ async def list_tools() -> list[Tool]:
132147
description="Shows the commit logs",
133148
inputSchema=GitLog.schema(),
134149
),
150+
Tool(
151+
name=GitTools.CREATE_BRANCH,
152+
description="Creates a new branch from an optional base branch",
153+
inputSchema=GitCreateBranch.schema(),
154+
),
135155
]
136156

137157
async def list_repos() -> Sequence[str]:
@@ -218,6 +238,17 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
218238
text="Commit history:\n" + "\n".join(log)
219239
)]
220240

241+
case GitTools.CREATE_BRANCH:
242+
result = git_create_branch(
243+
repo,
244+
arguments["branch_name"],
245+
arguments.get("base_branch")
246+
)
247+
return [TextContent(
248+
type="text",
249+
text=result
250+
)]
251+
221252
case _:
222253
raise ValueError(f"Unknown tool: {name}")
223254

0 commit comments

Comments
 (0)