From a601d6b69840d8bea2095be8771cffd56bdd62f0 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 7 Aug 2025 02:18:01 +0000 Subject: [PATCH] Remove last empty line in GetFileContentFn (#80) Normally, each file should be end with a blank line, but git does not consider it as a new line, so we should not return it to llm, or it may generate wrong information when editing the existing file. Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/80 Reviewed-by: hiifong Co-authored-by: yp05327 <576951401@qq.com> Co-committed-by: yp05327 <576951401@qq.com> --- operation/repo/file.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/operation/repo/file.go b/operation/repo/file.go index 92a24fb..26cce66 100644 --- a/operation/repo/file.go +++ b/operation/repo/file.go @@ -149,6 +149,16 @@ func GetFileContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTo }) } + if err := scanner.Err(); err != nil { + return to.ErrorResult(fmt.Errorf("scan content err: %v", err)) + } + + // remove the last blank line if exists + // git does not consider the last line as a new line + if contentLines[len(contentLines)-1].Content == "" { + contentLines = contentLines[:len(contentLines)-1] + } + contentBytes, err := json.MarshalIndent(contentLines, "", " ") if err != nil { return to.ErrorResult(fmt.Errorf("marshal content lines err: %v", err))