Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
184d5a95dd | ||
|
|
e2594f45f2 |
+1
-38
@@ -38,44 +38,7 @@ func main() {
|
||||
ownerAllow := allowlist.New(cfg.AllowedOwners)
|
||||
|
||||
reg := registry.New()
|
||||
reg.Register(tools.NewRepoList(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewRepoGet(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewRepoSearch(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewRepoStatus(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewFileRead(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewFileWriteBranch(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewFileDelete(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewDirList(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewBranchList(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewBranchDelete(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewBranchProtectionGet(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewPRCreate(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewPRGet(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewPRList(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewPRMerge(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewPRComment(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewPRFilesDiff(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewWorkflowRunTrigger(giteaClient, ownerAllow, cfg.GiteaBaseURL))
|
||||
reg.Register(tools.NewWorkflowRunStatus(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewCodeSearch(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewIssueCreate(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewIssueEdit(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewIssueComment(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewCreateProjectFromTemplate(giteaClient, ownerAllow, "mathias", "template-go-web"))
|
||||
reg.Register(tools.NewTagCreate(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewRepoCreate(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewRepoUpdate(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewRepoMirrorPush(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewRepoTree(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewRepoTopicsUpdate(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewIssueGet(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewIssueList(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewIssueListComments(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewIssueClose(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewIssueReopen(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewWorkflowRunList(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewReleaseCreate(giteaClient, ownerAllow))
|
||||
reg.Register(tools.NewRepoDelete(giteaClient, ownerAllow))
|
||||
tools.RegisterAll(reg, giteaClient, ownerAllow, cfg.GiteaBaseURL, "mathias", "template-go-web")
|
||||
|
||||
mcpSrv := mcp.NewServer(mcp.ServerOptions{
|
||||
Registry: reg,
|
||||
|
||||
@@ -3,8 +3,10 @@ package gitea
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/golang-lru/v2/expirable"
|
||||
@@ -40,7 +42,21 @@ func (c *Client) DefaultBranch(ctx context.Context, owner, name string) (string,
|
||||
return repo.DefaultBranch, nil
|
||||
}
|
||||
|
||||
// hasEmptySegment reports whether the path portion (before any query string)
|
||||
// contains an empty segment ("//"), which means an owner or repo path
|
||||
// parameter was empty. Forwarding it upstream yields gitea's opaque
|
||||
// /api/swagger 404 (#36), so callers reject it locally instead.
|
||||
func hasEmptySegment(path string) bool {
|
||||
if i := strings.IndexByte(path, '?'); i >= 0 {
|
||||
path = path[:i]
|
||||
}
|
||||
return strings.Contains(path, "//")
|
||||
}
|
||||
|
||||
func (c *Client) doOnce(ctx context.Context, method, path string, body []byte) ([]byte, int, error) {
|
||||
if hasEmptySegment(path) {
|
||||
return nil, 0, fmt.Errorf("%w: upstream path %q has an empty owner or repo segment", ErrValidation, path)
|
||||
}
|
||||
var reader io.Reader
|
||||
if body != nil {
|
||||
reader = bytes.NewReader(body)
|
||||
@@ -107,6 +123,9 @@ type rawResponse struct {
|
||||
}
|
||||
|
||||
func (c *Client) doRaw(ctx context.Context, method, path string, body []byte) (*rawResponse, error) {
|
||||
if hasEmptySegment(path) {
|
||||
return nil, fmt.Errorf("%w: upstream path %q has an empty owner or repo segment", ErrValidation, path)
|
||||
}
|
||||
var reader io.Reader
|
||||
if body != nil {
|
||||
reader = bytes.NewReader(body)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package gitea_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// #36 second line of defence: when owner or repo is empty the path contains an
|
||||
// empty segment ("//"). Rather than forward it upstream — where gitea answers
|
||||
// with its opaque /api/swagger 404 — the client must reject it locally with a
|
||||
// validation error and never touch the network.
|
||||
func TestEmptyPathSegmentRejectedBeforeNetwork(t *testing.T) {
|
||||
var hits int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddInt32(&hits, 1)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
|
||||
paths := []string{
|
||||
"/api/v1/repos/mathias//issues", // empty repo
|
||||
"/api/v1/repos//gitea-mcp/contents/", // empty owner
|
||||
"/api/v1/repos/mathias//issues?state=open", // empty repo before query
|
||||
}
|
||||
for _, p := range paths {
|
||||
_, _, err := c.GetJSON(context.Background(), p)
|
||||
require.Error(t, err, "path %q should be rejected", p)
|
||||
assert.ErrorIs(t, err, gitea.ErrValidation)
|
||||
}
|
||||
assert.Equal(t, int32(0), atomic.LoadInt32(&hits), "guard must short-circuit before any HTTP call")
|
||||
}
|
||||
|
||||
// A well-formed path with a query string must still pass the guard.
|
||||
func TestWellFormedPathPasses(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`[]`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
_, status, err := c.GetJSON(context.Background(), "/api/v1/repos/mathias/gitea-mcp/issues?state=open")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 200, status)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package tools_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"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/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// #36: every caller (claude.ai connector, gitea's own convention) sends the
|
||||
// repo identifier as `repo` and issue/PR index as `index`, but the tools
|
||||
// declare them as `name` and `number`. Unmatched fields zero-valued the path
|
||||
// segment and produced gitea's misleading /api/swagger 404. parseArgs now
|
||||
// aliases repo->name and index->number so the idiomatic call works.
|
||||
func TestRepoAndIndexAliasesResolve(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args string
|
||||
wantPath string
|
||||
}{
|
||||
{"repo+index aliases", `{"owner":"mathias","repo":"infra","index":7}`, "/api/v1/repos/mathias/infra/issues/7"},
|
||||
{"canonical name+number", `{"owner":"mathias","name":"infra","number":7}`, "/api/v1/repos/mathias/infra/issues/7"},
|
||||
{"explicit name wins over repo", `{"owner":"mathias","name":"infra","repo":"ignored","number":7}`, "/api/v1/repos/mathias/infra/issues/7"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var gotPath string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"number":7,"title":"t"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tool := tools.NewIssueGet(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"}))
|
||||
out, err := tool.Call(context.Background(), json.RawMessage(tc.args))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tc.wantPath, gotPath)
|
||||
assert.Contains(t, string(out), `"number":7`)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
// RegisterAll registers every gitea-mcp tool on reg. main.go and the
|
||||
// dispatch round-trip test share this single list so a newly added tool
|
||||
// cannot be wired in one place but missing from the other.
|
||||
//
|
||||
// giteaBaseURL is needed by workflow_run_trigger; tmplOwner/tmplRepo seed
|
||||
// create_project_from_template's default source template.
|
||||
func RegisterAll(
|
||||
reg *registry.Registry,
|
||||
c *gitea.Client,
|
||||
a *allowlist.Allowlist,
|
||||
giteaBaseURL, tmplOwner, tmplRepo string,
|
||||
) {
|
||||
reg.Register(NewRepoList(c, a))
|
||||
reg.Register(NewRepoGet(c, a))
|
||||
reg.Register(NewRepoSearch(c, a))
|
||||
reg.Register(NewRepoStatus(c, a))
|
||||
reg.Register(NewFileRead(c, a))
|
||||
reg.Register(NewFileWriteBranch(c, a))
|
||||
reg.Register(NewFileDelete(c, a))
|
||||
reg.Register(NewDirList(c, a))
|
||||
reg.Register(NewBranchList(c, a))
|
||||
reg.Register(NewBranchDelete(c, a))
|
||||
reg.Register(NewBranchProtectionGet(c, a))
|
||||
reg.Register(NewPRCreate(c, a))
|
||||
reg.Register(NewPRGet(c, a))
|
||||
reg.Register(NewPRList(c, a))
|
||||
reg.Register(NewPRMerge(c, a))
|
||||
reg.Register(NewPRComment(c, a))
|
||||
reg.Register(NewPRFilesDiff(c, a))
|
||||
reg.Register(NewWorkflowRunTrigger(c, a, giteaBaseURL))
|
||||
reg.Register(NewWorkflowRunStatus(c, a))
|
||||
reg.Register(NewCodeSearch(c, a))
|
||||
reg.Register(NewIssueCreate(c, a))
|
||||
reg.Register(NewIssueEdit(c, a))
|
||||
reg.Register(NewIssueComment(c, a))
|
||||
reg.Register(NewCreateProjectFromTemplate(c, a, tmplOwner, tmplRepo))
|
||||
reg.Register(NewTagCreate(c, a))
|
||||
reg.Register(NewRepoCreate(c, a))
|
||||
reg.Register(NewRepoUpdate(c, a))
|
||||
reg.Register(NewRepoMirrorPush(c, a))
|
||||
reg.Register(NewRepoTree(c, a))
|
||||
reg.Register(NewRepoTopicsUpdate(c, a))
|
||||
reg.Register(NewIssueGet(c, a))
|
||||
reg.Register(NewIssueList(c, a))
|
||||
reg.Register(NewIssueListComments(c, a))
|
||||
reg.Register(NewIssueClose(c, a))
|
||||
reg.Register(NewIssueReopen(c, a))
|
||||
reg.Register(NewWorkflowRunList(c, a))
|
||||
reg.Register(NewReleaseCreate(c, a))
|
||||
reg.Register(NewRepoDelete(c, a))
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package tools_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// buildRegistry wires the full tool set exactly as main.go does, against an
|
||||
// unroutable gitea base URL so any handler that reaches the network fails fast
|
||||
// (connection refused) rather than hanging.
|
||||
func buildRegistry() *registry.Registry {
|
||||
reg := registry.New()
|
||||
c := gitea.NewClient("http://127.0.0.1:1", "")
|
||||
a := allowlist.New([]string{"mathias"})
|
||||
tools.RegisterAll(reg, c, a, "http://127.0.0.1:1", "mathias", "template-go-web")
|
||||
return reg
|
||||
}
|
||||
|
||||
// Every registered tool must be dispatchable and advertise a parseable input
|
||||
// schema. This is the regression guard for #36's whole class: a tool that is
|
||||
// wired up but unroutable (or ships a malformed schema) fails CI here instead
|
||||
// of 404ing a live caller.
|
||||
func TestEveryRegisteredToolIsDispatchable(t *testing.T) {
|
||||
reg := buildRegistry()
|
||||
descs := reg.Tools()
|
||||
require.NotEmpty(t, descs)
|
||||
|
||||
for _, d := range descs {
|
||||
t.Run(d.Name, func(t *testing.T) {
|
||||
require.NotEmpty(t, d.Name, "tool has empty name")
|
||||
assert.True(t, json.Valid(d.InputSchema),
|
||||
"tool %q ships invalid JSON input schema", d.Name)
|
||||
|
||||
// Dispatch with empty args. We do not care whether the call
|
||||
// succeeds (most fail allowlist/validation/network) — only that
|
||||
// the name resolves to a handler. ErrToolNotFound here means the
|
||||
// tool advertised a name the dispatcher cannot route.
|
||||
_, err := reg.Dispatch(context.Background(), d.Name, json.RawMessage(`{}`))
|
||||
assert.False(t, errors.Is(err, registry.ErrToolNotFound),
|
||||
"registered tool %q does not dispatch", d.Name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Lock the tool count so an accidental drop of a registration in RegisterAll
|
||||
// (the single source main.go and this test share) fails loudly.
|
||||
func TestRegisteredToolCount(t *testing.T) {
|
||||
assert.Len(t, buildRegistry().Tools(), 38)
|
||||
}
|
||||
+49
-1
@@ -2,6 +2,7 @@ package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/registry"
|
||||
)
|
||||
@@ -17,7 +18,54 @@ func parseArgs(raw json.RawMessage, dst any) error {
|
||||
if len(raw) == 0 {
|
||||
return json.Unmarshal([]byte("{}"), dst)
|
||||
}
|
||||
return json.Unmarshal(raw, dst)
|
||||
return json.Unmarshal(normalizeAliases(raw), dst)
|
||||
}
|
||||
|
||||
// normalizeAliases maps the near-universal gitea/GitHub argument names onto the
|
||||
// idiosyncratic ones these tools declare: `repo` -> `name`, `index` -> `number`.
|
||||
// Every MCP caller (the claude.ai connector, LLMs primed on gitea's own API)
|
||||
// reaches for `repo`/`index`; without this the unmatched fields zero-valued the
|
||||
// upstream path segment and produced gitea's misleading /api/swagger 404 (#36).
|
||||
// The alias key is left intact so a tool whose real field IS `index`
|
||||
// (e.g. pr_merge) is unaffected.
|
||||
func normalizeAliases(raw json.RawMessage) json.RawMessage {
|
||||
var m map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
return raw // not a JSON object — leave untouched
|
||||
}
|
||||
changed := aliasInto(m, "name", "repo")
|
||||
changed = aliasInto(m, "number", "index") || changed
|
||||
if !changed {
|
||||
return raw
|
||||
}
|
||||
if patched, err := json.Marshal(m); err == nil {
|
||||
return patched
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
// aliasInto copies m[alias] into m[canonical] when canonical is absent or an
|
||||
// empty/zero JSON value and alias carries a usable one. An explicit canonical
|
||||
// value always wins. Returns true if it wrote canonical.
|
||||
func aliasInto(m map[string]json.RawMessage, canonical, alias string) bool {
|
||||
av, ok := m[alias]
|
||||
if !ok || isEmptyJSON(av) {
|
||||
return false
|
||||
}
|
||||
if cv, ok := m[canonical]; ok && !isEmptyJSON(cv) {
|
||||
return false
|
||||
}
|
||||
m[canonical] = av
|
||||
return true
|
||||
}
|
||||
|
||||
func isEmptyJSON(v json.RawMessage) bool {
|
||||
switch strings.TrimSpace(string(v)) {
|
||||
case "", `""`, "null", "0":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// capLimit returns a sane page size: 0 or negative → def, > 50 → 50.
|
||||
|
||||
Reference in New Issue
Block a user