This commit is contained in:
hiifong
2025-03-23 21:32:53 +08:00
parent 065f65ad2a
commit f4bbd5a09a
7 changed files with 415 additions and 189 deletions

View File

@@ -22,34 +22,43 @@ var (
CreateBranchTool = mcp.NewTool(
CreateBranchToolName,
mcp.WithDescription("Create 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 create"), mcp.DefaultString("")),
mcp.WithString("old_branch", mcp.Description("Name of the old branch to create from"), mcp.DefaultString("")),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
mcp.WithString("branch", mcp.Required(), mcp.Description("Name of the branch to create")),
mcp.WithString("old_branch", mcp.Required(), mcp.Description("Name of the old branch to create from")),
)
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("")),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
mcp.WithString("branch", mcp.Required(), mcp.Description("Name of the branch to delete")),
)
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("")),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
)
)
func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called CreateBranchFn")
owner := req.Params.Arguments["owner"].(string)
repo := req.Params.Arguments["repo"].(string)
branch := req.Params.Arguments["branch"].(string)
oldBranch := req.Params.Arguments["old_branch"].(string)
owner, ok := req.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("owner is required")
}
repo, ok := req.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("repo is required")
}
branch, ok := req.Params.Arguments["branch"].(string)
if !ok {
return nil, fmt.Errorf("branch is required")
}
oldBranch, _ := req.Params.Arguments["old_branch"].(string)
_, _, err := gitea.Client().CreateBranch(owner, repo, gitea_sdk.CreateBranchOption{
BranchName: branch,
@@ -64,9 +73,18 @@ func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
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)
owner, ok := req.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("owner is required")
}
repo, ok := req.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("repo is required")
}
branch, ok := req.Params.Arguments["branch"].(string)
if !ok {
return nil, fmt.Errorf("branch is required")
}
_, _, err := gitea.Client().DeleteRepoBranch(owner, repo, branch)
if err != nil {
return nil, fmt.Errorf("Delete Branch Error: %v", err)
@@ -77,8 +95,14 @@ func DeleteBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
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)
owner, ok := req.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("owner is required")
}
repo, ok := req.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("repo is required")
}
opt := gitea_sdk.ListRepoBranchesOptions{
ListOptions: gitea_sdk.ListOptions{
Page: 1,

View File

@@ -4,10 +4,11 @@ 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"
gitea_sdk "code.gitea.io/sdk/gitea"
"github.com/mark3labs/mcp-go/mcp"
)
@@ -19,24 +20,32 @@ 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("")),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
mcp.WithString("sha", mcp.Description("sha")),
mcp.WithString("path", mcp.Description("path")),
)
)
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)
owner, ok := req.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("owner is required")
}
repo, ok := req.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("repo is required")
}
sha, _ := req.Params.Arguments["sha"].(string)
path, _ := req.Params.Arguments["path"].(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),
SHA: sha,
Path: path,
}
commits, _, err := gitea.Client().ListRepoCommits(owner, repo, opt)
if err != nil {

View File

@@ -23,56 +23,62 @@ 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("")),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
mcp.WithString("ref", mcp.Required(), mcp.Description("ref")),
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
)
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("")),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
mcp.WithString("content", mcp.Required(), mcp.Description("file content")),
mcp.WithString("message", mcp.Required(), mcp.Description("commit message")),
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")),
mcp.WithString("new_branch_name", mcp.Description("new branch name")),
)
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("")),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
mcp.WithString("content", mcp.Required(), mcp.Description("file content")),
mcp.WithString("message", mcp.Required(), mcp.Description("commit message")),
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")),
)
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("")),
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
mcp.WithString("message", mcp.Required(), mcp.Description("commit message")),
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")),
mcp.WithString("sha", mcp.Description("sha")),
)
)
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)
owner, ok := req.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("owner is required")
}
repo, ok := req.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("repo is required")
}
ref, _ := req.Params.Arguments["ref"].(string)
filePath, ok := req.Params.Arguments["filePath"].(string)
if !ok {
return nil, fmt.Errorf("filePath is required")
}
file, _, err := gitea.Client().GetFile(owner, repo, ref, filePath)
if err != nil {
return nil, fmt.Errorf("get file err: %v", err)
@@ -82,15 +88,26 @@ func GetFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResul
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)
owner, ok := req.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("owner is required")
}
repo, ok := req.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("repo is required")
}
filePath, ok := req.Params.Arguments["filePath"].(string)
if !ok {
return nil, fmt.Errorf("filePath is required")
}
content, _ := req.Params.Arguments["content"].(string)
message, _ := req.Params.Arguments["message"].(string)
branchName, _ := req.Params.Arguments["branch_name"].(string)
opt := gitea_sdk.CreateFileOptions{
Content: req.Params.Arguments["content"].(string),
Content: content,
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),
Message: message,
BranchName: branchName,
},
}
@@ -103,17 +120,26 @@ func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
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)
owner, ok := req.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("owner is required")
}
repo, ok := req.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("repo is required")
}
filePath, ok := req.Params.Arguments["filePath"].(string)
if !ok {
return nil, fmt.Errorf("filePath is required")
}
content, _ := req.Params.Arguments["content"].(string)
message, _ := req.Params.Arguments["message"].(string)
branchName, _ := req.Params.Arguments["branch_name"].(string)
opt := gitea_sdk.UpdateFileOptions{
Content: req.Params.Arguments["content"].(string),
FromPath: req.Params.Arguments["from_path"].(string),
SHA: req.Params.Arguments["sha"].(string),
Content: content,
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),
Message: message,
BranchName: branchName,
},
}
_, _, err := gitea.Client().UpdateFile(owner, repo, filePath, opt)
@@ -125,15 +151,25 @@ func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
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)
owner, ok := req.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("owner is required")
}
repo, ok := req.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("repo is required")
}
filePath, ok := req.Params.Arguments["filePath"].(string)
if !ok {
return nil, fmt.Errorf("filePath is required")
}
message, _ := req.Params.Arguments["message"].(string)
branchName, _ := req.Params.Arguments["branch_name"].(string)
opt := gitea_sdk.DeleteFileOptions{
FileOptions: gitea_sdk.FileOptions{
Message: req.Params.Arguments["message"].(string),
BranchName: req.Params.Arguments["branch_name"].(string),
Message: message,
BranchName: branchName,
},
SHA: req.Params.Arguments["sha"].(string),
}
_, err := gitea.Client().DeleteFile(owner, repo, filePath, opt)
if err != nil {

View File

@@ -25,16 +25,16 @@ var (
CreateRepoTool = mcp.NewTool(
CreateRepoToolName,
mcp.WithDescription("Create repository"),
mcp.WithString("name", mcp.Required(), mcp.Description("Name of the repository to create"), mcp.DefaultString("test")),
mcp.WithString("description", mcp.Description("Description of the repository to create"), mcp.DefaultString("")),
mcp.WithBoolean("private", mcp.Description("Whether the repository is private"), mcp.DefaultBool(true)),
mcp.WithString("issue_labels", mcp.Description("Issue Label set to use"), mcp.DefaultString("")),
mcp.WithBoolean("auto_init", mcp.Description("Whether the repository should be auto-intialized?"), mcp.DefaultBool(false)),
mcp.WithBoolean("template", mcp.Description("Whether the repository is template"), mcp.DefaultBool(false)),
mcp.WithString("gitignores", mcp.Description("Gitignores to use"), mcp.DefaultString("")),
mcp.WithString("license", mcp.Description("License to use"), mcp.DefaultString("MIT")),
mcp.WithString("readme", mcp.Description("Readme of the repository to create"), mcp.DefaultString("")),
mcp.WithString("default_branch", mcp.Description("DefaultBranch of the repository (used when initializes and in template)"), mcp.DefaultString("main")),
mcp.WithString("name", mcp.Required(), mcp.Description("Name of the repository to create")),
mcp.WithString("description", mcp.Description("Description of the repository to create")),
mcp.WithBoolean("private", mcp.Description("Whether the repository is private")),
mcp.WithString("issue_labels", mcp.Description("Issue Label set to use")),
mcp.WithBoolean("auto_init", mcp.Description("Whether the repository should be auto-intialized?")),
mcp.WithBoolean("template", mcp.Description("Whether the repository is template")),
mcp.WithString("gitignores", mcp.Description("Gitignores to use")),
mcp.WithString("license", mcp.Description("License to use")),
mcp.WithString("readme", mcp.Description("Readme of the repository to create")),
mcp.WithString("default_branch", mcp.Description("DefaultBranch of the repository (used when initializes and in template)")),
)
ForkRepoTool = mcp.NewTool(
@@ -50,7 +50,7 @@ var (
ListMyReposToolName,
mcp.WithDescription("List my repositories"),
mcp.WithNumber("page", mcp.Required(), mcp.Description("Page number"), mcp.DefaultNumber(1), mcp.Min(1)),
mcp.WithNumber("pageSize", mcp.Required(), mcp.Description("Page size number"), mcp.DefaultNumber(10), mcp.Min(1)),
mcp.WithNumber("pageSize", mcp.Required(), mcp.Description("Page size number"), mcp.DefaultNumber(100), mcp.Min(1)),
)
)
@@ -76,16 +76,19 @@ func RegisterTool(s *server.MCPServer) {
func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called CreateRepoFn")
name := req.Params.Arguments["name"].(string)
description := req.Params.Arguments["description"].(string)
private := req.Params.Arguments["private"].(bool)
issueLabels := req.Params.Arguments["issue_labels"].(string)
autoInit := req.Params.Arguments["auto_init"].(bool)
template := req.Params.Arguments["template"].(bool)
gitignores := req.Params.Arguments["gitignores"].(string)
license := req.Params.Arguments["license"].(string)
readme := req.Params.Arguments["readme"].(string)
defaultBranch := req.Params.Arguments["default_branch"].(string)
name, ok := req.Params.Arguments["name"].(string)
if !ok {
return nil, errors.New("repository name is required")
}
description, _ := req.Params.Arguments["description"].(string)
private, _ := req.Params.Arguments["private"].(bool)
issueLabels, _ := req.Params.Arguments["issue_labels"].(string)
autoInit, _ := req.Params.Arguments["auto_init"].(bool)
template, _ := req.Params.Arguments["template"].(bool)
gitignores, _ := req.Params.Arguments["gitignores"].(string)
license, _ := req.Params.Arguments["license"].(string)
readme, _ := req.Params.Arguments["readme"].(string)
defaultBranch, _ := req.Params.Arguments["default_branch"].(string)
opt := gitea_sdk.CreateRepoOption{
Name: name,
@@ -108,11 +111,19 @@ func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
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)
user, ok := req.Params.Arguments["user"].(string)
if !ok {
return nil, errors.New("user name is required")
}
repo, ok := req.Params.Arguments["repo"].(string)
if !ok {
return nil, errors.New("repository name is required")
}
organization, _ := req.Params.Arguments["organization"].(string)
name, _ := req.Params.Arguments["name"].(string)
opt := gitea_sdk.CreateForkOption{
Organization: ptr.To(req.Params.Arguments["organization"].(string)),
Name: ptr.To(req.Params.Arguments["name"].(string)),
Organization: ptr.To(organization),
Name: ptr.To(name),
}
_, _, err := gitea.Client().CreateFork(user, repo, opt)
if err != nil {