Go's regexp unrolls a bounded repeat into that many copies of its
class: [A-Za-z0-9.-]{0,254} (the session terminal's bundle id) was
89kB of program, kept from start to exit, and Codex's item-id refusal
`.{0,1000}?` 88kB. Measured one by one over magpie's 540 package-level
patterns (1.5MB in all), the nine largest bounded ones were ~440kB.
capre.MustCompile(expr, max) is the pattern with * or +, matching only
a string of max characters at most (rune count, so the (?i) Kelvin
sign and invalid bytes count as the regexp counted them). The nine:
settings' bundle id, library's name and agent id (now name@wsl:distro
cut in two), Anthropic tool names, the trace page's name and find,
access's MCP server, OpenClaw's profile and Codex's provider id. The
item-id refusal is a function: the tail within 1000 characters of a
head.
Run on mote (go1.26.8):
- go test ./internal/capre ./internal/access ./internal/settings
./internal/library ./internal/sessions ./internal/agent ./internal/gui
. : ok; ./internal/gateway -run 'ItemID|Undeclared|ToolName|Refus': ok
- TestCappedMatchesBounded holds each old pattern against its new one
over 14 units x 19 lengths (0..300, at each bound +-1); with the
length check taken out it is red (1586 mismatches).
TestItemIDRefusalAsTheRegexp holds the function against the old
regexp at 998..1002 characters of x, é, 日, \xff, \n; with 1001 it is
red. TestCappedIsSmall: 1304 bytes held, 49680 bounded.
- go vet native, GOOS=linux and GOOS=windows over the touched packages
(gui natively: its Linux build needs cgo); gofmt -l clean.
- Real build, GODEBUG=inittrace=1: magpie's own inits allocate
11.93MB -> 10.61MB.
- Idle on the real-account copy, 180s heap: regexp programs (sampled)
4.54MB -> 3.01MB.
Not tried: the full ./... suite; the gateway package beyond the run
above.
108 lines
3.3 KiB
Go
108 lines
3.3 KiB
Go
package access
|
|
|
|
import (
|
|
"errors"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/capre"
|
|
)
|
|
|
|
// A key's MCP list (Key.MCP, wakaka on Discord) holds it to some of the
|
|
// MCP servers the gateway serves, and some of their tools: the Library's
|
|
// servers (by the hub, /mcp/<name>, /mcp/run/<name>) and magpie's own
|
|
// (web search, image recognition, image generation, Magpie Browser, the
|
|
// plugin tools). An entry is a server's name, every tool of it, or
|
|
// "<server>/<tool>", that one tool; NoMCP alone holds the key to none.
|
|
// An empty list is every server, as keys always were.
|
|
|
|
// NoMCP is the list of a key held to no MCP server at all.
|
|
const NoMCP = "-"
|
|
|
|
// mcpServerRe is a server's name as the Library takes one (library's
|
|
// nameRe), and as magpie names its own servers.
|
|
var mcpServerRe = capre.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_-]*$`, 64)
|
|
|
|
// CleanMCP is a key's MCP list as it is kept: each entry trimmed and
|
|
// checked, told apart in no other way than as written, a tool dropped
|
|
// whose server is listed whole, and NoMCP dropped beside any server.
|
|
func CleanMCP(in []string) ([]string, error) {
|
|
var out []string
|
|
none := false
|
|
for _, e := range in {
|
|
e = strings.TrimSpace(e)
|
|
if e == "" {
|
|
continue
|
|
}
|
|
if e == NoMCP {
|
|
none = true
|
|
continue
|
|
}
|
|
server, tool, hasTool := strings.Cut(e, "/")
|
|
if !mcpServerRe.MatchString(server) || hasTool && (tool == "" || len(tool) > 128 || strings.TrimSpace(tool) != tool) {
|
|
return nil, errors.New("A key's MCP entry is <server> or <server>/<tool>, like github or github/search_code: " + e)
|
|
}
|
|
if !slices.Contains(out, e) {
|
|
out = append(out, e)
|
|
}
|
|
}
|
|
// a server listed whole has every tool already
|
|
out = slices.DeleteFunc(out, func(e string) bool {
|
|
server, _, hasTool := strings.Cut(e, "/")
|
|
return hasTool && slices.Contains(out, server)
|
|
})
|
|
if len(out) > 500 {
|
|
return nil, errors.New("A key can list at most 500 MCP servers and tools")
|
|
}
|
|
if len(out) == 0 && none {
|
|
return []string{NoMCP}, nil
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// mcpServer is whether the list l lets in server, and whether it lets in
|
|
// every tool of it; an empty list lets in everything.
|
|
func mcpServer(l []string, server string) (ok, every bool) {
|
|
if len(l) == 0 {
|
|
return true, true
|
|
}
|
|
for _, e := range l {
|
|
s, _, hasTool := strings.Cut(e, "/")
|
|
if s != server {
|
|
continue
|
|
}
|
|
if !hasTool {
|
|
return true, true
|
|
}
|
|
ok = true
|
|
}
|
|
return ok, false
|
|
}
|
|
|
|
// MCPHeld is whether the key, or the key it was issued under, is held to
|
|
// some MCP servers or tools.
|
|
func (who Identity) MCPHeld() bool {
|
|
return len(who.MCP) > 0 || who.Issuer != nil && len(who.Issuer.MCP) > 0
|
|
}
|
|
|
|
// MCPServer is whether the key may reach the MCP server called server,
|
|
// and whether it may use every tool of it (else MCPAllows says which).
|
|
func (who Identity) MCPServer(server string) (ok, every bool) {
|
|
ok, every = mcpServer(who.MCP, server)
|
|
if who.Issuer != nil {
|
|
pOK, pEvery := mcpServer(who.Issuer.MCP, server)
|
|
ok, every = ok && pOK, every && pEvery
|
|
}
|
|
return ok, ok && every
|
|
}
|
|
|
|
// MCPAllows is whether the key may use the tool of the MCP server called
|
|
// server.
|
|
func (who Identity) MCPAllows(server, tool string) bool {
|
|
allows := func(l []string) bool {
|
|
ok, every := mcpServer(l, server)
|
|
return every || ok && slices.Contains(l, server+"/"+tool)
|
|
}
|
|
return allows(who.MCP) && (who.Issuer == nil || allows(who.Issuer.MCP))
|
|
}
|