Granting or taking back a server's tools leaves the rest as it was
A tool is seen where every level above that names tools names it (store Effective.AllowsTool). The console and /admin/mcp wrote a level's list as given, so a team that saw its organization's hr.* through having no list was given jira.* and then saw neither; and revoking a level's last server left [] in the store, closed to every tool, shown nowhere. store.GrantTools and RevokeTools now do both screens' work: a level's first list starts from the nearest list above it; taking a server away leaves the others, and a level left naming nothing goes back to no list, or stays closed when a level above names tools, so it opens to nothing it was kept from. GrantTools says which levels above still keep the granted tools out of reach, and both screens say so. The page shows closed levels and member scopes by name, and offers no revoke for a * grant it would not change. The console calls the store instead of keys limits with a list built from what it had loaded, which overwrote changes made since. Found in review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7a04248148
commit
44451bacd9
11 files changed
+422
-71
No files matched your search
+62
-38
@@ -21,7 +21,8 @@ import (
|
||||
|
||||
func init() {
|
||||
// Named from the grant's scope in the template.
|
||||
registerKeys("mcp.scope.org", "mcp.scope.team", "mcp.scope.project", "mcp.scope.user", "mcp.scope.key")
|
||||
registerKeys("mcp.scope.org", "mcp.scope.team", "mcp.scope.project", "mcp.scope.user", "mcp.scope.key",
|
||||
"mcp.scope.org-member", "mcp.scope.team-member", "mcp.scope.project-member")
|
||||
}
|
||||
|
||||
// MCPAdmin is where the servers, their sealed credentials and the tree's
|
||||
@@ -31,6 +32,8 @@ type MCPAdmin interface {
|
||||
AllLimits(ctx context.Context) (map[string]store.Limits, error)
|
||||
LimitsFor(ctx context.Context, scope store.Scope, id string) (store.Limits, error)
|
||||
PutLimits(ctx context.Context, l store.Limits) error
|
||||
GrantTools(ctx context.Context, scope store.Scope, id, server string, tools []string) (store.ToolGrant, error)
|
||||
RevokeTools(ctx context.Context, scope store.Scope, id, server string) error
|
||||
Tree(ctx context.Context) ([]store.TreeOrg, error)
|
||||
Users(ctx context.Context, state store.UserState) ([]store.User, error)
|
||||
}
|
||||
@@ -46,6 +49,9 @@ type mcpPageData struct {
|
||||
// Tools are what a test found, for the server Tested.
|
||||
Tested string
|
||||
Tools []string
|
||||
// Closed are the levels that name no tool at all, and so see none
|
||||
// whatever is granted above them.
|
||||
Closed []mcpGrantRow
|
||||
Notice string
|
||||
Error string
|
||||
}
|
||||
@@ -66,6 +72,9 @@ type mcpRow struct {
|
||||
type mcpGrantRow struct {
|
||||
Scope, ID, Label string
|
||||
Tools []string
|
||||
// Revocable is false for a grant of every tool, "*", which taking this
|
||||
// server's away would not change: it is changed with the level's limits.
|
||||
Revocable bool
|
||||
}
|
||||
|
||||
// RegisterAdminMCP mounts /admin/mcp, for administrators only: a server's
|
||||
@@ -81,6 +90,11 @@ func (p *AccountPages) RegisterAdminMCP(mux *http.ServeMux, s MCPAdmin, local fu
|
||||
d.Error = l.Err(err)
|
||||
}
|
||||
grants := mcpGrants(ctx, s)
|
||||
for _, g := range grants {
|
||||
if len(g.Tools) == 0 {
|
||||
d.Closed = append(d.Closed, g)
|
||||
}
|
||||
}
|
||||
for _, m := range list {
|
||||
row := mcpRow{MCPServerView: adminapi.MCPServerView{Name: m.Name, URL: m.URL, Description: m.Description,
|
||||
Enabled: m.Enabled, TimeoutSeconds: int(m.Timeout.Seconds()), RatePerMinute: m.RatePerMinute},
|
||||
@@ -95,13 +109,16 @@ func (p *AccountPages) RegisterAdminMCP(mux *http.ServeMux, s MCPAdmin, local fu
|
||||
slices.Sort(row.Plain)
|
||||
for _, g := range grants {
|
||||
var mine []string
|
||||
revocable := false
|
||||
for _, t := range g.Tools {
|
||||
if strings.HasPrefix(t, m.Name+".") || t == "*" {
|
||||
mine = append(mine, t)
|
||||
revocable = revocable || t != "*"
|
||||
}
|
||||
}
|
||||
if len(mine) > 0 {
|
||||
row.Grants = append(row.Grants, mcpGrantRow{Scope: g.Scope, ID: g.ID, Label: g.Label, Tools: mine})
|
||||
row.Grants = append(row.Grants, mcpGrantRow{Scope: g.Scope, ID: g.ID, Label: g.Label, Tools: mine,
|
||||
Revocable: revocable})
|
||||
}
|
||||
}
|
||||
d.Servers = append(d.Servers, row)
|
||||
@@ -254,16 +271,25 @@ func (p *AccountPages) RegisterAdminMCP(mux *http.ServeMux, s MCPAdmin, local fu
|
||||
if err == nil {
|
||||
id, label, err = mcpResolve(r.Context(), s, l, scope, which)
|
||||
}
|
||||
notice := l.T("mcp.granted", name, label)
|
||||
if err == nil {
|
||||
err = mcpSetTools(r.Context(), s, name, scope, id, tools)
|
||||
var g store.ToolGrant
|
||||
if g, err = s.GrantTools(r.Context(), scope, id, name, tools); err == nil && len(g.BlockedBy) > 0 {
|
||||
names := mcpNames(r.Context(), s)
|
||||
var by []string
|
||||
for _, o := range g.BlockedBy {
|
||||
by = append(by, l.T("mcp.scope."+string(o.Scope))+" "+or(names[o.CounterKey()], o.ID))
|
||||
}
|
||||
notice += " " + l.T("mcp.blocked_above", strings.Join(by, ", "), name)
|
||||
}
|
||||
}
|
||||
done(w, r, admin, "mcp grants "+name, l.T("mcp.granted", name, label), err)
|
||||
done(w, r, admin, "mcp grants "+name, notice, err)
|
||||
}))
|
||||
|
||||
mux.HandleFunc("POST /admin/mcp/{name}/revoke", adminsOnly(func(w http.ResponseWriter, r *http.Request, admin store.User) {
|
||||
l, name := langOf(r), r.PathValue("name")
|
||||
scope, id := store.Scope(r.FormValue("scope")), r.FormValue("id")
|
||||
err := mcpSetTools(r.Context(), s, name, scope, id, nil)
|
||||
err := s.RevokeTools(r.Context(), scope, id, name)
|
||||
done(w, r, admin, "mcp grants "+name, l.T("mcp.revoked", name), err)
|
||||
}))
|
||||
}
|
||||
@@ -275,26 +301,10 @@ func mcpGrants(ctx context.Context, s MCPAdmin) []mcpGrantRow {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
names := map[string]string{}
|
||||
if tree, err := s.Tree(ctx); err == nil {
|
||||
for _, o := range tree {
|
||||
names[store.LimitOwner{Scope: store.ScopeOrg, ID: o.ID}.CounterKey()] = o.Name
|
||||
for _, t := range o.Teams {
|
||||
names[store.LimitOwner{Scope: store.ScopeTeam, ID: t.ID}.CounterKey()] = o.Name + "/" + t.Name
|
||||
for _, pr := range t.Projects {
|
||||
names[store.LimitOwner{Scope: store.ScopeProject, ID: pr.ID}.CounterKey()] = o.Name + "/" + t.Name + "/" + pr.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if users, err := s.Users(ctx, ""); err == nil {
|
||||
for _, u := range users {
|
||||
names[store.LimitOwner{Scope: store.ScopeUser, ID: u.ID}.CounterKey()] = u.Email
|
||||
}
|
||||
}
|
||||
names := mcpNames(ctx, s)
|
||||
var out []mcpGrantRow
|
||||
for key, lim := range limits {
|
||||
if len(lim.AllowedTools) == 0 {
|
||||
if lim.AllowedTools == nil {
|
||||
continue
|
||||
}
|
||||
label := names[key]
|
||||
@@ -307,6 +317,35 @@ func mcpGrants(ctx context.Context, s MCPAdmin) []mcpGrantRow {
|
||||
return out
|
||||
}
|
||||
|
||||
// mcpNames are the tree's levels and the people by what they are called,
|
||||
// keyed as limits are; a member scope is named as the level it is set on.
|
||||
func mcpNames(ctx context.Context, s MCPAdmin) map[string]string {
|
||||
names := map[string]string{}
|
||||
name := func(scope store.Scope, id, label string) {
|
||||
names[store.LimitOwner{Scope: scope, ID: id}.CounterKey()] = label
|
||||
if m := store.MemberScope(scope); m != "" {
|
||||
names[store.LimitOwner{Scope: m, ID: id}.CounterKey()] = label
|
||||
}
|
||||
}
|
||||
if tree, err := s.Tree(ctx); err == nil {
|
||||
for _, o := range tree {
|
||||
name(store.ScopeOrg, o.ID, o.Name)
|
||||
for _, t := range o.Teams {
|
||||
name(store.ScopeTeam, t.ID, o.Name+"/"+t.Name)
|
||||
for _, pr := range t.Projects {
|
||||
name(store.ScopeProject, pr.ID, o.Name+"/"+t.Name+"/"+pr.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if users, err := s.Users(ctx, ""); err == nil {
|
||||
for _, u := range users {
|
||||
name(store.ScopeUser, u.ID, u.Email)
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// mcpResolve is the ID of the level a form names: a path in the tree, a
|
||||
// person's e-mail, or a key's ID; and how to say it.
|
||||
func mcpResolve(ctx context.Context, s MCPAdmin, l *Lang, scope store.Scope, which string) (string, string, error) {
|
||||
@@ -369,21 +408,6 @@ func mcpCheckTools(l *Lang, server string, tools []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// mcpSetTools makes a level's grants of a server's tools these, leaving its
|
||||
// other grants and its other limits as they are; none revokes them.
|
||||
func mcpSetTools(ctx context.Context, s MCPAdmin, server string, scope store.Scope, id string, tools []string) error {
|
||||
lim, err := s.LimitsFor(ctx, scope, id)
|
||||
switch {
|
||||
case errors.Is(err, store.ErrNotFound):
|
||||
lim = store.Limits{Scope: scope, ScopeID: id}
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
keep := slices.DeleteFunc(slices.Clone(lim.AllowedTools), func(t string) bool { return strings.HasPrefix(t, server+".") })
|
||||
lim.AllowedTools = append(keep, tools...)
|
||||
return s.PutLimits(ctx, lim)
|
||||
}
|
||||
|
||||
// mcpSecrets turns the form's credential into what the server is sent.
|
||||
func mcpSecrets(l *Lang, kind, header, value string) (map[string]string, error) {
|
||||
header, value = strings.TrimSpace(header), strings.TrimSpace(value)
|
||||
|
||||
Reference in new issue
Block a user