forgejo-mcp/operation/repo/file.go
BrilliantKahn c2c33de184 feat: name binary-file case in get_file_content description
Per maintainer review on #116 — current description tells the LLM what with_metadata returns but not when to flip it.
2026-05-06 11:44:22 +02:00

195 lines
7.4 KiB
Go

package repo
import (
"context"
"encoding/base64"
"fmt"
"codeberg.org/goern/forgejo-mcp/v2/operation/params"
"codeberg.org/goern/forgejo-mcp/v2/pkg/forgejo"
"codeberg.org/goern/forgejo-mcp/v2/pkg/log"
"codeberg.org/goern/forgejo-mcp/v2/pkg/to"
forgejo_sdk "codeberg.org/mvdkleijn/forgejo-sdk/forgejo/v3"
"github.com/mark3labs/mcp-go/mcp"
)
const (
GetFileToolName = "get_file_content"
CreateFileToolName = "create_file"
UpdateFileToolName = "update_file"
DeleteFileToolName = "delete_file"
)
var (
GetFileContentTool = mcp.NewTool(
GetFileToolName,
mcp.WithDescription("Get file content as plain text by default. Set `with_metadata=true` for binary files, or when you need the SHA/encoding/links from the full `ContentsResponse` (e.g. before a follow-up `update_file` call)."),
mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)),
mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)),
mcp.WithString("ref", mcp.Required(), mcp.Description(params.Ref)),
mcp.WithString("filePath", mcp.Required(), mcp.Description(params.FilePath)),
mcp.WithBoolean("with_metadata", mcp.Description("Return the full ContentsResponse (sha, encoding, links, type, size, base64 content) instead of plain text.")),
)
CreateFileTool = mcp.NewTool(
CreateFileToolName,
mcp.WithDescription("Create file"),
mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)),
mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)),
mcp.WithString("filePath", mcp.Required(), mcp.Description(params.FilePath)),
mcp.WithString("content", mcp.Required(), mcp.Description(params.Content)),
mcp.WithString("message", mcp.Required(), mcp.Description(params.Message)),
mcp.WithString("branch_name", mcp.Required(), mcp.Description(params.BranchName)),
mcp.WithString("new_branch_name", mcp.Description(params.NewBranchName)),
)
UpdateFileTool = mcp.NewTool(
UpdateFileToolName,
mcp.WithDescription("Update file"),
mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)),
mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)),
mcp.WithString("filePath", mcp.Required(), mcp.Description(params.FilePath)),
mcp.WithString("content", mcp.Required(), mcp.Description(params.Content)),
mcp.WithString("message", mcp.Required(), mcp.Description(params.Message)),
mcp.WithString("branch_name", mcp.Required(), mcp.Description(params.BranchName)),
mcp.WithString("sha", mcp.Required(), mcp.Description(params.SHA)),
mcp.WithString("new_branch_name", mcp.Description(params.NewBranchName)),
)
DeleteFileTool = mcp.NewTool(
DeleteFileToolName,
mcp.WithDescription("Delete file"),
mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)),
mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)),
mcp.WithString("filePath", mcp.Required(), mcp.Description(params.FilePath)),
mcp.WithString("message", mcp.Required(), mcp.Description(params.Message)),
mcp.WithString("branch_name", mcp.Required(), mcp.Description(params.BranchName)),
mcp.WithString("sha", mcp.Required(), mcp.Description(params.SHA)),
mcp.WithString("new_branch_name", mcp.Description(params.NewBranchName)),
)
)
func GetFileContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called GetFileFn")
owner, ok := req.GetArguments()["owner"].(string)
if !ok {
return to.ErrorResult(fmt.Errorf("owner is required"))
}
repo, ok := req.GetArguments()["repo"].(string)
if !ok {
return to.ErrorResult(fmt.Errorf("repo is required"))
}
ref, _ := req.GetArguments()["ref"].(string)
filePath, ok := req.GetArguments()["filePath"].(string)
if !ok {
return to.ErrorResult(fmt.Errorf("filePath is required"))
}
withMetadata, _ := req.GetArguments()["with_metadata"].(bool)
if withMetadata {
content, _, err := forgejo.Client().GetContents(owner, repo, ref, filePath)
if err != nil {
return to.ErrorResult(fmt.Errorf("get file err: %v", err))
}
return to.TextResult(content)
}
// Default: plain text via GetFile (SDK /raw/ endpoint, no base64).
// GetFile returns []byte; binary files are returned as-is without detection.
rawBytes, _, err := forgejo.Client().GetFile(owner, repo, ref, filePath)
if err != nil {
return to.ErrorResult(fmt.Errorf("get file err: %v", err))
}
return to.TextResult(string(rawBytes))
}
func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called CreateFileFn")
owner, _ := req.GetArguments()["owner"].(string)
repo, _ := req.GetArguments()["repo"].(string)
filePath, _ := req.GetArguments()["filePath"].(string)
content, _ := req.GetArguments()["content"].(string)
message, _ := req.GetArguments()["message"].(string)
branchName, _ := req.GetArguments()["branch_name"].(string)
newBranchName, ok := req.GetArguments()["new_branch_name"].(string)
if !ok || newBranchName == "" {
newBranchName = ""
}
opt := forgejo_sdk.CreateFileOptions{
FileOptions: forgejo_sdk.FileOptions{
Message: message,
BranchName: branchName,
NewBranchName: newBranchName,
},
Content: base64.StdEncoding.EncodeToString([]byte(content)),
}
fileResp, _, err := forgejo.Client().CreateFile(owner, repo, filePath, opt)
if err != nil {
return to.ErrorResult(fmt.Errorf("create file error: %v", err))
}
return to.TextResult(fileResp)
}
func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called UpdateFileFn")
owner, _ := req.GetArguments()["owner"].(string)
repo, _ := req.GetArguments()["repo"].(string)
filePath, _ := req.GetArguments()["filePath"].(string)
content, _ := req.GetArguments()["content"].(string)
message, _ := req.GetArguments()["message"].(string)
branchName, _ := req.GetArguments()["branch_name"].(string)
sha, _ := req.GetArguments()["sha"].(string)
newBranchName, ok := req.GetArguments()["new_branch_name"].(string)
if !ok || newBranchName == "" {
newBranchName = ""
}
opt := forgejo_sdk.UpdateFileOptions{
FileOptions: forgejo_sdk.FileOptions{
Message: message,
BranchName: branchName,
NewBranchName: newBranchName,
},
SHA: sha,
Content: base64.StdEncoding.EncodeToString([]byte(content)),
}
fileResp, _, err := forgejo.Client().UpdateFile(owner, repo, filePath, opt)
if err != nil {
return to.ErrorResult(fmt.Errorf("update file error: %v", err))
}
return to.TextResult(fileResp)
}
func DeleteFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
log.Debugf("Called DeleteFileFn")
owner, ok := req.GetArguments()["owner"].(string)
if !ok {
return to.ErrorResult(fmt.Errorf("owner is required"))
}
repo, ok := req.GetArguments()["repo"].(string)
if !ok {
return to.ErrorResult(fmt.Errorf("repo is required"))
}
filePath, ok := req.GetArguments()["filePath"].(string)
if !ok {
return to.ErrorResult(fmt.Errorf("filePath is required"))
}
message, _ := req.GetArguments()["message"].(string)
branchName, _ := req.GetArguments()["branch_name"].(string)
sha, ok := req.GetArguments()["sha"].(string)
if !ok {
return to.ErrorResult(fmt.Errorf("sha is required"))
}
opt := forgejo_sdk.DeleteFileOptions{
FileOptions: forgejo_sdk.FileOptions{
Message: message,
BranchName: branchName,
},
SHA: sha,
}
_, err := forgejo.Client().DeleteFile(owner, repo, filePath, opt)
if err != nil {
return to.ErrorResult(fmt.Errorf("delete file err: %v", err))
}
return to.TextResult("Delete file success")
}