Skip to content

Commit

Permalink
Merge pull request #157 from mikegehard/create-new-branches
Browse files Browse the repository at this point in the history
Create new branches
  • Loading branch information
jspahrsummers authored Dec 3, 2024
2 parents 36b21e0 + c0a1cb7 commit 20f684a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/git/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ Please note that mcp-server-git is currently in early development. The functiona
- `max_count` (number, optional): Maximum number of commits to show (default: 10)
- Returns: Array of commit entries with hash, author, date, and message

8. `git_create_branch`
- Creates a new branch
- Inputs:
- `repo_path` (string): Path to Git repository
- `branch_name` (string): Name of the new branch
- `start_point` (string, optional): Starting point for the new branch
- Returns: Confirmation of branch creation

## Installation

Expand Down Expand Up @@ -156,6 +163,29 @@ cd path/to/servers/src/git
npx @modelcontextprotocol/inspector uv run mcp-server-git
```

Running `tail -n 20 -f ~/Library/Logs/Claude/mcp*.log` will show the logs from the server and may
help you debug any issues.

## Development

If you are doing local development, there are two ways to test your changes:

1. Run the MCP inspector to test your changes. See [Debugging](#debugging) for run instructions.

2. Test using the Claude desktop app. Add the following to your `claude_desktop_config.json`:

```json
"git": {
"command": "uv",
"args": [
"--directory",
"/<path to mcp-servers>/mcp-servers/src/git",
"run",
"mcp-server-git"
]
}
```

## License

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.
31 changes: 31 additions & 0 deletions src/git/src/mcp_server_git/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class GitLog(BaseModel):
repo_path: str
max_count: int = 10

class GitCreateBranch(BaseModel):
repo_path: str
branch_name: str
base_branch: str | None = None

class GitTools(str, Enum):
STATUS = "git_status"
DIFF_UNSTAGED = "git_diff_unstaged"
Expand All @@ -47,6 +52,7 @@ class GitTools(str, Enum):
ADD = "git_add"
RESET = "git_reset"
LOG = "git_log"
CREATE_BRANCH = "git_create_branch"

def git_status(repo: git.Repo) -> str:
return repo.git.status()
Expand Down Expand Up @@ -81,6 +87,15 @@ def git_log(repo: git.Repo, max_count: int = 10) -> list[str]:
)
return log

def git_create_branch(repo: git.Repo, branch_name: str, base_branch: str | None = None) -> str:
if base_branch:
base = repo.refs[base_branch]
else:
base = repo.active_branch

repo.create_head(branch_name, base)
return f"Created branch '{branch_name}' from '{base.name}'"

async def serve(repository: Path | None) -> None:
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -132,6 +147,11 @@ async def list_tools() -> list[Tool]:
description="Shows the commit logs",
inputSchema=GitLog.schema(),
),
Tool(
name=GitTools.CREATE_BRANCH,
description="Creates a new branch from an optional base branch",
inputSchema=GitCreateBranch.schema(),
),
]

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

case GitTools.CREATE_BRANCH:
result = git_create_branch(
repo,
arguments["branch_name"],
arguments.get("base_branch")
)
return [TextContent(
type="text",
text=result
)]

case _:
raise ValueError(f"Unknown tool: {name}")

Expand Down

0 comments on commit 20f684a

Please sign in to comment.