Commit ce88682

mo khan <mo@mokhan.ca>
2025-06-22 16:10:04
feat: add --repository flag to mcp-git
1 parent 7d2cba3
Changed files (2)
cmd
pkg
cmd/git/main.go
@@ -2,14 +2,28 @@ package main
 
 import (
 	"context"
+	"flag"
 	"log"
+	"os"
 
 	"github.com/xlgmokha/mcp/pkg/git"
 	"github.com/xlgmokha/mcp/pkg/mcp"
 )
 
 func main() {
-	server := git.New()
+	var repository = flag.String("repository", "", "Path to the git repository (defaults to current directory)")
+	flag.Parse()
+
+	repoPath := *repository
+	if repoPath == "" {
+		var err error
+		repoPath, err = os.Getwd()
+		if err != nil {
+			log.Fatalf("Failed to get current directory: %v", err)
+		}
+	}
+
+	server := git.New(repoPath)
 
 	// Set up basic initialization
 	server.SetInitializeHandler(func(req mcp.InitializeRequest) (mcp.InitializeResult, error) {
pkg/git/server.go
@@ -14,14 +14,16 @@ import (
 // Server implements the Git MCP server
 type Server struct {
 	*mcp.Server
+	repoPath string
 }
 
 // New creates a new Git MCP server
-func New() *Server {
+func New(repoPath string) *Server {
 	server := mcp.NewServer("mcp-git", "1.0.0")
 
 	gitServer := &Server{
-		Server: server,
+		Server:   server,
+		repoPath: repoPath,
 	}
 
 	// Register all git tools
@@ -261,7 +263,7 @@ func (gs *Server) ListTools() []mcp.Tool {
 func (gs *Server) HandleGitStatus(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	output, err := gs.runGitCommand(repoPath, "status")
@@ -275,7 +277,7 @@ func (gs *Server) HandleGitStatus(req mcp.CallToolRequest) (mcp.CallToolResult,
 func (gs *Server) HandleGitDiffUnstaged(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	output, err := gs.runGitCommand(repoPath, "diff")
@@ -289,7 +291,7 @@ func (gs *Server) HandleGitDiffUnstaged(req mcp.CallToolRequest) (mcp.CallToolRe
 func (gs *Server) HandleGitDiffStaged(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	output, err := gs.runGitCommand(repoPath, "diff", "--cached")
@@ -303,7 +305,7 @@ func (gs *Server) HandleGitDiffStaged(req mcp.CallToolRequest) (mcp.CallToolResu
 func (gs *Server) HandleGitDiff(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	target, ok := req.Arguments["target"].(string)
@@ -322,7 +324,7 @@ func (gs *Server) HandleGitDiff(req mcp.CallToolRequest) (mcp.CallToolResult, er
 func (gs *Server) HandleGitCommit(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	message, ok := req.Arguments["message"].(string)
@@ -341,7 +343,7 @@ func (gs *Server) HandleGitCommit(req mcp.CallToolRequest) (mcp.CallToolResult,
 func (gs *Server) HandleGitAdd(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	filesInterface, ok := req.Arguments["files"]
@@ -366,7 +368,7 @@ func (gs *Server) HandleGitAdd(req mcp.CallToolRequest) (mcp.CallToolResult, err
 func (gs *Server) HandleGitReset(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	_, err := gs.runGitCommand(repoPath, "reset")
@@ -380,7 +382,7 @@ func (gs *Server) HandleGitReset(req mcp.CallToolRequest) (mcp.CallToolResult, e
 func (gs *Server) HandleGitLog(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	maxCount := 10
@@ -401,7 +403,7 @@ func (gs *Server) HandleGitLog(req mcp.CallToolRequest) (mcp.CallToolResult, err
 func (gs *Server) HandleGitCreateBranch(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	branchName, ok := req.Arguments["branch_name"].(string)
@@ -429,7 +431,7 @@ func (gs *Server) HandleGitCreateBranch(req mcp.CallToolRequest) (mcp.CallToolRe
 func (gs *Server) HandleGitCheckout(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	branchName, ok := req.Arguments["branch_name"].(string)
@@ -448,7 +450,7 @@ func (gs *Server) HandleGitCheckout(req mcp.CallToolRequest) (mcp.CallToolResult
 func (gs *Server) HandleGitShow(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	revision, ok := req.Arguments["revision"].(string)
@@ -467,7 +469,7 @@ func (gs *Server) HandleGitShow(req mcp.CallToolRequest) (mcp.CallToolResult, er
 func (gs *Server) HandleGitInit(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
 	repoPath, ok := req.Arguments["repo_path"].(string)
 	if !ok {
-		return mcp.NewToolError("repo_path is required"), nil
+		repoPath = gs.repoPath
 	}
 
 	// Ensure directory exists