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:
Lunny Xiao
2025-03-25 04:30:18 +00:00
5 changed files with 34 additions and 17 deletions

View File

@@ -97,7 +97,7 @@ The Gitea MCP Server supports the following tools:
|delete_branch|Branch|Delete a branch|
|list_branches|Branch|List all branches 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|
|update_file|File|Update an existing file|
|delete_file|File|Delete a file|

View File

@@ -65,7 +65,7 @@ func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
OldBranchName: oldBranch,
})
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
@@ -87,7 +87,7 @@ func DeleteBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
}
_, _, err := gitea.Client().DeleteRepoBranch(owner, repo, branch)
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")
@@ -111,7 +111,7 @@ func ListBranchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
}
branches, _, err := gitea.Client().ListRepoBranches(owner, repo, opt)
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)

View File

@@ -22,8 +22,10 @@ var (
mcp.WithDescription("List repository commits"),
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")),
mcp.WithString("sha", mcp.Description("SHA or branch to start listing commits from")),
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 {
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)
path, _ := req.Params.Arguments["path"].(string)
opt := gitea_sdk.ListCommitOptions{
ListOptions: gitea_sdk.ListOptions{
Page: 1,
PageSize: 1000,
Page: int(page),
PageSize: int(pageSize),
},
SHA: sha,
Path: path,

View File

@@ -13,19 +13,19 @@ import (
)
const (
GetFileToolName = "get_file"
GetFileToolName = "get_file_content"
CreateFileToolName = "create_file"
UpdateFileToolName = "update_file"
DeleteFileToolName = "delete_file"
)
var (
GetFileTool = mcp.NewTool(
GetFileContentTool = mcp.NewTool(
GetFileToolName,
mcp.WithDescription("Get file"),
mcp.WithDescription("Get file Content and Metadata"),
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("ref", mcp.Required(), mcp.Description("ref can be branch/tag/commit")),
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("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("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("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")
owner, ok := req.Params.Arguments["owner"].(string)
if !ok {
@@ -79,11 +80,11 @@ func GetFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResul
if !ok {
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 {
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) {
@@ -132,10 +133,16 @@ func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
if !ok {
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)
message, _ := req.Params.Arguments["message"].(string)
branchName, _ := req.Params.Arguments["branch_name"].(string)
opt := gitea_sdk.UpdateFileOptions{
SHA: sha,
Content: content,
FileOptions: gitea_sdk.FileOptions{
Message: message,

View File

@@ -60,7 +60,7 @@ func RegisterTool(s *server.MCPServer) {
s.AddTool(ListMyReposTool, ListMyReposFn)
// File
s.AddTool(GetFileTool, GetFileFn)
s.AddTool(GetFileContentTool, GetFileContentFn)
s.AddTool(CreateFileTool, CreateFileFn)
s.AddTool(UpdateFileTool, UpdateFileFn)
s.AddTool(DeleteFileTool, DeleteFileFn)