Skip to content

Commit 20f684a

Browse files
Merge pull request #157 from mikegehard/create-new-branches
Create new branches
2 parents 36b21e0 + c0a1cb7 commit 20f684a

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/git/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ Please note that mcp-server-git is currently in early development. The functiona
5353
- `max_count` (number, optional): Maximum number of commits to show (default: 10)
5454
- Returns: Array of commit entries with hash, author, date, and message
5555

56+
8. `git_create_branch`
57+
- Creates a new branch
58+
- Inputs:
59+
- `repo_path` (string): Path to Git repository
60+
- `branch_name` (string): Name of the new branch
61+
- `start_point` (string, optional): Starting point for the new branch
62+
- Returns: Confirmation of branch creation
5663

5764
## Installation
5865

@@ -156,6 +163,29 @@ cd path/to/servers/src/git
156163
npx @modelcontextprotocol/inspector uv run mcp-server-git
157164
```
158165

166+
Running `tail -n 20 -f ~/Library/Logs/Claude/mcp*.log` will show the logs from the server and may
167+
help you debug any issues.
168+
169+
## Development
170+
171+
If you are doing local development, there are two ways to test your changes:
172+
173+
1. Run the MCP inspector to test your changes. See [Debugging](#debugging) for run instructions.
174+
175+
2. Test using the Claude desktop app. Add the following to your `claude_desktop_config.json`:
176+
177+
```json
178+
"git": {
179+
"command": "uv",
180+
"args": [
181+
"--directory",
182+
"/<path to mcp-servers>/mcp-servers/src/git",
183+
"run",
184+
"mcp-server-git"
185+
]
186+
}
187+
```
188+
159189
## License
160190

161191
This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.

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)