mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
Share portable MCP definitions with Hermes through its native YAML configuration while preserving unrelated settings and managed-entry safeguards. Support explicit Hermes homes and native OAuth options without copying credential state, and expose the target consistently in the dashboard, schemas, completions and documentation. Refs #825
69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package mcp
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// Aliases and merges can make changing one entry affect another. Fail closed
|
|
// instead of flattening those relationships when editing an owned extension.
|
|
func plainYAML(node *yaml.Node) error {
|
|
if node.Kind == yaml.AliasNode || node.Tag == "!!merge" {
|
|
return fmt.Errorf("MCP YAML configuration must not use aliases or merges")
|
|
}
|
|
for _, child := range node.Content {
|
|
if err := plainYAML(child); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (n *Native) editYAML(changes map[string]map[string]any) ([]byte, error) {
|
|
// Parse a fresh tree so repeated previews never mutate the original.
|
|
var doc yaml.Node
|
|
if err := parseYAML(n.data, &doc); err != nil {
|
|
return nil, err
|
|
}
|
|
key := nativeKey(n.Target)
|
|
if field(&doc, key) == nil {
|
|
if err := put(&doc, key, map[string]any{}); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
extensions := field(&doc, key)
|
|
for _, name := range sortedKeys(changes) {
|
|
entry := changes[name]
|
|
if entry != nil {
|
|
if err := put(extensions, name, entry); err != nil {
|
|
return nil, err
|
|
}
|
|
continue
|
|
}
|
|
for i := 0; i+1 < len(extensions.Content); i += 2 {
|
|
if extensions.Content[i].Value == name {
|
|
extensions.Content = append(extensions.Content[:i], extensions.Content[i+2:]...)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
// Expand newly populated empty flow mappings, while retaining comments and
|
|
// all unrelated config nodes (including built-in extensions).
|
|
if len(mapping(&doc).Content) > 0 {
|
|
mapping(&doc).Style = 0
|
|
}
|
|
if len(extensions.Content) > 0 {
|
|
extensions.Style = 0
|
|
}
|
|
out, err := utils.MarshalYAML(&doc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := ParseNative(n.Target, out); err != nil {
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|