wakaka on Discord (#help, 11-06) asked for gateway keys limited to some MCP servers and tools, as keys already are to models (#882). A key's MCP list (caller-keys.json "mcp") holds entries "server" (every tool of it) or "server/tool"; "-" alone is no MCP server; an empty list is every server, as before. A key issued under another is held to its issuer's list too. Every MCP route checks it: - magpie's own servers (web search, vision, image, Magpie Browser, plugin tools) through serveMCP: initialize/tools/list/tools/call are refused or filtered per server and tool, and /v1/magpie/key-limits names "mcp"; - the Library hub ("magpie"): only the backing servers and tools the key may use are listed, and a call by name or by fallback is refused; - /mcp/<name> and /mcp/run/<name>: the server is refused, tools/list is filtered in JSON and SSE replies, tools/call refused; a reply the filter can't read (an encoded body) is a 502, never passed through. GUI: "MCP servers" in the key row's menu opens the app's own picker (no native select) with All, No MCP server, each server and its tools (GET /api/caller-keys/mcp lists magpie's own servers and the Library's). A key held to some shows a badge saying which, that opens the same list; a free key shows nothing more in its row, on hover either. A hover badge was tried first: it pushed the row past 560px (gateway-caller-keys) and squeezed the masked key under a drag at 420px (gateway-key-arrange). CLI: magpie gateway-key mcp <id> github fs/read_file | none | all. zh, zh-TW, ja, de strings; docs/reference.md and the gateway-routing and library subsystem pages. Not covered: a model's own built-in web search (a vendor tool, not MCP). GET /api/caller-keys/mcp is named in the CLI parity test's notYet (its list is what `magpie gateway-key mcp` takes; TestEveryRouteHasACommandOrIsNamed caught it at the rebased head). Run on mote (mote-run), at the pushed head: - go test ./internal/access ./internal/cli ./internal/gui ./internal/library ./internal/gateway at a9c40186d (this change on2ce44dc28): ok except internal/gui's TestMainWindowCorners/macOS_26_SDK (#3949, the same message on macOS 27). After the rebase ontoa9667daad(no file in common): TestCoreScriptBudget, TestCallerKey*, TestEveryRoute*, TestGatewayKey*, TestKeyMCP*, internal/access and go vet ./internal/... at d36f89224's tree: ok. - gateway-caller-keys chromium en reads wider than 560px 4 of 6 runs alone on this branch and 4 of 6 on origin/maina9667daad: #3797, recorded there. - go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean; darwin, linux (nogui) and windows builds ok; gofmt -l clean. - GUI: gateway-key-mcp.test.cjs in chromium and webkit, widths 1100 and 420, en/zh/zh-TW/ja/de: 20/20. Every gateway-key*, gateway-keys* and i18n* file in both engines: ok except gateway-key-models en (red on origin/mainecc26a339too, #4081/#5233) and i18n-keys "a plugin's words are its own" (red on main, #5332). - The whole GUI suite (both engines, every language) with the hover badge: 91 reds in 25 files. Rerun alone on this branch and on origin/main54dc257f1: only gateway-caller-keys (560px sideways), gateway-key-readonly, models-fold and models-select-all-api were red on the branch alone; the badge moved into the row menu for the first two. After it, every gateway-key*, gateway-caller-keys, gateway-card, gateway-connect-keys, api-key-theme and i18n* file passes in both engines, except i18n-keys "a plugin's words are its own" (red on main, #5332, none of the three strings are this change's) and 1 of 20 models-select-all-api "the click scrolled" (#4389; models-fold is #4071). The rest are red on origin/main too and each has an issue: account-weight #4644, codex-auto-reset #2360, header-centre #3479, images-codex-one #4618, key-replace #4126, menu-input-scroll #3402, mobile-web #3952, model-chip-steady #4388, model-copy-id #4621, models-works-only #4391, nav-timeout #4787, omarchy #4624, page-width-steady #5013, phone-connect-menu #4706/#4707, plugin-move-unserved #4392. Broken by hand, each went red: the serveMCP server gate, call check and list filter, the own-server list filter and key-limits "mcp" (TestKeyMCPOwnServer); hubTools, hubServersOf, the hubToolNamed fallback and hubMCPRun (TestKeyMCPHub); /mcp/run (TestKeyMCPRun); the /mcp/<name> guard and filter (TestKeyMCPProxy); the GUI's "No MCP server" being exclusive (gateway-key-mcp). gateway-key-readonly chromium 420 ja was red with the hover badge (the sibling run, the whole suite and its rerun; green on origin/main) and passes with the badge in the row menu. Not tried: a real Library server through a live gateway, the hub with a real agent, and running on Windows or Linux (vet and build only). Reported-by: wakaka
192 lines
7.1 KiB
Go
192 lines
7.1 KiB
Go
package gui
|
|
|
|
import (
|
|
"cmp"
|
|
"encoding/json"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/budget"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/library"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// keyRow is a gateway key as the Gateway page lists it, with what it has
|
|
// used of its limit (#585): Used of its first, UsedAll of each (#2227),
|
|
// in the order of its limits.
|
|
type keyRow struct {
|
|
access.Key
|
|
Used *budget.Status `json:"used,omitempty"`
|
|
UsedAll []budget.Status `json:"usedAll,omitempty"`
|
|
}
|
|
|
|
// keyModelJSON is a model a gateway key may be held to, as its picker
|
|
// lists it.
|
|
type keyModelJSON struct {
|
|
ID string `json:"id"` // "<provider>/<model>" or "group/<id>", what the key keeps
|
|
Name string `json:"name"`
|
|
Provider string `json:"provider,omitempty"`
|
|
ProviderName string `json:"providerName,omitempty"`
|
|
Group bool `json:"group,omitempty"` // a routing group (Magic_zero on Discord)
|
|
Account bool `json:"account,omitempty"` // a signed-in account the key may be held to (#905)
|
|
Key bool `json:"key,omitempty"` // one of a provider's keys, by its KeyID
|
|
Plan string `json:"plan,omitempty"` // the account's subscription, for the menu's note
|
|
// Decides marks a decision model (Jev, Clef…), which no agent lists:
|
|
// a key held to some models reaches it at /v1/systemone, and another
|
|
// magpie lists it, only when it is picked (Kautism on Discord)
|
|
Decides bool `json:"decides,omitempty"`
|
|
}
|
|
|
|
// keyMCPJSON is an MCP server a gateway key may be held to, with its
|
|
// tools, as its picker lists it.
|
|
type keyMCPJSON struct {
|
|
Name string `json:"name"`
|
|
Title string `json:"title,omitempty"`
|
|
Tools []string `json:"tools"`
|
|
Own bool `json:"own,omitempty"` // one of magpie's own
|
|
}
|
|
|
|
func withLimits(keys []access.Key) []keyRow {
|
|
now := time.Now()
|
|
out := make([]keyRow, 0, len(keys))
|
|
for _, k := range keys {
|
|
row := keyRow{Key: k, UsedAll: budget.All(k, now)}
|
|
if len(row.UsedAll) > 0 {
|
|
row.Used = &row.UsedAll[0]
|
|
}
|
|
out = append(out, row)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func callerKeyRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /api/caller-keys", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
access.MigrateLegacyLANKeyBestEffort()
|
|
keys, err := access.List()
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, map[string]any{"keys": withLimits(keys), "accountNames": provider.AccountNames()})
|
|
})
|
|
// the models a key may be held to (#882), and the accounts and keys
|
|
// (#905): the routing groups, then each provider's models, then its
|
|
// accounts and keys; a group the key names is its with every member
|
|
mux.HandleFunc("GET /api/caller-keys/models", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
out := []keyModelJSON{}
|
|
accounts := []keyModelJSON{}
|
|
for _, e := range provider.Catalog() {
|
|
if e.Group != "" {
|
|
// by the id magpie keeps it by, group/<id>, as a key held to it
|
|
// before names it: a found group's auto/<model> passes for it
|
|
// (gateway's allows), and its name is what the picker shows
|
|
out = append(out, keyModelJSON{ID: provider.GroupPrefix + e.Group, Name: cmp.Or(e.Name, e.Group), Group: true})
|
|
continue
|
|
}
|
|
out = append(out, keyModelJSON{ID: e.Provider.ID + "/" + e.Model, Name: cmp.Or(e.Name, e.Model), Provider: e.Provider.ID, ProviderName: cmp.Or(e.Provider.Name, e.Provider.ID)})
|
|
}
|
|
// the models a routing group listing alone keeps out of the lists,
|
|
// each after its provider's: a key not given the group is listed
|
|
// those it may use (the gateway's keyListed), so the picker offers
|
|
// them to pick one at a time too (luckymanjc on Discord: Codex
|
|
// offered GPT-6-Luna alone)
|
|
for _, s := range provider.GroupStandIns() {
|
|
e := s.Entry
|
|
id := e.Provider.ID + "/" + e.Model
|
|
if slices.ContainsFunc(out, func(m keyModelJSON) bool { return strings.EqualFold(m.ID, id) }) {
|
|
continue
|
|
}
|
|
m := keyModelJSON{ID: id, Name: cmp.Or(e.Name, e.Model), Provider: e.Provider.ID, ProviderName: cmp.Or(e.Provider.Name, e.Provider.ID)}
|
|
at := len(out)
|
|
for i := len(out) - 1; i >= 0; i-- {
|
|
if out[i].Provider == m.Provider {
|
|
at = i + 1
|
|
break
|
|
}
|
|
}
|
|
out = slices.Insert(out, at, m)
|
|
}
|
|
// the decision models the routing groups' pickers offer, each
|
|
// after its provider's chat models: the gateway holds a key to
|
|
// them as to any model (serveSystemOne, and another magpie's list
|
|
// of them), so a key held to some models had no way to be let use
|
|
// one but All models (Kautism on Discord: a key held to two
|
|
// groups, and Clef and Jev listed nowhere)
|
|
for _, e := range provider.OfferedDeciders(nil) {
|
|
id := e.Provider.ID + "/" + e.Model
|
|
if slices.ContainsFunc(out, func(m keyModelJSON) bool { return strings.EqualFold(m.ID, id) }) {
|
|
continue
|
|
}
|
|
m := keyModelJSON{ID: id, Name: cmp.Or(e.Name, e.Model), Provider: e.Provider.ID, ProviderName: cmp.Or(e.Provider.Name, e.Provider.ID), Decides: true}
|
|
at := len(out)
|
|
for i := len(out) - 1; i >= 0; i-- {
|
|
if out[i].Provider == m.Provider {
|
|
at = i + 1
|
|
break
|
|
}
|
|
}
|
|
out = slices.Insert(out, at, m)
|
|
}
|
|
for _, p := range provider.All() {
|
|
if !p.On() {
|
|
continue
|
|
}
|
|
name := cmp.Or(p.Name, p.ID)
|
|
// an account by its stable id, kept through renames, shown by the
|
|
// name the user gave it, else who is signed in; a key by its
|
|
// fingerprint
|
|
for _, a := range p.AccountIDs() {
|
|
accounts = append(accounts, keyModelJSON{ID: p.ID + "/" + a.ID, Name: a.Shown(p), Provider: p.ID, ProviderName: name, Account: !a.Key, Key: a.Key, Plan: a.Plan})
|
|
}
|
|
}
|
|
writeJSON(w, map[string]any{"models": out, "accounts": accounts})
|
|
})
|
|
// the MCP servers and tools a key may be held to (wakaka on Discord):
|
|
// magpie's own, then the Library's, each with its tools by name
|
|
mux.HandleFunc("GET /api/caller-keys/mcp", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
out := []keyMCPJSON{}
|
|
for _, o := range gateway.OwnMCPTools(r.Context()) {
|
|
m := keyMCPJSON{Name: o.Name, Title: o.Title, Tools: o.Tools, Own: true}
|
|
if m.Tools == nil {
|
|
m.Tools = []string{}
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
for _, s := range library.KeyMCPServers() {
|
|
m := keyMCPJSON{Name: s.Name, Title: s.Title, Tools: []string{}}
|
|
for _, t := range s.Tools {
|
|
m.Tools = append(m.Tools, t.Name)
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
writeJSON(w, map[string]any{"servers": out})
|
|
})
|
|
mux.HandleFunc("POST /api/caller-keys/{action}", func(w http.ResponseWriter, r *http.Request) {
|
|
var in access.Change
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
access.MigrateLegacyLANKeyBestEffort()
|
|
secret, err := access.Update(r.PathValue("action"), in)
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
keys, err := access.List()
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
writeJSON(w, map[string]any{"keys": withLimits(keys), "accountNames": provider.AccountNames(), "secret": secret})
|
|
})
|
|
}
|