mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2025-08-24 06:43:05 +00:00
Merge pull request 'Add SHA to update file, convert get_file into get_file_content' (#5) from yp05327/gitea-mcp:add-sha-to-update-file into main
Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/5 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
@@ -97,7 +97,7 @@ The Gitea MCP Server supports the following tools:
|
|||||||
|delete_branch|Branch|Delete a branch|
|
|delete_branch|Branch|Delete a branch|
|
||||||
|list_branches|Branch|List all branches in a repository|
|
|list_branches|Branch|List all branches in a repository|
|
||||||
|list_repo_commits|Commit|List all commits in a repository|
|
|list_repo_commits|Commit|List all commits in a repository|
|
||||||
|get_file|File|Get the content of a file|
|
|get_file_content|File|Get the content and metadata of a file|
|
||||||
|create_file|File|Create a new file|
|
|create_file|File|Create a new file|
|
||||||
|update_file|File|Update an existing file|
|
|update_file|File|Update an existing file|
|
||||||
|delete_file|File|Delete a file|
|
|delete_file|File|Delete a file|
|
||||||
|
@@ -65,7 +65,7 @@ func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
|
|||||||
OldBranchName: oldBranch,
|
OldBranchName: oldBranch,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Create Branch Error: %v", err)
|
return nil, fmt.Errorf("create branch error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mcp.NewToolResultText("Branch Created"), nil
|
return mcp.NewToolResultText("Branch Created"), nil
|
||||||
@@ -87,7 +87,7 @@ func DeleteBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
|
|||||||
}
|
}
|
||||||
_, _, err := gitea.Client().DeleteRepoBranch(owner, repo, branch)
|
_, _, err := gitea.Client().DeleteRepoBranch(owner, repo, branch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Delete Branch Error: %v", err)
|
return nil, fmt.Errorf("delete branch error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return to.TextResult("Branch Deleted")
|
return to.TextResult("Branch Deleted")
|
||||||
@@ -111,7 +111,7 @@ func ListBranchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
|
|||||||
}
|
}
|
||||||
branches, _, err := gitea.Client().ListRepoBranches(owner, repo, opt)
|
branches, _, err := gitea.Client().ListRepoBranches(owner, repo, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("List Branches Error: %v", err)
|
return nil, fmt.Errorf("list branches error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return to.TextResult(branches)
|
return to.TextResult(branches)
|
||||||
|
@@ -22,8 +22,10 @@ var (
|
|||||||
mcp.WithDescription("List repository commits"),
|
mcp.WithDescription("List repository commits"),
|
||||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
mcp.WithString("sha", mcp.Description("sha")),
|
mcp.WithString("sha", mcp.Description("SHA or branch to start listing commits from")),
|
||||||
mcp.WithString("path", mcp.Description("path")),
|
mcp.WithString("path", mcp.Description("path indicates that only commits that include the path's file/dir should be returned.")),
|
||||||
|
mcp.WithNumber("page", mcp.Required(), mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("page_size", mcp.Required(), mcp.Description("page size"), mcp.DefaultNumber(50), mcp.Min(1)),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,12 +39,20 @@ func ListRepoCommitsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallT
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("repo is required")
|
return nil, fmt.Errorf("repo is required")
|
||||||
}
|
}
|
||||||
|
page, ok := req.Params.Arguments["page"].(float64)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("page is required")
|
||||||
|
}
|
||||||
|
pageSize, ok := req.Params.Arguments["page_size"].(float64)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("page_size is required")
|
||||||
|
}
|
||||||
sha, _ := req.Params.Arguments["sha"].(string)
|
sha, _ := req.Params.Arguments["sha"].(string)
|
||||||
path, _ := req.Params.Arguments["path"].(string)
|
path, _ := req.Params.Arguments["path"].(string)
|
||||||
opt := gitea_sdk.ListCommitOptions{
|
opt := gitea_sdk.ListCommitOptions{
|
||||||
ListOptions: gitea_sdk.ListOptions{
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
Page: 1,
|
Page: int(page),
|
||||||
PageSize: 1000,
|
PageSize: int(pageSize),
|
||||||
},
|
},
|
||||||
SHA: sha,
|
SHA: sha,
|
||||||
Path: path,
|
Path: path,
|
||||||
|
@@ -13,19 +13,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GetFileToolName = "get_file"
|
GetFileToolName = "get_file_content"
|
||||||
CreateFileToolName = "create_file"
|
CreateFileToolName = "create_file"
|
||||||
UpdateFileToolName = "update_file"
|
UpdateFileToolName = "update_file"
|
||||||
DeleteFileToolName = "delete_file"
|
DeleteFileToolName = "delete_file"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
GetFileTool = mcp.NewTool(
|
GetFileContentTool = mcp.NewTool(
|
||||||
GetFileToolName,
|
GetFileToolName,
|
||||||
mcp.WithDescription("Get file"),
|
mcp.WithDescription("Get file Content and Metadata"),
|
||||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
mcp.WithString("ref", mcp.Required(), mcp.Description("ref")),
|
mcp.WithString("ref", mcp.Required(), mcp.Description("ref can be branch/tag/commit")),
|
||||||
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,7 +47,8 @@ var (
|
|||||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
||||||
mcp.WithString("content", mcp.Required(), mcp.Description("file content")),
|
mcp.WithString("sha", mcp.Required(), mcp.Description("sha is the SHA for the file that already exists")),
|
||||||
|
mcp.WithString("content", mcp.Required(), mcp.Description("file content, base64 encoded")),
|
||||||
mcp.WithString("message", mcp.Required(), mcp.Description("commit message")),
|
mcp.WithString("message", mcp.Required(), mcp.Description("commit message")),
|
||||||
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")),
|
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")),
|
||||||
)
|
)
|
||||||
@@ -64,7 +65,7 @@ var (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func GetFileContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called GetFileFn")
|
log.Debugf("Called GetFileFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.Params.Arguments["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -79,11 +80,11 @@ func GetFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResul
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("filePath is required")
|
return nil, fmt.Errorf("filePath is required")
|
||||||
}
|
}
|
||||||
file, _, err := gitea.Client().GetFile(owner, repo, ref, filePath)
|
content, _, err := gitea.Client().GetContents(owner, repo, ref, filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("get file err: %v", err)
|
return nil, fmt.Errorf("get file err: %v", err)
|
||||||
}
|
}
|
||||||
return to.TextResult(file)
|
return to.TextResult(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
@@ -132,10 +133,16 @@ func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("filePath is required")
|
return nil, fmt.Errorf("filePath is required")
|
||||||
}
|
}
|
||||||
|
sha, ok := req.Params.Arguments["sha"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("sha is required")
|
||||||
|
}
|
||||||
content, _ := req.Params.Arguments["content"].(string)
|
content, _ := req.Params.Arguments["content"].(string)
|
||||||
message, _ := req.Params.Arguments["message"].(string)
|
message, _ := req.Params.Arguments["message"].(string)
|
||||||
branchName, _ := req.Params.Arguments["branch_name"].(string)
|
branchName, _ := req.Params.Arguments["branch_name"].(string)
|
||||||
|
|
||||||
opt := gitea_sdk.UpdateFileOptions{
|
opt := gitea_sdk.UpdateFileOptions{
|
||||||
|
SHA: sha,
|
||||||
Content: content,
|
Content: content,
|
||||||
FileOptions: gitea_sdk.FileOptions{
|
FileOptions: gitea_sdk.FileOptions{
|
||||||
Message: message,
|
Message: message,
|
||||||
|
@@ -60,7 +60,7 @@ func RegisterTool(s *server.MCPServer) {
|
|||||||
s.AddTool(ListMyReposTool, ListMyReposFn)
|
s.AddTool(ListMyReposTool, ListMyReposFn)
|
||||||
|
|
||||||
// File
|
// File
|
||||||
s.AddTool(GetFileTool, GetFileFn)
|
s.AddTool(GetFileContentTool, GetFileContentFn)
|
||||||
s.AddTool(CreateFileTool, CreateFileFn)
|
s.AddTool(CreateFileTool, CreateFileFn)
|
||||||
s.AddTool(UpdateFileTool, UpdateFileFn)
|
s.AddTool(UpdateFileTool, UpdateFileFn)
|
||||||
s.AddTool(DeleteFileTool, DeleteFileFn)
|
s.AddTool(DeleteFileTool, DeleteFileFn)
|
||||||
|
Reference in New Issue
Block a user