Files
gitea-mcp/internal/gitea/files.go
Mathias Bergqvist 044086b067 feat(tools): file_read with default-branch resolution
Adds GetFileContents to the gitea client and a file_read MCP tool.
When ref is omitted, the tool resolves the repo default_branch via
GetRepo before fetching contents. Decoded content capped at 1 MiB.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-04 22:11:50 +02:00

35 lines
735 B
Go

package gitea
import (
"context"
"encoding/json"
"fmt"
)
type FileContents struct {
Path string `json:"path"`
Sha string `json:"sha"`
Size int64 `json:"size"`
Content string `json:"content"`
Encoding string `json:"encoding"`
}
func (c *Client) GetFileContents(ctx context.Context, owner, repo, path, ref string) (*FileContents, error) {
p := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, path)
if ref != "" {
p += "?ref=" + ref
}
body, status, err := c.GetJSON(ctx, p)
if err != nil {
return nil, err
}
if err := MapStatus(status, body); err != nil {
return nil, err
}
var fc FileContents
if err := json.Unmarshal(body, &fc); err != nil {
return nil, err
}
return &fc, nil
}