Files
skillshare/internal/mcp/options.go
T
Willie 578790c416 feat(mcp): write per-Agent native fields from the source (#579)
* feat(mcp): write per-Agent native fields from the source

Some MCP settings have no portable form, such as Codex's
default_tools_approval_mode or Claude Code's per-server timeout. Sync
kept them only while an entry survived, so a recreated entry (for
example after a rename) lost them, and every machine needed them added
by hand again.

A server's `options` now holds these fields per Agent, and sync writes
them as given. The field ledger that piOptions used is extended to
every Agent, so sync removes an option it wrote once the source drops
it, and reports a conflict when the Agent changed it since.

Options are refused when they could break the entry or leak a secret:
fields Skillshare writes (including portable names some Agents rename),
values a native format cannot write back (null, arrays of tables),
options.pi (piOptions covers Pi), disabled entries, and credentials as
plain text, since the Agent's file may be committed.

The dashboard has no editor for options yet but keeps them on save.

Refs: #578

* fix(mcp): close gaps in per-Agent options found in review

- A credential option passed if it merely contained "${", so text around
  a reference, or another Agent's syntax, was written out as plain text.
  It must now be a whole-value reference in the Agent's own syntax, and
  Codex and Antigravity options, which read no references, cannot hold one.
- The Kilo Code project check looked only at portable env references, so
  an {env:VAR} in options reached a project file Kilo refuses to load.
- A replace resolution left the dropped-option conflict in place, so the
  plan stayed blocked; replace now lets the source win and prunes those
  fields.
- Adopting an entry that already matched skipped writing its options until
  a later sync; the takeover now writes them.

* fix(mcp): refuse numeric credentials and constrain option values in the schema

- YAML reads an unquoted secret such as clientSecret: 123456 as a number,
  which the credential check skipped, so sync wrote it out as plain text.
  A mapping under a sensitive name such as credentials also hid its
  plainly named fields. Sensitivity now carries down through mappings and
  lists, and a number under a sensitive name is refused.
- The schema allowed any value inside an Agent's options, so editors
  accepted null or nested lists that LoadSource then rejects. A recursive
  optionValue definition now matches the runtime rules.

* docs(mcp): name every Agent whose options cannot hold a credential

The docs named only Codex and Antigravity, but Claude Desktop, Goose,
Junie, LM Studio and Warp read no environment references there either,
so a source such as options.goose.apiKey: "${GOOSE_KEY}" looked valid
while LoadSource refused it.
2026-10-17 23:30:04 +08:00

129 lines
4.3 KiB
Go

package mcp
import (
"fmt"
"slices"
"strings"
)
var portableFields = []string{"command", "args", "env", "url", "headers", "type", "transport"}
func (s Server) validateOptions(name string) error {
if len(s.Options) == 0 {
return nil
}
if s.Disabled {
return fmt.Errorf("MCP %s: options cannot be set on a disabled entry; it only switches the server off", name)
}
for _, agent := range sortedKeys(s.Options) {
switch {
case agent == "pi":
return fmt.Errorf("MCP %s: options.pi is not used; set Pi's fields in piOptions", name)
case !validTarget(agent):
return fmt.Errorf("MCP %s: options.%s is not an Agent; use an Agent name such as codex or claude", name, agent)
}
for _, key := range sortedKeys(s.Options[agent]) {
// Some Agents rename the portable fields, such as OpenCode's environment, so both names are refused.
if slices.Contains(additionalManagedFields(agent), key) || slices.Contains(portableFields, key) {
return fmt.Errorf("MCP %s: options.%s cannot set %s; Skillshare writes that field from the server's own settings", name, agent, key)
}
if !optionValue(s.Options[agent][key], false) {
return fmt.Errorf("MCP %s: options.%s.%s must be a string, number, boolean, list of those, or a mapping of them", name, agent, key)
}
if field := literalSecret(agent, key, s.Options[agent][key], false); field != "" {
if prefix := envRefPrefix(agent); prefix != "" {
return fmt.Errorf("MCP %s: options.%s.%s holds a credential as plain text; make the whole value an environment reference, %sVAR}", name, agent, field, prefix)
}
return fmt.Errorf("MCP %s: options.%s.%s holds a credential, and %s cannot read an environment reference there; keep it out of options", name, agent, field, agent)
}
}
}
return nil
}
// optionValue accepts what every native format writes back unchanged: TOML has no null,
// and Skillshare's TOML writer refuses an array of tables.
func optionValue(value any, inList bool) bool {
switch v := value.(type) {
case string, bool, int, int64, uint64, float64:
return true
case []any:
for _, item := range v {
if inList || !optionValue(item, true) {
return false
}
}
return !inList
case map[string]any:
for _, item := range v {
if !optionValue(item, false) {
return false
}
}
return !inList
}
return false
}
// literalSecret names the first field holding a credential as plain text, as import judges
// one. The Agent's file may be committed, so only a whole environment reference in the
// Agent's own syntax may stand there; text around one would still be written out. Everything
// under a sensitive name is sensitive, and a number there is a credential written as plain text.
func literalSecret(agent, key string, value any, sensitive bool) string {
sensitive = sensitive || sensitiveKey.MatchString(key)
switch v := value.(type) {
case string:
prefix := envRefPrefix(agent)
ref := prefix != "" && strings.HasPrefix(v, prefix) && strings.HasSuffix(v, "}") && envName.MatchString(v[len(prefix):len(v)-1])
if v != "" && (sensitive || hasURLPassword(v)) && !ref {
return key
}
case int, int64, uint64, float64:
if sensitive {
return key
}
case []any:
for _, item := range v {
if field := literalSecret(agent, key, item, sensitive); field != "" {
return field
}
}
case map[string]any:
for _, name := range sortedKeys(v) {
if field := literalSecret(agent, name, v[name], sensitive); field != "" {
return key + "." + field
}
}
}
return ""
}
// hasReference reports an interpolation anywhere in an Agent's options.
func hasReference(value any) bool {
switch v := value.(type) {
case string:
return hasInterpolation(v)
case []any:
return slices.ContainsFunc(v, hasReference)
case map[string]any:
for _, item := range v {
if hasReference(item) {
return true
}
}
}
return false
}
// withOptions adds the Agent's options to a rendered entry. A field Render already wrote,
// such as Copilot's tools, belongs to the server's own settings.
func withOptions(target string, s Server, out map[string]any) (map[string]any, error) {
for _, key := range sortedKeys(s.Options[target]) {
if _, set := out[key]; set {
return nil, fmt.Errorf("options.%s cannot set %s; Skillshare writes that field from the server's own settings", target, key)
}
out[key] = s.Options[target][key]
}
return out, nil
}