Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions pkgs/mcpx/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ type Extras struct {
// Supervised runs a stdio server under a per-server supervisor
// process (#1385, Phase 1 of #1384): the supervisor owns the child's
// pipes for the child's whole life and daemons attach to it, so the
// child survives a daemon restart. Default false - the direct spawn
// path is unchanged.
Supervised bool `json:"supervised,omitempty"`
// child survives a daemon restart. Since Phase 3 (#9963) supervised
// is the default for stdio servers in any config loaded from a
// file: parse materializes true when the key is absent, an explicit
// false opts the server back into the legacy direct-spawn path,
// and an explicit true is the Phase 1 opt-in, unchanged. A nil here
// means no file load has defaulted it (a programmatically built
// config) and reads as not supervised. Read it through
// SupervisedEnabled, never directly.
Supervised *bool `json:"supervised,omitempty"`
// Logging overrides the global settings for records about this server, so
// a single misbehaving server can be made verbose without drowning in the
// rest. Nested rather than flat, because "logging.level" reads as one
Expand Down Expand Up @@ -198,6 +204,16 @@ type Extras struct {
Preconditions map[string]Precondition `json:"preconditions,omitempty"`
}

// SupervisedEnabled reports whether this server's resolved config
// runs its stdio child under a supervisor: the explicit value, with
// the Phase 3 default (true) already materialized by parse for
// file-loaded configs (#9963). Nil Extras or a nil field (a config
// built in code, never parsed from a file) reads as false, the
// Phase 1 behaviour.
func (e *Extras) SupervisedEnabled() bool {
return e != nil && e.Supervised != nil && *e.Supervised
}

// ToolView is one declared view of a tool (#1933 slices 1-3). A view
// is either a renamed view of an aliased server's tool (From), a
// script-backed tool that runs a declared local command (Command), or
Expand Down Expand Up @@ -864,6 +880,20 @@ func (c *Config) Resolve(name string) (*Resolved, error) {
merged.Name = name
merged.AliasOf = s.AliasOf
merged.Mcpx = s.Mcpx
// Only the supervised bit is inherited from the target: an
// alias is a view of the target's process, so how that
// process is spawned follows the target, but the rest of the
// alias's extras are its own view (hiding is per view, and
// the target's excludeTools must not leak into the alias).
if target.Mcpx != nil && target.Mcpx.Supervised != nil {
if merged.Mcpx == nil {
merged.Mcpx = &Extras{Supervised: target.Mcpx.Supervised}
} else if merged.Mcpx.Supervised == nil {
cp := *merged.Mcpx
cp.Supervised = target.Mcpx.Supervised
merged.Mcpx = &cp
}
}
s = &merged
}
// A per-server protocol overrides upstream.protocol, so it is read
Expand Down Expand Up @@ -1485,6 +1515,9 @@ func mergeExtras(near, far Extras) Extras {
// survive into the nearer config, and a key both files use
// composes with allOf rather than being replaced.
near.ArgumentRules = mergeArgumentRules(near.ArgumentRules, far.ArgumentRules)
if near.Supervised == nil {
near.Supervised = far.Supervised
}
return near
}

Expand Down Expand Up @@ -1555,6 +1588,20 @@ func parse(b []byte) (*Config, error) {
if s.URL != "" && s.Transport == "" {
s.Transport = "http"
}
// Phase 3 of #1384 (#9963): supervised stdio is the default.
// Materialize it here, at file load, so every consumer of a
// parsed config (daemon, CLI, settings views) sees the same
// resolved value and an explicit "supervised": false remains
// the one opt-out into the legacy direct-spawn path.
if s.Command != "" {
if s.Mcpx == nil {
s.Mcpx = &Extras{}
}
if s.Mcpx.Supervised == nil {
tr := true
s.Mcpx.Supervised = &tr
}
}
}
return c, nil
}
Expand Down
45 changes: 45 additions & 0 deletions pkgs/mcpx/internal/config/supervised_default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package config_test

import (
"testing"

"github.com/developing-today/code/pkgs/mcpx/internal/config"
)

// Phase 3 of #1384 (#9963): a config loaded from a file defaults its
// stdio servers to supervised; "supervised": false is the opt-out
// into the legacy direct-spawn path, and HTTP servers are untouched.
func TestSupervisedDefaultsOnForStdioFromFile(t *testing.T) {
dir := t.TempDir()
p := write(t, dir, "c.json", `{
"mcpServers": {
"plain": { "command": "x" },
"optout": { "command": "x", "mcpx": { "supervised": false } },
"optin": { "command": "x", "mcpx": { "supervised": true } },
"web": { "url": "https://example.com/mcp" }
}
}`)
c, err := config.Load(p)
if err != nil {
t.Fatal(err)
}
if !c.MCPServers["plain"].Mcpx.SupervisedEnabled() {
t.Fatal("a stdio server with no supervised key should default to supervised after a file load")
}
if c.MCPServers["optout"].Mcpx.SupervisedEnabled() {
t.Fatal("supervised:false should opt out of the default")
}
if !c.MCPServers["optin"].Mcpx.SupervisedEnabled() {
t.Fatal("supervised:true should stay supervised")
}
if c.MCPServers["web"].Mcpx != nil && c.MCPServers["web"].Mcpx.SupervisedEnabled() {
t.Fatal("an HTTP server should not be defaulted to supervised")
}
r, err := c.Resolve("plain")
if err != nil {
t.Fatal(err)
}
if !r.Mcpx.SupervisedEnabled() {
t.Fatal("the resolved config should carry the supervised default to the pool")
}
}
1 change: 1 addition & 0 deletions pkgs/mcpx/internal/daemon/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ import (
// removed when the run ends; a process still executing from one of them
// fails the run.
func TestMain(m *testing.M) {
testsupport.SuperviseReExec()
os.Exit(testsupport.MainWithSweep(m))
}
19 changes: 19 additions & 0 deletions pkgs/mcpx/internal/daemon/registry_handoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/developing-today/code/pkgs/mcpx/internal/defaults"
"github.com/developing-today/code/pkgs/mcpx/internal/mcpclient"
"github.com/developing-today/code/pkgs/mcpx/internal/pool"
)

Expand Down Expand Up @@ -213,3 +214,21 @@ func (r *Registry) AdoptSupervised(ctx context.Context) {
p.AdoptRunningSupervised(ctx)
}
}

// ReapOrphanedSupervisors runs the reap half of the #9963
// adopt-or-reap scan at daemon startup: a supervisor socket no
// configured supervised server maps to belongs to a server that
// left the config (or opted out), so its supervisor and child are
// terminated and the reap logged; a socket whose supervisor refuses
// (a daemon attached, a protocol-version mismatch) and stale socket
// files are logged and left or removed accordingly. See
// mcpclient.ScanSupervisorSockets.
func (r *Registry) ReapOrphanedSupervisors() {
var configured []string
for _, name := range r.Names() {
if v, ok := r.View(name); ok && v.Stdio() && v.Mcpx.SupervisedEnabled() {
configured = append(configured, name)
}
}
mcpclient.ScanSupervisorSockets(mcpclient.StateDir(), configured, r.logf)
}
3 changes: 3 additions & 0 deletions pkgs/mcpx/internal/daemon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ func (s *Server) Listen(tcpPort int) error {
// Adopt supervised instances a previous daemon left running
// (#9962): their supervisors outlive the daemon by design.
s.reg.AdoptSupervised(context.Background())
// Reap the supervisors no configured server claims anymore
// (#9963, the other half of the adopt-or-reap scan).
s.reg.ReapOrphanedSupervisors()
return s.publish()
}

Expand Down
Loading