feat: add --tools flag to filter exposed MCP tools (#167)
Adds `-O`/`-tools` CLI flag and `GITEA_TOOLS` environment variable accepting a comma-separated list of tool names. When set, only the listed tools are exposed to MCP clients, which lets AI agents trim their tool context. Composes with `--read-only`. Unknown names are logged at startup so typos surface instead of failing silently. Co-Authored-By: silverwind <me@silverwind.io> Co-Authored-By: Claude (Opus 4.7) <noreply@anthropic.com>
This commit is contained in:
committed by
silverwind
parent
cd82f6f207
commit
bcefbaa9c1
+16
@@ -17,6 +17,7 @@ var (
|
||||
host string
|
||||
port int
|
||||
token string
|
||||
tools string
|
||||
version bool
|
||||
)
|
||||
|
||||
@@ -31,6 +32,9 @@ func init() {
|
||||
flag.StringVar(&token, "token", "", "")
|
||||
flag.BoolVar(&flagPkg.ReadOnly, "r", false, "")
|
||||
flag.BoolVar(&flagPkg.ReadOnly, "read-only", false, "")
|
||||
defaultTools := os.Getenv("GITEA_TOOLS")
|
||||
flag.StringVar(&tools, "O", defaultTools, "")
|
||||
flag.StringVar(&tools, "tools", defaultTools, "")
|
||||
flag.BoolVar(&flagPkg.Debug, "d", false, "")
|
||||
flag.BoolVar(&flagPkg.Debug, "debug", false, "")
|
||||
flag.BoolVar(&flagPkg.Insecure, "k", false, "")
|
||||
@@ -48,6 +52,7 @@ func init() {
|
||||
fmt.Fprintf(w, " -p, -port <number>\tHTTP server port (default: 8080)\n")
|
||||
fmt.Fprintf(w, " -T, -token <token>\tPersonal access token\n")
|
||||
fmt.Fprintf(w, " -r, -read-only\tExpose only read-only tools\n")
|
||||
fmt.Fprintf(w, " -O, -tools <names>\tComma-separated list of tool names to expose\n")
|
||||
fmt.Fprintf(w, " -d, -debug\tEnable debug mode\n")
|
||||
fmt.Fprintf(w, " -k, -insecure\tIgnore TLS certificate errors\n")
|
||||
fmt.Fprintf(w, " -v, -version\tPrint version and exit\n")
|
||||
@@ -59,6 +64,7 @@ func init() {
|
||||
fmt.Fprintf(w, " GITEA_HOST\tOverride Gitea host URL\n")
|
||||
fmt.Fprintf(w, " GITEA_INSECURE\tSet to 'true' to ignore TLS errors\n")
|
||||
fmt.Fprintf(w, " GITEA_READONLY\tSet to 'true' for read-only mode\n")
|
||||
fmt.Fprintf(w, " GITEA_TOOLS\tComma-separated list of tool names to expose\n")
|
||||
fmt.Fprintf(w, " MCP_MODE\tOverride transport mode\n")
|
||||
w.Flush()
|
||||
}
|
||||
@@ -95,6 +101,16 @@ func init() {
|
||||
flagPkg.ReadOnly = true
|
||||
}
|
||||
|
||||
allowed := map[string]struct{}{}
|
||||
for t := range strings.SplitSeq(tools, ",") {
|
||||
if t = strings.TrimSpace(t); t != "" {
|
||||
allowed[t] = struct{}{}
|
||||
}
|
||||
}
|
||||
if len(allowed) > 0 {
|
||||
flagPkg.AllowedTools = allowed
|
||||
}
|
||||
|
||||
if os.Getenv("GITEA_DEBUG") == "true" {
|
||||
flagPkg.Debug = true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user