From b9e575ad64592aeafc3f6fa6affdefaa38303e3c Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 25 Mar 2025 03:42:37 +0000 Subject: [PATCH 1/3] fix --- operation/repo/commit.go | 18 ++++++++++++++---- operation/repo/file.go | 14 +++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/operation/repo/commit.go b/operation/repo/commit.go index 9c12fa1..04e2249 100644 --- a/operation/repo/commit.go +++ b/operation/repo/commit.go @@ -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, diff --git a/operation/repo/file.go b/operation/repo/file.go index 7c722da..35b473e 100644 --- a/operation/repo/file.go +++ b/operation/repo/file.go @@ -2,6 +2,7 @@ package repo import ( "context" + "encoding/base64" "fmt" "gitea.com/gitea/gitea-mcp/pkg/gitea" @@ -25,7 +26,7 @@ var ( mcp.WithDescription("Get file"), 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 +48,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("raw file content")), mcp.WithString("message", mcp.Required(), mcp.Description("commit message")), mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")), ) @@ -132,11 +134,17 @@ 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{ - Content: content, + SHA: sha, + Content: base64.StdEncoding.EncodeToString([]byte(content)), FileOptions: gitea_sdk.FileOptions{ Message: message, BranchName: branchName, From 0e225f21daf200c6631125da76d353132cc0b20c Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 25 Mar 2025 04:25:28 +0000 Subject: [PATCH 2/3] fix --- operation/repo/branch.go | 6 +++--- operation/repo/file.go | 17 ++++++++--------- operation/repo/repo.go | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/operation/repo/branch.go b/operation/repo/branch.go index c21a942..dd23e70 100644 --- a/operation/repo/branch.go +++ b/operation/repo/branch.go @@ -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) diff --git a/operation/repo/file.go b/operation/repo/file.go index 35b473e..1822ee7 100644 --- a/operation/repo/file.go +++ b/operation/repo/file.go @@ -2,7 +2,6 @@ package repo import ( "context" - "encoding/base64" "fmt" "gitea.com/gitea/gitea-mcp/pkg/gitea" @@ -14,16 +13,16 @@ 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 can be branch/tag/commit")), @@ -49,7 +48,7 @@ var ( mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")), mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")), mcp.WithString("sha", mcp.Required(), mcp.Description("sha is the SHA for the file that already exists")), - mcp.WithString("content", mcp.Required(), mcp.Description("raw file content")), + 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")), ) @@ -66,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 { @@ -81,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) { @@ -144,7 +143,7 @@ func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe opt := gitea_sdk.UpdateFileOptions{ SHA: sha, - Content: base64.StdEncoding.EncodeToString([]byte(content)), + Content: content, FileOptions: gitea_sdk.FileOptions{ Message: message, BranchName: branchName, diff --git a/operation/repo/repo.go b/operation/repo/repo.go index f9c9b09..ae29c2b 100644 --- a/operation/repo/repo.go +++ b/operation/repo/repo.go @@ -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) From d48e233fbafe255ea522a9768a6fad75741a0f51 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 25 Mar 2025 04:26:47 +0000 Subject: [PATCH 3/3] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e38147d..88423f9 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,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|