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>
116 lines
3.6 KiB
Go
116 lines
3.6 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.com/gitea/gitea-mcp/pkg/annotation"
|
|
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
|
"gitea.com/gitea/gitea-mcp/pkg/params"
|
|
"gitea.com/gitea/gitea-mcp/pkg/to"
|
|
"gitea.com/gitea/gitea-mcp/pkg/tool"
|
|
|
|
gitea_sdk "gitea.dev/sdk"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
)
|
|
|
|
// CommitTool holds the commit-related tools (scope "commit").
|
|
var CommitTool = tool.New("commit")
|
|
|
|
const (
|
|
ListRepoCommitsToolName = "list_commits"
|
|
GetCommitToolName = "get_commit"
|
|
)
|
|
|
|
var (
|
|
ListRepoCommitsTool = mcp.NewTool(
|
|
ListRepoCommitsToolName,
|
|
mcp.WithDescription("List commits in a repository, optionally starting from a specific branch or SHA and filtered to commits touching a given file path."),
|
|
mcp.WithToolAnnotation(annotation.ReadOnly("List repository commits")),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description(params.OwnerDesc)),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description(params.RepoDesc)),
|
|
mcp.WithString("sha", mcp.Description("starting SHA or branch")),
|
|
mcp.WithString("path", mcp.Description("only commits touching this path")),
|
|
mcp.WithNumber("page", mcp.Description(params.PageDesc), mcp.DefaultNumber(1), mcp.Min(1)),
|
|
mcp.WithNumber("per_page", mcp.Description(params.PaginationDesc), mcp.DefaultNumber(30), mcp.Min(1)),
|
|
)
|
|
|
|
GetCommitTool = mcp.NewTool(
|
|
GetCommitToolName,
|
|
mcp.WithDescription("Get details for a single commit in a repository by its SHA."),
|
|
mcp.WithToolAnnotation(annotation.ReadOnly("Get commit details")),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description(params.OwnerDesc)),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description(params.RepoDesc)),
|
|
mcp.WithString("sha", mcp.Required()),
|
|
)
|
|
)
|
|
|
|
func init() {
|
|
CommitTool.RegisterRead(server.ServerTool{
|
|
Tool: ListRepoCommitsTool,
|
|
Handler: ListRepoCommitsFn,
|
|
})
|
|
CommitTool.RegisterRead(server.ServerTool{
|
|
Tool: GetCommitTool,
|
|
Handler: GetCommitFn,
|
|
})
|
|
}
|
|
|
|
func ListRepoCommitsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
args := req.GetArguments()
|
|
owner, err := params.GetString(args, "owner")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
repo, err := params.GetString(args, "repo")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
page, pageSize := params.GetPagination(args, 30)
|
|
sha, _ := args["sha"].(string)
|
|
path, _ := args["path"].(string)
|
|
opt := gitea_sdk.ListCommitOptions{
|
|
ListOptions: gitea_sdk.ListOptions{
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
},
|
|
SHA: sha,
|
|
Path: path,
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
commits, _, err := client.Repositories.ListRepoCommits(ctx, owner, repo, opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("list repo commits err: %v", err))
|
|
}
|
|
return to.TextResult(slimCommits(commits))
|
|
}
|
|
|
|
func GetCommitFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
args := req.GetArguments()
|
|
owner, err := params.GetString(args, "owner")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
repo, err := params.GetString(args, "repo")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
sha, err := params.GetString(args, "sha")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
commit, _, err := client.Repositories.GetSingleCommit(ctx, owner, repo, sha)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get commit %v err: %v", sha, err))
|
|
}
|
|
return to.TextResult(slimCommit(commit))
|
|
}
|