fix(mcp): rebuild the catalog when discovery changes during a build

Discovery and the package loads read the tree separately. A nested skill
created in between was published only as a file of its parent, with no
entry of its own. Discover again after the loads and rebuild, up to three
times, until both agree.
This commit is contained in:
Willie
2026-10-06 17:13:44 +08:00
parent 69f7dfa89b
commit a3ddb88f99
2 changed files with 45 additions and 0 deletions
+28
View File
@@ -24,6 +24,9 @@ const extensionID = "io.modelcontextprotocol/skills"
var errNotFound = errors.New("not found")
// afterDiscover runs between discovery and the loads; tests change the tree there.
var afterDiscover = func() {}
// File is one manifest entry of a skill.
type File struct {
URI string `json:"uri"`
@@ -75,6 +78,31 @@ func (b *Builder) Build() (*Catalog, error) {
if err != nil {
return nil, err
}
afterDiscover()
// Discovery and the loads read the tree separately. A skill added, removed or
// toggled in between would leave a parent publishing the files of a skill the
// catalog does not list, so build again until discovery agrees with the loads.
for range 3 {
c, err := b.build(all)
if err != nil {
return nil, err
}
again, err := ssync.DiscoverSourceSkillsAll(b.Source, b.Walk)
if err != nil {
return nil, err
}
if slices.EqualFunc(all, again, func(x, y ssync.DiscoveredSkill) bool {
return x.RelPath == y.RelPath && x.Disabled == y.Disabled
}) {
return c, nil
}
all = again
}
return nil, errors.New("the skills source kept changing while it was read; list the skills again")
}
func (b *Builder) build(all []ssync.DiscoveredSkill) (*Catalog, error) {
var err error
var selected []ssync.DiscoveredSkill
if b.Skills != nil {
if selected, err = ssync.SelectTargetSkills(all, b.Target, *b.Skills); err != nil {
+17
View File
@@ -138,6 +138,23 @@ func TestBuild_ServesParentAndNestedSkillTogether(t *testing.T) {
}
}
// A nested skill created after discovery would otherwise be published only as a
// file of its parent.
func TestBuild_ListsNestedSkillCreatedDuringTheBuild(t *testing.T) {
src := t.TempDir()
writeFile(t, filepath.Join(src, "suite/SKILL.md"), skillMD("suite"))
afterDiscover = func() { writeFile(t, filepath.Join(src, "suite/child/SKILL.md"), skillMD("child")) }
t.Cleanup(func() { afterDiscover = func() {} })
c, err := (&Builder{Source: src}).Build()
if err != nil {
t.Fatal(err)
}
if len(c.Skills) != 2 {
t.Errorf("skills=%v warnings=%v, want suite and suite/child served", c.Skills, c.Skipped)
}
}
func TestBuild_SkipsParentOfInvalidNestedSkill(t *testing.T) {
src := t.TempDir()
writeFile(t, filepath.Join(src, "suite/SKILL.md"), skillMD("suite"))