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>
This commit is contained in:
34
internal/gitea/files.go
Normal file
34
internal/gitea/files.go
Normal file
@@ -0,0 +1,34 @@
|
||||
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
|
||||
}
|
||||
30
internal/gitea/files_test.go
Normal file
30
internal/gitea/files_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package gitea_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetFileContents(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "/api/v1/repos/mathias/infra/contents/README.md", r.URL.Path)
|
||||
assert.Equal(t, "main", r.URL.Query().Get("ref"))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"path":"README.md","sha":"deadbeef","size":13,"content":"SGVsbG8sIHdvcmxkIQ==","encoding":"base64"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
fc, err := c.GetFileContents(context.Background(), "mathias", "infra", "README.md", "main")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "README.md", fc.Path)
|
||||
assert.Equal(t, "deadbeef", fc.Sha)
|
||||
assert.Equal(t, int64(13), fc.Size)
|
||||
assert.Equal(t, "SGVsbG8sIHdvcmxkIQ==", fc.Content)
|
||||
}
|
||||
Reference in New Issue
Block a user