Skip to content

Commit e139c6b

Browse files
Add git_push tool to Git MCP server
- Implement git_push function with support for: - Pushing to custom remotes (default: origin) - Pushing specific branches - Force push capability - Setting upstream branch (-u flag) - Add GitPush model with comprehensive field descriptions - Register git_push as a new tool in the MCP server - Add test coverage for push functionality - Update README with git_push tool documentation Resolves modelcontextprotocol#618
1 parent b686223 commit e139c6b

3 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎src/git/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ Please note that mcp-server-git is currently in early development. The functiona
9494
- `not_contains` (string, optional): The commit sha that branch should NOT contain. Do not pass anything to this param if no commit sha is specified
9595
- Returns: List of branches
9696

97+
13. `git_push`
98+
- Pushes changes to a remote repository
99+
- Inputs:
100+
- `repo_path` (string): Path to the Git repository
101+
- `remote` (string, optional): The remote repository to push to (default: 'origin')
102+
- `branch` (string, optional): The branch to push. If not specified, pushes the current branch
103+
- `force` (boolean, optional): Force push the branch, overwriting remote history if necessary (default: false)
104+
- `set_upstream` (boolean, optional): Set the upstream branch when pushing (git push -u) (default: false)
105+
- Returns: Confirmation of push operation with output details
106+
97107
## Installation
98108

99109
### Using uv (recommended)

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ class GitBranch(BaseModel):
9090
description="The commit sha that branch should NOT contain. Do not pass anything to this param if no commit sha is specified",
9191
)
9292

93+
class GitPush(BaseModel):
94+
repo_path: str = Field(
95+
...,
96+
description="The path to the Git repository.",
97+
)
98+
remote: str = Field(
99+
default="origin",
100+
description="The remote repository to push to (default: 'origin').",
101+
)
102+
branch: Optional[str] = Field(
103+
None,
104+
description="The branch to push. If not specified, pushes the current branch.",
105+
)
106+
force: bool = Field(
107+
default=False,
108+
description="Force push the branch, overwriting remote history if necessary.",
109+
)
110+
set_upstream: bool = Field(
111+
default=False,
112+
description="Set the upstream branch when pushing (git push -u).",
113+
)
114+
93115

94116
class GitTools(str, Enum):
95117
STATUS = "git_status"
@@ -103,6 +125,7 @@ class GitTools(str, Enum):
103125
CREATE_BRANCH = "git_create_branch"
104126
CHECKOUT = "git_checkout"
105127
SHOW = "git_show"
128+
PUSH = "git_push"
106129

107130
BRANCH = "git_branch"
108131

@@ -230,6 +253,24 @@ def git_branch(repo: git.Repo, branch_type: str, contains: str | None = None, no
230253

231254
return branch_info
232255

256+
def git_push(repo: git.Repo, remote: str = "origin", branch: str | None = None, force: bool = False, set_upstream: bool = False) -> str:
257+
args = []
258+
if force:
259+
args.append("--force")
260+
if set_upstream:
261+
args.append("-u")
262+
263+
if branch:
264+
args.extend([remote, branch])
265+
else:
266+
args.append(remote)
267+
268+
try:
269+
result = repo.git.push(*args)
270+
return f"Pushed successfully to {remote}: {result}"
271+
except git.GitCommandError as e:
272+
return f"Error pushing to {remote}: {e}"
273+
233274

234275
async def serve(repository: Path | None) -> None:
235276
logger = logging.getLogger(__name__)
@@ -302,7 +343,11 @@ async def list_tools() -> list[Tool]:
302343
description="Shows the contents of a commit",
303344
inputSchema=GitShow.model_json_schema(),
304345
),
305-
346+
Tool(
347+
name=GitTools.PUSH,
348+
description="Pushes changes to a remote repository",
349+
inputSchema=GitPush.model_json_schema(),
350+
),
306351
Tool(
307352
name=GitTools.BRANCH,
308353
description="List Git branches",
@@ -447,6 +492,19 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
447492
text=result
448493
)]
449494

495+
case GitTools.PUSH:
496+
result = git_push(
497+
repo,
498+
arguments.get("remote", "origin"),
499+
arguments.get("branch", None),
500+
arguments.get("force", False),
501+
arguments.get("set_upstream", False),
502+
)
503+
return [TextContent(
504+
type="text",
505+
text=result
506+
)]
507+
450508
case _:
451509
raise ValueError(f"Unknown tool: {name}")
452510

‎src/git/tests/test_server.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from pathlib import Path
33
import git
4-
from mcp_server_git.server import git_checkout, git_branch, git_add
4+
from mcp_server_git.server import git_checkout, git_branch, git_add, git_push
55
import shutil
66

77
@pytest.fixture
@@ -91,3 +91,7 @@ def test_git_add_specific_files(test_repository):
9191
assert "file1.txt" in staged_files
9292
assert "file2.txt" not in staged_files
9393
assert result == "Files staged successfully"
94+
95+
def test_git_push_no_remote(test_repository):
96+
result = git_push(test_repository)
97+
assert "Error pushing to origin" in result

0 commit comments

Comments
 (0)