This commit is contained in:
hiifong
2025-03-23 14:53:15 +08:00
parent c99f1306e3
commit 24310675ba
9 changed files with 417 additions and 13 deletions

View File

@@ -2,16 +2,20 @@ package repo
import (
"context"
"fmt"
"gitea.com/gitea/gitea-mcp/pkg/gitea"
"gitea.com/gitea/gitea-mcp/pkg/log"
"github.com/mark3labs/mcp-go/mcp"
"gitea.com/gitea/gitea-mcp/pkg/to"
gitea_sdk "code.gitea.io/sdk/gitea"
"github.com/mark3labs/mcp-go/mcp"
)
const (
CreateBranchToolName = "create_branch"
DeleteBranchToolName = "delete_branch"
ListBranchesToolName = "list_branches"
)
var (
@@ -23,6 +27,21 @@ var (
mcp.WithString("branch", mcp.Required(), mcp.Description("Name of the branch to create"), mcp.DefaultString("")),
mcp.WithString("old_branch", mcp.Description("Name of the old branch to create from"), mcp.DefaultString("")),
)
DeleteBranchTool = mcp.NewTool(
DeleteBranchToolName,
mcp.WithDescription("Delete branch"),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner"), mcp.DefaultString("")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name"), mcp.DefaultString("")),
mcp.WithString("branch", mcp.Required(), mcp.Description("Name of the branch to delete"), mcp.DefaultString("")),
)
ListBranchesTool = mcp.NewTool(
ListBranchesToolName,
mcp.WithDescription("List branches"),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner"), mcp.DefaultString("")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name"), mcp.DefaultString("")),
)
)
func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
@@ -37,8 +56,39 @@ func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
OldBranchName: oldBranch,
})
if err != nil {
return mcp.NewToolResultError("Create Branch Error"), err
return nil, fmt.Errorf("Create Branch Error: %v", err)
}
return mcp.NewToolResultText("Branch Created"), nil
}
func DeleteBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called DeleteBranchFn")
owner := req.Params.Arguments["owner"].(string)
repo := req.Params.Arguments["repo"].(string)
branch := req.Params.Arguments["branch"].(string)
_, _, err := gitea.Client().DeleteRepoBranch(owner, repo, branch)
if err != nil {
return nil, fmt.Errorf("Delete Branch Error: %v", err)
}
return to.TextResult("Branch Deleted")
}
func ListBranchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called ListBranchesFn")
owner := req.Params.Arguments["owner"].(string)
repo := req.Params.Arguments["repo"].(string)
opt := gitea_sdk.ListRepoBranchesOptions{
ListOptions: gitea_sdk.ListOptions{
Page: 1,
PageSize: 100,
},
}
branches, _, err := gitea.Client().ListRepoBranches(owner, repo, opt)
if err != nil {
return nil, fmt.Errorf("List Branches Error: %v", err)
}
return to.TextResult(branches)
}

46
operation/repo/commit.go Normal file
View File

@@ -0,0 +1,46 @@
package repo
import (
"context"
"fmt"
gitea_sdk "code.gitea.io/sdk/gitea"
"gitea.com/gitea/gitea-mcp/pkg/gitea"
"gitea.com/gitea/gitea-mcp/pkg/log"
"gitea.com/gitea/gitea-mcp/pkg/to"
"github.com/mark3labs/mcp-go/mcp"
)
const (
ListRepoCommitsToolName = "list_repo_commits"
)
var (
ListRepoCommitsTool = mcp.NewTool(
ListRepoCommitsToolName,
mcp.WithDescription("List repository commits"),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner"), mcp.DefaultString("")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name"), mcp.DefaultString("")),
mcp.WithString("sha", mcp.Description("sha"), mcp.DefaultString("")),
mcp.WithString("path", mcp.Description("path"), mcp.DefaultString("")),
)
)
func ListRepoCommitsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called ListRepoCommitsFn")
owner := req.Params.Arguments["owner"].(string)
repo := req.Params.Arguments["repo"].(string)
opt := gitea_sdk.ListCommitOptions{
ListOptions: gitea_sdk.ListOptions{
Page: 1,
PageSize: 1000,
},
SHA: req.Params.Arguments["sha"].(string),
Path: req.Params.Arguments["path"].(string),
}
commits, _, err := gitea.Client().ListRepoCommits(owner, repo, opt)
if err != nil {
return nil, fmt.Errorf("list repo commits err: %v", err)
}
return to.TextResult(commits)
}

143
operation/repo/file.go Normal file
View File

@@ -0,0 +1,143 @@
package repo
import (
"context"
"fmt"
"gitea.com/gitea/gitea-mcp/pkg/gitea"
"gitea.com/gitea/gitea-mcp/pkg/log"
"gitea.com/gitea/gitea-mcp/pkg/to"
gitea_sdk "code.gitea.io/sdk/gitea"
"github.com/mark3labs/mcp-go/mcp"
)
const (
GetFileToolName = "get_file"
CreateFileToolName = "create_file"
UpdateFileToolName = "update_file"
DeleteFileToolName = "delete_file"
)
var (
GetFileTool = mcp.NewTool(
GetFileToolName,
mcp.WithDescription("Get file"),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner"), mcp.DefaultString("")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name"), mcp.DefaultString("")),
mcp.WithString("ref", mcp.Required(), mcp.Description("ref"), mcp.DefaultString("")),
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path"), mcp.DefaultString("")),
)
CreateFileTool = mcp.NewTool(
CreateFileToolName,
mcp.WithDescription("Create file"),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner"), mcp.DefaultString("")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name"), mcp.DefaultString("")),
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path"), mcp.DefaultString("")),
mcp.WithString("content", mcp.Required(), mcp.Description("file content"), mcp.DefaultString("")),
mcp.WithString("message", mcp.Required(), mcp.Description("commit message"), mcp.DefaultString("")),
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name"), mcp.DefaultString("")),
mcp.WithString("new_branch_name", mcp.Description("new branch name"), mcp.DefaultString("")),
)
UpdateFileTool = mcp.NewTool(
UpdateFileToolName,
mcp.WithDescription("Update file"),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner"), mcp.DefaultString("")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name"), mcp.DefaultString("")),
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path"), mcp.DefaultString("")),
mcp.WithString("content", mcp.Required(), mcp.Description("file content"), mcp.DefaultString("")),
mcp.WithString("message", mcp.Required(), mcp.Description("commit message"), mcp.DefaultString("")),
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name"), mcp.DefaultString("")),
mcp.WithString("new_branch_name", mcp.Description("new branch name"), mcp.DefaultString("")),
mcp.WithString("from_path", mcp.Description("from path"), mcp.DefaultString("")),
mcp.WithString("sha", mcp.Description("sha"), mcp.DefaultString("")),
)
DeleteFileTool = mcp.NewTool(
DeleteFileToolName,
mcp.WithDescription("Delete file"),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner"), mcp.DefaultString("")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name"), mcp.DefaultString("")),
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path"), mcp.DefaultString("")),
mcp.WithString("message", mcp.Required(), mcp.Description("commit message"), mcp.DefaultString("")),
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name"), mcp.DefaultString("")),
mcp.WithString("sha", mcp.Description("sha"), mcp.DefaultString("")),
)
)
func GetFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called GetFileFn")
owner := req.Params.Arguments["owner"].(string)
repo := req.Params.Arguments["repo"].(string)
ref := req.Params.Arguments["ref"].(string)
filePath := req.Params.Arguments["filePath"].(string)
file, _, err := gitea.Client().GetFile(owner, repo, ref, filePath)
if err != nil {
return nil, fmt.Errorf("get file err: %v", err)
}
return to.TextResult(file)
}
func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called CreateFileFn")
owner := req.Params.Arguments["owner"].(string)
repo := req.Params.Arguments["repo"].(string)
filePath := req.Params.Arguments["filePath"].(string)
opt := gitea_sdk.CreateFileOptions{
Content: req.Params.Arguments["content"].(string),
FileOptions: gitea_sdk.FileOptions{
Message: req.Params.Arguments["message"].(string),
BranchName: req.Params.Arguments["branch_name"].(string),
NewBranchName: req.Params.Arguments["new_branch_name"].(string),
},
}
_, _, err := gitea.Client().CreateFile(owner, repo, filePath, opt)
if err != nil {
return nil, fmt.Errorf("create file err: %v", err)
}
return to.TextResult("Create file success")
}
func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called UpdateFileFn")
owner := req.Params.Arguments["owner"].(string)
repo := req.Params.Arguments["repo"].(string)
filePath := req.Params.Arguments["filePath"].(string)
opt := gitea_sdk.UpdateFileOptions{
Content: req.Params.Arguments["content"].(string),
FromPath: req.Params.Arguments["from_path"].(string),
SHA: req.Params.Arguments["sha"].(string),
FileOptions: gitea_sdk.FileOptions{
Message: req.Params.Arguments["message"].(string),
BranchName: req.Params.Arguments["branch_name"].(string),
NewBranchName: req.Params.Arguments["new_branch_name"].(string),
},
}
_, _, err := gitea.Client().UpdateFile(owner, repo, filePath, opt)
if err != nil {
return nil, fmt.Errorf("update file err: %v", err)
}
return to.TextResult("Update file success")
}
func DeleteFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called DeleteFileFn")
owner := req.Params.Arguments["owner"].(string)
repo := req.Params.Arguments["repo"].(string)
filePath := req.Params.Arguments["filePath"].(string)
opt := gitea_sdk.DeleteFileOptions{
FileOptions: gitea_sdk.FileOptions{
Message: req.Params.Arguments["message"].(string),
BranchName: req.Params.Arguments["branch_name"].(string),
},
SHA: req.Params.Arguments["sha"].(string),
}
_, err := gitea.Client().DeleteFile(owner, repo, filePath, opt)
if err != nil {
return nil, fmt.Errorf("delete file err: %v", err)
}
return to.TextResult("Delete file success")
}

View File

@@ -2,18 +2,22 @@ package repo
import (
"context"
"errors"
"fmt"
gitea_sdk "code.gitea.io/sdk/gitea"
"gitea.com/gitea/gitea-mcp/pkg/gitea"
"gitea.com/gitea/gitea-mcp/pkg/log"
"gitea.com/gitea/gitea-mcp/pkg/ptr"
"gitea.com/gitea/gitea-mcp/pkg/to"
gitea_sdk "code.gitea.io/sdk/gitea"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
const (
CreateRepoToolName = "create_repo"
ForkRepoToolName = "fork_repo"
ListMyReposToolName = "list_my_repos"
)
@@ -33,6 +37,15 @@ var (
mcp.WithString("default_branch", mcp.Description("DefaultBranch of the repository (used when initializes and in template)"), mcp.DefaultString("main")),
)
ForkRepoTool = mcp.NewTool(
ForkRepoToolName,
mcp.WithDescription("Fork repository"),
mcp.WithString("user", mcp.Required(), mcp.Description("User name of the repository to fork")),
mcp.WithString("repo", mcp.Required(), mcp.Description("Repository name to fork")),
mcp.WithString("organization", mcp.Description("Organization name to fork")),
mcp.WithString("name", mcp.Description("Name of the forked repository")),
)
ListMyReposTool = mcp.NewTool(
ListMyReposToolName,
mcp.WithDescription("List my repositories"),
@@ -43,10 +56,22 @@ var (
func RegisterTool(s *server.MCPServer) {
s.AddTool(CreateRepoTool, CreateRepoFn)
s.AddTool(ForkRepoTool, ForkRepoFn)
s.AddTool(ListMyReposTool, ListMyReposFn)
// File
s.AddTool(GetFileTool, GetFileFn)
s.AddTool(CreateFileTool, CreateFileFn)
s.AddTool(UpdateFileTool, UpdateFileFn)
s.AddTool(DeleteFileTool, DeleteFileFn)
// Branch
s.AddTool(CreateBranchTool, CreateBranchFn)
s.AddTool(DeleteBranchTool, DeleteBranchFn)
s.AddTool(ListBranchesTool, ListBranchesFn)
// Commit
s.AddTool(ListRepoCommitsTool, ListRepoCommitsFn)
}
func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
@@ -81,15 +106,30 @@ func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
return to.TextResult(repo)
}
func ForkRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called ForkRepoFn")
user := req.Params.Arguments["user"].(string)
repo := req.Params.Arguments["repo"].(string)
opt := gitea_sdk.CreateForkOption{
Organization: ptr.To(req.Params.Arguments["organization"].(string)),
Name: ptr.To(req.Params.Arguments["name"].(string)),
}
_, _, err := gitea.Client().CreateFork(user, repo, opt)
if err != nil {
return nil, fmt.Errorf("fork repository error %v", err)
}
return to.TextResult("Fork success")
}
func ListMyReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called ListMyReposFn")
page, ok := req.Params.Arguments["page"].(float64)
if !ok {
return mcp.NewToolResultError("get page number error"), nil
return nil, errors.New("get page number error")
}
size, ok := req.Params.Arguments["pageSize"].(float64)
if !ok {
return mcp.NewToolResultError("get page size number error"), nil
return nil, errors.New("get page size number error")
}
opt := gitea_sdk.ListReposOptions{
ListOptions: gitea_sdk.ListOptions{
@@ -99,7 +139,7 @@ func ListMyReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolR
}
repos, _, err := gitea.Client().ListMyRepos(opt)
if err != nil {
return mcp.NewToolResultError("List my repositories error"), err
return nil, fmt.Errorf("list my repositories error %v", err)
}
return to.TextResult(repos)