package repo import ( "context" "encoding/base64" "fmt" "codeberg.org/goern/forgejo-mcp/v2/operation/params" "codeberg.org/goern/forgejo-mcp/v2/pkg/forgejo" "codeberg.org/goern/forgejo-mcp/v2/pkg/log" "codeberg.org/goern/forgejo-mcp/v2/pkg/to" forgejo_sdk "codeberg.org/mvdkleijn/forgejo-sdk/forgejo/v2" "github.com/mark3labs/mcp-go/mcp" ) const ( GetFileToolName = "get_file_content" CreateFileToolName = "create_file" UpdateFileToolName = "update_file" DeleteFileToolName = "delete_file" ) var ( GetFileContentTool = mcp.NewTool( GetFileToolName, mcp.WithDescription("Get file content"), mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)), mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)), mcp.WithString("ref", mcp.Required(), mcp.Description(params.Ref)), mcp.WithString("filePath", mcp.Required(), mcp.Description(params.FilePath)), ) CreateFileTool = mcp.NewTool( CreateFileToolName, mcp.WithDescription("Create file"), mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)), mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)), mcp.WithString("filePath", mcp.Required(), mcp.Description(params.FilePath)), mcp.WithString("content", mcp.Required(), mcp.Description(params.Content)), mcp.WithString("message", mcp.Required(), mcp.Description(params.Message)), mcp.WithString("branch_name", mcp.Required(), mcp.Description(params.BranchName)), mcp.WithString("new_branch_name", mcp.Description(params.NewBranchName)), ) UpdateFileTool = mcp.NewTool( UpdateFileToolName, mcp.WithDescription("Update file"), mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)), mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)), mcp.WithString("filePath", mcp.Required(), mcp.Description(params.FilePath)), mcp.WithString("content", mcp.Required(), mcp.Description(params.Content)), mcp.WithString("message", mcp.Required(), mcp.Description(params.Message)), mcp.WithString("branch_name", mcp.Required(), mcp.Description(params.BranchName)), mcp.WithString("sha", mcp.Required(), mcp.Description(params.SHA)), mcp.WithString("new_branch_name", mcp.Description(params.NewBranchName)), ) DeleteFileTool = mcp.NewTool( DeleteFileToolName, mcp.WithDescription("Delete file"), mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)), mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)), mcp.WithString("filePath", mcp.Required(), mcp.Description(params.FilePath)), mcp.WithString("message", mcp.Required(), mcp.Description(params.Message)), mcp.WithString("branch_name", mcp.Required(), mcp.Description(params.BranchName)), mcp.WithString("sha", mcp.Required(), mcp.Description(params.SHA)), mcp.WithString("new_branch_name", mcp.Description(params.NewBranchName)), ) ) func GetFileContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called GetFileFn") owner, ok := req.GetArguments()["owner"].(string) if !ok { return to.ErrorResult(fmt.Errorf("owner is required")) } repo, ok := req.GetArguments()["repo"].(string) if !ok { return to.ErrorResult(fmt.Errorf("repo is required")) } ref, _ := req.GetArguments()["ref"].(string) filePath, ok := req.GetArguments()["filePath"].(string) if !ok { return to.ErrorResult(fmt.Errorf("filePath is required")) } content, _, err := forgejo.Client().GetContents(owner, repo, ref, filePath) if err != nil { return to.ErrorResult(fmt.Errorf("get file err: %v", err)) } return to.TextResult(content) } func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called CreateFileFn") owner, _ := req.GetArguments()["owner"].(string) repo, _ := req.GetArguments()["repo"].(string) filePath, _ := req.GetArguments()["filePath"].(string) content, _ := req.GetArguments()["content"].(string) message, _ := req.GetArguments()["message"].(string) branchName, _ := req.GetArguments()["branch_name"].(string) newBranchName, ok := req.GetArguments()["new_branch_name"].(string) if !ok || newBranchName == "" { newBranchName = "" } opt := forgejo_sdk.CreateFileOptions{ FileOptions: forgejo_sdk.FileOptions{ Message: message, BranchName: branchName, NewBranchName: newBranchName, }, Content: base64.StdEncoding.EncodeToString([]byte(content)), } fileResp, _, err := forgejo.Client().CreateFile(owner, repo, filePath, opt) if err != nil { return to.ErrorResult(fmt.Errorf("create file error: %v", err)) } return to.TextResult(fileResp) } func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called UpdateFileFn") owner, _ := req.GetArguments()["owner"].(string) repo, _ := req.GetArguments()["repo"].(string) filePath, _ := req.GetArguments()["filePath"].(string) content, _ := req.GetArguments()["content"].(string) message, _ := req.GetArguments()["message"].(string) branchName, _ := req.GetArguments()["branch_name"].(string) sha, _ := req.GetArguments()["sha"].(string) newBranchName, ok := req.GetArguments()["new_branch_name"].(string) if !ok || newBranchName == "" { newBranchName = "" } opt := forgejo_sdk.UpdateFileOptions{ FileOptions: forgejo_sdk.FileOptions{ Message: message, BranchName: branchName, NewBranchName: newBranchName, }, SHA: sha, Content: base64.StdEncoding.EncodeToString([]byte(content)), } fileResp, _, err := forgejo.Client().UpdateFile(owner, repo, filePath, opt) if err != nil { return to.ErrorResult(fmt.Errorf("update file error: %v", err)) } return to.TextResult(fileResp) } func DeleteFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called DeleteFileFn") owner, ok := req.GetArguments()["owner"].(string) if !ok { return to.ErrorResult(fmt.Errorf("owner is required")) } repo, ok := req.GetArguments()["repo"].(string) if !ok { return to.ErrorResult(fmt.Errorf("repo is required")) } filePath, ok := req.GetArguments()["filePath"].(string) if !ok { return to.ErrorResult(fmt.Errorf("filePath is required")) } message, _ := req.GetArguments()["message"].(string) branchName, _ := req.GetArguments()["branch_name"].(string) sha, ok := req.GetArguments()["sha"].(string) if !ok { return to.ErrorResult(fmt.Errorf("sha is required")) } opt := forgejo_sdk.DeleteFileOptions{ FileOptions: forgejo_sdk.FileOptions{ Message: message, BranchName: branchName, }, SHA: sha, } _, err := forgejo.Client().DeleteFile(owner, repo, filePath, opt) if err != nil { return to.ErrorResult(fmt.Errorf("delete file err: %v", err)) } return to.TextResult("Delete file success") }