mirror of
				https://gitea.com/gitea/gitea-mcp.git
				synced 2025-10-30 09:51:50 +00:00 
			
		
		
		
	feat: add GetDirContent tool for retrieving directory entries (#53)
### 🚀 What's Changed This PR introduces a new MCP tool `get_dir_content` that allows users to retrieve a list of entries (files and subdirectories) from a specified directory in a Gitea repository. ### ✨ Features Added - **New Tool**: `GetDirContent` tool for directory listing functionality - **Tool Registration**: Properly registered as a read operation in the MCP server - **Parameter Validation**: Comprehensive input validation for required parameters - **Error Handling**: Robust error handling with descriptive error messages ### 🔧 Technical Details - **Tool Name**: `get_dir_content` - **Required Parameters**: - `owner`: Repository owner - `repo`: Repository name - `ref`: Branch, tag, or commit reference - `filePath`: Directory path to list ### 📁 Files Modified - file.go: Added tool definition, registration, and handler function ### 🎯 Use Cases This tool enables users to: - Browse repository directory structures - List files and folders in specific directories - Navigate repository contents programmatically - Support file management workflows in MCP clients Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/53 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: ZRE <chy853@gmail.com> Co-committed-by: ZRE <chy853@gmail.com>
This commit is contained in:
		| @@ -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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user