mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-08-03 15:49:23 +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>
270 lines
7.9 KiB
Go
270 lines
7.9 KiB
Go
package wiki
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"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"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
)
|
|
|
|
var Tool = tool.New("wiki")
|
|
|
|
const (
|
|
WikiReadToolName = "wiki_read"
|
|
WikiWriteToolName = "wiki_write"
|
|
)
|
|
|
|
var (
|
|
WikiReadTool = mcp.NewTool(
|
|
WikiReadToolName,
|
|
mcp.WithDescription("Read wiki: list pages, get content, revision history."),
|
|
mcp.WithToolAnnotation(annotation.ReadOnly("Read wiki pages")),
|
|
mcp.WithString("method", mcp.Required(), mcp.Enum("list", "get", "get_revisions")),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description(params.OwnerDesc)),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description(params.RepoDesc)),
|
|
mcp.WithString("pageName", mcp.Description("for 'get'/'get_revisions'")),
|
|
)
|
|
|
|
WikiWriteTool = mcp.NewTool(
|
|
WikiWriteToolName,
|
|
mcp.WithDescription("Write wiki pages: create, update, delete."),
|
|
mcp.WithToolAnnotation(annotation.Destructive("Create, update, or delete wiki pages")),
|
|
mcp.WithString("method", mcp.Required(), mcp.Enum("create", "update", "delete")),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description(params.OwnerDesc)),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description(params.RepoDesc)),
|
|
mcp.WithString("pageName", mcp.Description("for 'update'/'delete'")),
|
|
mcp.WithString("title", mcp.Description("for 'create'")),
|
|
mcp.WithString("content", mcp.Description("for 'create'/'update'")),
|
|
mcp.WithString("message", mcp.Description("commit message")),
|
|
)
|
|
)
|
|
|
|
func init() {
|
|
Tool.RegisterRead(server.ServerTool{
|
|
Tool: WikiReadTool,
|
|
Handler: wikiReadFn,
|
|
})
|
|
Tool.RegisterWrite(server.ServerTool{
|
|
Tool: WikiWriteTool,
|
|
Handler: wikiWriteFn,
|
|
})
|
|
}
|
|
|
|
func wikiReadFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
method, err := params.GetString(req.GetArguments(), "method")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
switch method {
|
|
case "list":
|
|
return listWikiPagesFn(ctx, req)
|
|
case "get":
|
|
return getWikiPageFn(ctx, req)
|
|
case "get_revisions":
|
|
return getWikiRevisionsFn(ctx, req)
|
|
default:
|
|
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
|
}
|
|
}
|
|
|
|
func wikiWriteFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
method, err := params.GetString(req.GetArguments(), "method")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
switch method {
|
|
case "create":
|
|
return createWikiPageFn(ctx, req)
|
|
case "update":
|
|
return updateWikiPageFn(ctx, req)
|
|
case "delete":
|
|
return deleteWikiPageFn(ctx, req)
|
|
default:
|
|
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
|
}
|
|
}
|
|
|
|
func listWikiPagesFn(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)
|
|
}
|
|
|
|
var result any
|
|
_, err = gitea.DoJSON(ctx, "GET", fmt.Sprintf("repos/%s/%s/wiki/pages", url.PathEscape(owner), url.PathEscape(repo)), nil, nil, &result)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("list wiki pages err: %v", err))
|
|
}
|
|
|
|
return to.TextResult(result)
|
|
}
|
|
|
|
func getWikiPageFn(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)
|
|
}
|
|
pageName, err := params.GetString(args, "pageName")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
|
|
var result any
|
|
_, err = gitea.DoJSON(ctx, "GET", fmt.Sprintf("repos/%s/%s/wiki/page/%s", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(pageName)), nil, nil, &result)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get wiki page err: %v", err))
|
|
}
|
|
|
|
return to.TextResult(result)
|
|
}
|
|
|
|
func getWikiRevisionsFn(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)
|
|
}
|
|
pageName, err := params.GetString(args, "pageName")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
|
|
var result any
|
|
_, err = gitea.DoJSON(ctx, "GET", fmt.Sprintf("repos/%s/%s/wiki/revisions/%s", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(pageName)), nil, nil, &result)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get wiki revisions err: %v", err))
|
|
}
|
|
|
|
return to.TextResult(result)
|
|
}
|
|
|
|
func createWikiPageFn(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)
|
|
}
|
|
title, err := params.GetString(args, "title")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
content, err := params.GetString(args, "content")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
|
|
message, _ := args["message"].(string)
|
|
if message == "" {
|
|
message = fmt.Sprintf("Create wiki page '%s'", title)
|
|
}
|
|
|
|
requestBody := map[string]string{
|
|
"title": title,
|
|
"content_base64": base64.StdEncoding.EncodeToString([]byte(content)),
|
|
"message": message,
|
|
}
|
|
|
|
var result any
|
|
_, err = gitea.DoJSON(ctx, "POST", fmt.Sprintf("repos/%s/%s/wiki/new", url.PathEscape(owner), url.PathEscape(repo)), nil, requestBody, &result)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("create wiki page err: %v", err))
|
|
}
|
|
|
|
return to.TextResult(result)
|
|
}
|
|
|
|
func updateWikiPageFn(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)
|
|
}
|
|
pageName, err := params.GetString(args, "pageName")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
content, err := params.GetString(args, "content")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
|
|
requestBody := map[string]string{
|
|
"content_base64": base64.StdEncoding.EncodeToString([]byte(content)),
|
|
}
|
|
|
|
// If title is given, use it. Otherwise, keep current page name
|
|
if title, ok := args["title"].(string); ok && title != "" {
|
|
requestBody["title"] = title
|
|
} else {
|
|
requestBody["title"] = pageName
|
|
}
|
|
|
|
if message, ok := args["message"].(string); ok && message != "" {
|
|
requestBody["message"] = message
|
|
} else {
|
|
requestBody["message"] = fmt.Sprintf("Update wiki page '%s'", pageName)
|
|
}
|
|
|
|
var result any
|
|
_, err = gitea.DoJSON(ctx, "PATCH", fmt.Sprintf("repos/%s/%s/wiki/page/%s", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(pageName)), nil, requestBody, &result)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("update wiki page err: %v", err))
|
|
}
|
|
|
|
return to.TextResult(result)
|
|
}
|
|
|
|
func deleteWikiPageFn(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)
|
|
}
|
|
pageName, err := params.GetString(args, "pageName")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
|
|
_, err = gitea.DoJSON(ctx, "DELETE", fmt.Sprintf("repos/%s/%s/wiki/page/%s", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(pageName)), nil, nil, nil)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("delete wiki page err: %v", err))
|
|
}
|
|
|
|
return to.TextResult(map[string]string{"message": "Wiki page deleted successfully"})
|
|
}
|