Files
gitea-mcp/internal/tools/file_read.go
Mathias Bergqvist 4274b48ea5 feat(gitea): default-branch lru cache
Shared LRU avoids repeated Gitea calls for default-branch resolution;
the simple stdlib map alternative would race on concurrent access without
a mutex per entry, which is more code than the LRU.
2026-05-04 23:06:06 +02:00

89 lines
2.0 KiB
Go

package tools
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist"
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
"gitea.d-ma.be/mathias/gitea-mcp/internal/registry"
)
const fileReadMaxBytes = 1 << 20 // 1 MiB
type FileRead struct {
c *gitea.Client
a *allowlist.Allowlist
}
func NewFileRead(c *gitea.Client, a *allowlist.Allowlist) *FileRead {
return &FileRead{c: c, a: a}
}
func (t *FileRead) Descriptor() registry.ToolDescriptor {
return registry.ToolDescriptor{
Name: "file_read",
Description: "Read a file from a repo at a given ref. Defaults to the repo's default branch.",
InputSchema: json.RawMessage(`{
"type":"object",
"properties":{
"owner":{"type":"string"},
"name":{"type":"string"},
"path":{"type":"string"},
"ref":{"type":"string"}
},
"required":["owner","name","path"]
}`),
}
}
type fileReadArgs struct {
Owner string `json:"owner"`
Name string `json:"name"`
Path string `json:"path"`
Ref string `json:"ref"`
}
func (t *FileRead) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) {
var args fileReadArgs
if err := parseArgs(raw, &args); err != nil {
return nil, err
}
if err := t.a.Check(args.Owner); err != nil {
return nil, err
}
ref := args.Ref
if ref == "" {
var err error
ref, err = t.c.DefaultBranch(ctx, args.Owner, args.Name)
if err != nil {
return nil, err
}
}
fc, err := t.c.GetFileContents(ctx, args.Owner, args.Name, args.Path, ref)
if err != nil {
return nil, err
}
if fc.Size > fileReadMaxBytes {
return nil, fmt.Errorf("file %q size %d exceeds 1MiB cap: %w", args.Path, fc.Size, gitea.ErrValidation)
}
decoded, err := base64.StdEncoding.DecodeString(fc.Content)
if err != nil {
return nil, fmt.Errorf("decode base64 content: %w", err)
}
return textOK(map[string]any{
"path": fc.Path,
"ref": ref,
"sha": fc.Sha,
"size": fc.Size,
"content": string(decoded),
})
}