diff --git a/README.md b/README.md index 708fc0c..3c664c4 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,7 @@ The Gitea MCP Server supports the following tools: | list_tags | Tag | List all tags in a repository | | list_repo_commits | Commit | List all commits in a repository | | get_file_content | File | Get the content and metadata of a file | +| get_dir_content | File | Get a list of entries in a directory | | create_file | File | Create a new file | | update_file | File | Update an existing file | | delete_file | File | Delete a file | diff --git a/README.zh-cn.md b/README.zh-cn.md index ed8643c..c52dde4 100644 --- a/README.zh-cn.md +++ b/README.zh-cn.md @@ -194,6 +194,7 @@ Gitea MCP 服务器支持以下工具: | list_tags | 标签 | 列出所有标签 | | list_repo_commits | 提交 | 列出仓库中的所有提交 | | get_file_content | 文件 | 获取文件的内容和元数据 | +| get_dir_content | 文件 | 获取目录的内容列表 | | create_file | 文件 | 创建一个新文件 | | update_file | 文件 | 更新现有文件 | | delete_file | 文件 | 删除一个文件 | diff --git a/README.zh-tw.md b/README.zh-tw.md index 7856bc1..7871976 100644 --- a/README.zh-tw.md +++ b/README.zh-tw.md @@ -194,6 +194,7 @@ Gitea MCP 伺服器支持以下工具: | list_tags | 標籤 | 列出所有標籤 | | list_repo_commits | 提交 | 列出倉庫中的所有提交 | | get_file_content | 文件 | 獲取文件的內容和元數據 | +| get_dir_content | 文件 | 獲取目錄的內容列表 | | create_file | 文件 | 創建一個新文件 | | update_file | 文件 | 更新現有文件 | | delete_file | 文件 | 刪除一個文件 | diff --git a/operation/repo/file.go b/operation/repo/file.go index ccab6b1..52dc2af 100644 --- a/operation/repo/file.go +++ b/operation/repo/file.go @@ -16,6 +16,7 @@ import ( const ( GetFileToolName = "get_file_content" + GetDirToolName = "get_dir_content" CreateFileToolName = "create_file" UpdateFileToolName = "update_file" DeleteFileToolName = "delete_file" @@ -31,6 +32,15 @@ var ( mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")), ) + GetDirContentTool = mcp.NewTool( + GetDirToolName, + mcp.WithDescription("Get a list of entries in a directory"), + 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")), + mcp.WithString("filePath", mcp.Required(), mcp.Description("directory path")), + ) + CreateFileTool = mcp.NewTool( CreateFileToolName, mcp.WithDescription("Create file"), @@ -72,6 +82,10 @@ func init() { Tool: GetFileContentTool, Handler: GetFileContentFn, }) + Tool.RegisterRead(server.ServerTool{ + Tool: GetDirContentTool, + Handler: GetDirContentFn, + }) Tool.RegisterWrite(server.ServerTool{ Tool: CreateFileTool, Handler: CreateFileFn, @@ -108,6 +122,28 @@ func GetFileContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTo return to.TextResult(content) } +func GetDirContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { + log.Debugf("Called GetDirContentFn") + 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")) + } + content, _, err := gitea.Client().ListContents(owner, repo, ref, filePath) + if err != nil { + return to.ErrorResult(fmt.Errorf("get dir content err: %v", err)) + } + return to.TextResult(content) +} + func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called CreateFileFn") owner, ok := req.GetArguments()["owner"].(string)