mirror of
				https://gitea.com/gitea/gitea-mcp.git
				synced 2025-11-03 20:01:50 +00:00 
			
		
		
		
	Add withLines option to get_file_content (#76)
Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/76 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: yp05327 <576951401@qq.com> Co-committed-by: yp05327 <576951401@qq.com>
This commit is contained in:
		@@ -1,8 +1,11 @@
 | 
			
		||||
package repo
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"context"
 | 
			
		||||
	"encoding/base64"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"gitea.com/gitea/gitea-mcp/pkg/gitea"
 | 
			
		||||
@@ -30,6 +33,7 @@ var (
 | 
			
		||||
		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("file path")),
 | 
			
		||||
		mcp.WithBoolean("withLines", mcp.Description("whether to return file content with lines")),
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	GetDirContentTool = mcp.NewTool(
 | 
			
		||||
@@ -100,6 +104,11 @@ func init() {
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ContentLine struct {
 | 
			
		||||
	LineNumber int    `json:"line"`
 | 
			
		||||
	Content    string `json:"content"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetFileContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
 | 
			
		||||
	log.Debugf("Called GetFileFn")
 | 
			
		||||
	owner, ok := req.GetArguments()["owner"].(string)
 | 
			
		||||
@@ -119,6 +128,34 @@ func GetFileContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTo
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return to.ErrorResult(fmt.Errorf("get file err: %v", err))
 | 
			
		||||
	}
 | 
			
		||||
	withLines, _ := req.GetArguments()["withLines"].(bool)
 | 
			
		||||
	if withLines {
 | 
			
		||||
		rawContent, err := base64.StdEncoding.DecodeString(*content.Content)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return to.ErrorResult(fmt.Errorf("decode base64 content err: %v", err))
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		contentLines := make([]ContentLine, 0)
 | 
			
		||||
		line := 0
 | 
			
		||||
 | 
			
		||||
		scanner := bufio.NewScanner(bytes.NewReader(rawContent))
 | 
			
		||||
 | 
			
		||||
		for scanner.Scan() {
 | 
			
		||||
			line++
 | 
			
		||||
 | 
			
		||||
			contentLines = append(contentLines, ContentLine{
 | 
			
		||||
				LineNumber: line,
 | 
			
		||||
				Content:    scanner.Text(),
 | 
			
		||||
			})
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
		contentBytes, err := json.MarshalIndent(contentLines, "", "  ")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return to.ErrorResult(fmt.Errorf("marshal content lines err: %v", err))
 | 
			
		||||
		}
 | 
			
		||||
		contentStr := string(contentBytes)
 | 
			
		||||
		content.Content = &contentStr
 | 
			
		||||
	}
 | 
			
		||||
	return to.TextResult(content)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user