mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-08-03 07:39:22 +02:00
21aa4684d9
`-O` / `--tools` can already narrow the exposed tool set, but it needs every tool spelled out by name. `-S` / `--scope` (`GITEA_SCOPES`) complements it by selecting whole scopes, using the same names the `Scope` column of the tool tables documents. ```bash gitea-mcp -S issue,pull_request # only those scopes gitea-mcp --scope repository,branch --tools get_me # those scopes plus one extra tool ``` ## What changed - Each scope is one `tool.Tool` registry carrying a canonical name, so `operation/repo` is split into the six scopes its files already imply: `repository` (`repo.go` + `tree.go`), `file`, `branch`, `tag`, `commit`, `release`. The other twelve packages map 1:1, giving 18 scopes. - The two allowlists combine as a **union**: with neither set every tool loads; with only `--tools` set behaviour is unchanged; with both set, the selected scopes' tools plus the individually named tools load. `-r` / `GITEA_READONLY` still hides write tools on top. - Scope names are normalized on input (case, spaces, hyphens), so `Pull Request`, `pull-request` and `PULL_REQUEST` all resolve to `pull_request`. Unknown names only warn and list the valid scopes, mirroring how `--tools` treats unknown tool names. - The `Scope` column of all three READMEs now uses the canonical names verbatim, so it doubles as the reference for `--scope`, and `TestReadmeToolTables` compares that column against the registry in both directions — no translation table needed. - Flag parsing moves from `cmd.init()` into `Execute()`. `main()` only ever calls `Execute()`, so this is behaviourally equivalent, and it makes the `cmd` package testable at all: previously the `init()` parse of `os.Args` hit `flag.CommandLine`'s `ExitOnError` on `go test`'s own `-test.*` flags. ## Verification `make fmt`, `make lint-go` (0 issues) and `go test ./...` all pass. New tests cover the filter matrix (no filters / scope-only / tools-only regression / union / read-only interaction / unknown scope), scope-name uniqueness across `domainTools`, and the flag+env parsing and normalization. Smoke-tested `tools/list` over stdio against the built binary: | flags | tools exposed | | :-- | :-- | | _none_ | 54 (identical to `main`) | | `-S branch` | `create_branch`, `delete_branch`, `list_branches` | | `--scope 'Pull Request,TAG'` | the 4 `pull_request` + 4 `tag` tools | | `-O get_me` | `get_me` (unchanged) | | `-S commit -O get_me` | `get_commit`, `list_commits`, `get_me` | | `-S file -r` | `get_dir_contents`, `get_file_contents` | | `-S bogus,issue` | the 4 `issue` tools, plus a warning naming the valid scopes | Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/219 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: yp05327 <576951401@qq.com>
83 lines
2.8 KiB
Go
83 lines
2.8 KiB
Go
package operation
|
|
|
|
import (
|
|
"maps"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// toolTableRow matches a row of the "Available Tools" table in the README
|
|
// files, capturing the tool name, the scope cell and the access cell, e.g.
|
|
// "| get_me | user | Read | Get the current authenticated user |".
|
|
var toolTableRow = regexp.MustCompile(`^\|\s*([a-z_]+)\s*\|\s*([a-z_]+)\s*\|\s*(\S+)\s*\|`)
|
|
|
|
// readmeAccessLabels maps each README to the access-column labels it uses.
|
|
var readmeAccessLabels = map[string]map[string]string{
|
|
"../README.md": {"Read": "read", "Write": "write"},
|
|
"../README.zh-cn.md": {"读取": "read", "写入": "write"},
|
|
"../README.zh-tw.md": {"讀取": "read", "寫入": "write"},
|
|
}
|
|
|
|
// toolInfo is what TestReadmeToolTables tracks per tool, both as registered
|
|
// in code and as documented in a README, so the two can be compared.
|
|
type toolInfo struct {
|
|
scope string
|
|
access string
|
|
}
|
|
|
|
// TestReadmeToolTables ensures the tool tables in the README files stay in sync
|
|
// with the registered tools, in both directions and for every translation.
|
|
// The tables listed tools that no longer existed for several releases before
|
|
// anyone noticed.
|
|
// The scope names in the README are the canonical, lowercase snake_case names
|
|
// returned by (*tool.Tool).Scope(), so no translation is needed to compare them.
|
|
func TestReadmeToolTables(t *testing.T) {
|
|
registered := map[string]toolInfo{}
|
|
for _, d := range domainTools {
|
|
scope := d.Scope()
|
|
for _, st := range d.ReadTools() {
|
|
registered[st.Tool.Name] = toolInfo{scope: scope, access: "read"}
|
|
}
|
|
for _, st := range d.WriteTools() {
|
|
registered[st.Tool.Name] = toolInfo{scope: scope, access: "write"}
|
|
}
|
|
}
|
|
|
|
for path, labels := range readmeAccessLabels {
|
|
t.Run(filepath.Base(path), func(t *testing.T) {
|
|
content, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
documented := map[string]toolInfo{}
|
|
for line := range strings.SplitSeq(string(content), "\n") {
|
|
if match := toolTableRow.FindStringSubmatch(line); match != nil {
|
|
documented[match[1]] = toolInfo{scope: match[2], access: labels[match[3]]}
|
|
}
|
|
}
|
|
|
|
for _, name := range slices.Sorted(maps.Keys(registered)) {
|
|
got, ok := documented[name]
|
|
want := registered[name]
|
|
switch {
|
|
case !ok:
|
|
t.Errorf("tool %q is registered but missing from the tool table", name)
|
|
case got.access != want.access:
|
|
t.Errorf("tool %q is documented with %q access, want %q", name, got.access, want.access)
|
|
case got.scope != want.scope:
|
|
t.Errorf("tool %q is documented with scope %q, want %q", name, got.scope, want.scope)
|
|
}
|
|
}
|
|
for _, name := range slices.Sorted(maps.Keys(documented)) {
|
|
if _, ok := registered[name]; !ok {
|
|
t.Errorf("tool %q is in the tool table but is not registered", name)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|