Granting or taking back a server's tools leaves the rest as it was
CI / test (push) Successful in 10m29s
CI / test (push) Successful in 10m29s
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)
|
||||
|
||||
@@ -165,3 +165,21 @@ func TestTheMCPPageSpeaksTurkishWhenAsked(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAGrantBelowALevelThatNamesOtherToolsSaysSoAndKeepsWhatItHad(t *testing.T) {
|
||||
mux, st, _ := mcpPage(t, mcpAdmin)
|
||||
ctx := context.Background()
|
||||
org, _ := st.CreateOrg(ctx, "Acme")
|
||||
team, _ := st.CreateTeam(ctx, org, "eng")
|
||||
_ = st.PutLimits(ctx, store.Limits{Scope: store.ScopeOrg, ScopeID: org, AllowedTools: []string{"hr.*"}})
|
||||
_ = st.PutMCPServer(ctx, store.MCPServer{Name: "jira", URL: "https://jira/mcp", Enabled: true})
|
||||
|
||||
body := mirrorPost(t, mux, "/admin/mcp/jira/grant", url.Values{"csrf": {"tok"}, "scope": {"team"}, "which": {"Acme/eng"},
|
||||
"tools": {"jira.*"}}).Body.String()
|
||||
if !strings.Contains(body, "But organization Acme names tools and not these") {
|
||||
t.Errorf("the grant should say the organization keeps jira out of reach: %s", body)
|
||||
}
|
||||
if l, _ := st.LimitsFor(ctx, store.ScopeTeam, team); !slices.Equal(l.AllowedTools, []string{"hr.*", "jira.*"}) {
|
||||
t.Errorf("the team's list is %v; it should keep hr's, which it saw from its organization", l.AllowedTools)
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@
|
||||
"mcp.which_hint": "ORG/TEAM, ORG/TEAM/PROJECT, ORG, an e-mail or a key's ID",
|
||||
"mcp.tools": "Tools",
|
||||
"mcp.grant_button": "Grant",
|
||||
"mcp.confirm_revoke": "Revoke %s from %s? It and everything below it then no longer see those tools, unless another level grants them.",
|
||||
"mcp.confirm_revoke": "Revoke %s from %s? It then sees them only if the levels above grant them and it names no tools of its own.",
|
||||
"mcp.revoke": "Revoke from %s",
|
||||
"mcp.change": "Change",
|
||||
"mcp.address": "Address",
|
||||
@@ -78,5 +78,11 @@
|
||||
"mcp.err.header": "Which header carries the credential?",
|
||||
"mcp.err.auth": "The credential is a bearer token, another header or none.",
|
||||
"mcp.err.timeout": "The timeout is whole seconds.",
|
||||
"mcp.err.rate": "The rate is calls a minute."
|
||||
"mcp.err.rate": "The rate is calls a minute.",
|
||||
"mcp.scope.org-member": "each member of the organization",
|
||||
"mcp.scope.team-member": "each member of the team",
|
||||
"mcp.scope.project-member": "each member of the project",
|
||||
"mcp.blocked_above": "But %s names tools and not these: %s's tools stay out of reach here until it grants them too.",
|
||||
"mcp.closed": "Closed to every tool, whatever is granted above:",
|
||||
"mcp.how_grants": "A tool is seen where it is granted and every level above that names tools names it too. A level given its first grant keeps what it saw from above; taking a server's tools away leaves the rest."
|
||||
}
|
||||
@@ -30,7 +30,7 @@
|
||||
"mcp.which_hint": "ORG/TAKIM, ORG/TAKIM/PROJE, ORG, bir e-posta ya da anahtarın kimliği",
|
||||
"mcp.tools": "Araçlar",
|
||||
"mcp.grant_button": "Yetki ver",
|
||||
"mcp.confirm_revoke": "%s yetkisi %s üzerinden geri alınsın mı? Başka bir seviye vermedikçe orası ve altındakiler bu araçları artık görmez.",
|
||||
"mcp.confirm_revoke": "%s yetkisi %s üzerinden geri alınsın mı? Bundan sonra onları yalnızca üst seviyeler veriyorsa ve kendi araç listesi yoksa görür.",
|
||||
"mcp.revoke": "%s üzerinden geri al",
|
||||
"mcp.change": "Değiştir",
|
||||
"mcp.address": "Adres",
|
||||
@@ -77,5 +77,11 @@
|
||||
"mcp.err.header": "Kimlik bilgisini hangi başlık taşıyor?",
|
||||
"mcp.err.auth": "Kimlik bilgisi bir bearer token, başka bir başlık ya da hiçbiri olabilir.",
|
||||
"mcp.err.timeout": "Zaman aşımı tam saniye olmalı.",
|
||||
"mcp.err.rate": "Hız dakikadaki çağrı sayısıdır."
|
||||
"mcp.err.rate": "Hız dakikadaki çağrı sayısıdır.",
|
||||
"mcp.scope.org-member": "organizasyonun her üyesi",
|
||||
"mcp.scope.team-member": "takımın her üyesi",
|
||||
"mcp.scope.project-member": "projenin her üyesi",
|
||||
"mcp.blocked_above": "Ama %s araç listesi tanımlıyor ve bunlar onda yok: o da yetki verene kadar %s araçları burada erişilemez.",
|
||||
"mcp.closed": "Yukarıda ne verilirse verilsin tüm araçlara kapalı:",
|
||||
"mcp.how_grants": "Bir araç, yetki verildiği yerde ve araç listesi tanımlayan her üst seviye de onu tanımlıyorsa görünür. İlk kez yetki verilen bir seviye yukarıdan gördüklerini korur; bir sunucunun yetkisini almak diğerlerini bırakır."
|
||||
}
|
||||
@@ -51,11 +51,11 @@
|
||||
<button type="submit">{{t "mcp.grant_button"}}</button>
|
||||
</form>
|
||||
{{$name := .Name}}
|
||||
{{range .Grants}}
|
||||
{{range .Grants}}{{if .Revocable}}
|
||||
<form method="post" action="/admin/mcp/{{$name}}/revoke" class="inline" data-confirm="{{t "mcp.confirm_revoke" $name .Label}}">
|
||||
<input type="hidden" name="csrf" value="{{$.CSRF}}"><input type="hidden" name="scope" value="{{.Scope}}"><input type="hidden" name="id" value="{{.ID}}">
|
||||
<button type="submit">{{t "mcp.revoke" .Label}}</button></form>
|
||||
{{end}}
|
||||
{{end}}{{end}}
|
||||
|
||||
{{if not .Local}}
|
||||
<h3>{{t "mcp.change"}}</h3>
|
||||
@@ -81,6 +81,8 @@
|
||||
{{end}}
|
||||
</table>
|
||||
{{else}}<p class="sub">{{t "mcp.none"}}</p>{{end}}
|
||||
{{if .Closed}}<p class="note">{{t "mcp.closed"}} {{range $i, $c := .Closed}}{{if $i}}, {{end}}{{t (printf "mcp.scope.%s" $c.Scope)}} {{$c.Label}}{{end}}.</p>{{end}}
|
||||
<p class="sub">{{t "mcp.how_grants"}}</p>
|
||||
|
||||
<h2>{{t "mcp.add"}}</h2>
|
||||
<form method="post" action="/admin/mcp/add">
|
||||
|
||||
Reference in new issue
Block a user