Skip to content

Commit e572c6e

Browse files
committed
fix(core): start MCPs registered after activation
1 parent cf89e3c commit e572c6e

2 files changed

Lines changed: 56 additions & 11 deletions

File tree

‎packages/core/src/mcp/index.ts‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,21 @@ export const layer = (options?: Options) =>
667667
yield* bus.publish(McpEvent.StatusChanged, { server: name })
668668
})
669669

670+
// Called under the reconcile lock, whether activation precedes or follows metadata registration.
671+
const startPending = () => {
672+
for (const [name, entry] of entries) {
673+
if (entry.status.status !== "pending") continue
674+
entry.startup.closeUnsafe()
675+
fork(
676+
Effect.suspend(() =>
677+
entries.get(name) === entry && entry.status.status === "pending"
678+
? startServer(name, entry)
679+
: Effect.void,
680+
).pipe(locks.withLock(name)),
681+
)
682+
}
683+
}
684+
670685
let applied: Map<ServerName, Mcp.ServerConfig> | undefined
671686
const overrides = new Map<ServerName, Mcp.ServerConfig | false>()
672687
const reconcileLock = Semaphore.makeUnsafe(1)
@@ -696,6 +711,7 @@ export const layer = (options?: Options) =>
696711
continue
697712
}
698713
}
714+
if (active) startPending()
699715
return
700716
}
701717

@@ -720,17 +736,7 @@ export const layer = (options?: Options) =>
720736
Effect.gen(function* () {
721737
if (active) return
722738
active = true
723-
for (const [name, entry] of entries) {
724-
if (entry.status.status !== "pending") continue
725-
entry.startup.closeUnsafe()
726-
fork(
727-
Effect.suspend(() =>
728-
entries.get(name) === entry && entry.status.status === "pending"
729-
? startServer(name, entry)
730-
: Effect.void,
731-
).pipe(locks.withLock(name)),
732-
)
733-
}
739+
startPending()
734740
}),
735741
),
736742
).pipe(Effect.uninterruptible)

‎packages/core/test/mcp.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ function resourceMcpLayer(
280280
subscribe?: Bus.Interface["subscribe"]
281281
environment?: Layer.Layer<Environment.Service>
282282
published?: string[]
283+
beforeRegistration?: (service: Mcp.Interface) => Effect.Effect<void>
283284
},
284285
) {
285286
const directory = AbsolutePath.make(import.meta.dir)
@@ -288,6 +289,7 @@ function resourceMcpLayer(
288289
Effect.gen(function* () {
289290
const bus = yield* Bus.Service
290291
const mcp = yield* Mcp.Service
292+
if (overrides?.beforeRegistration) yield* overrides.beforeRegistration(mcp)
291293
yield* State.batch(
292294
Effect.gen(function* () {
293295
yield* ConfigMcpPlugin.register(bus.subscribe())
@@ -587,6 +589,43 @@ testEffect(Layer.empty).effect("does not spawn MCP for an unused location or its
587589
}),
588590
)
589591

592+
testEffect(Layer.empty).effect("starts first MCP registrations after activation on empty config", () =>
593+
Effect.gen(function* () {
594+
const counts = { spawned: 0, closed: 0 }
595+
yield* Effect.gen(function* () {
596+
const service = yield* Mcp.Service
597+
yield* advance(() => counts.spawned === 1)
598+
yield* Effect.all(Array.from({ length: 12 }, () => service.start()), { concurrency: "unbounded" })
599+
expect((yield* service.servers())[0]?.status.status).toBe("connected")
600+
expect(counts.spawned).toBe(1)
601+
yield* TestClock.adjust("61 seconds")
602+
yield* advance(() => counts.closed === 1)
603+
expect((yield* service.servers())[0]?.status.status).toBe("idle")
604+
}).pipe(
605+
Effect.provide(
606+
resourceMcpLayer(
607+
new ConfigMCP.Local({
608+
type: "local",
609+
command: [process.execPath, path.join(import.meta.dir, "fixture/mcp-prompts.ts")],
610+
}),
611+
undefined,
612+
undefined,
613+
{
614+
environment: countedEnvironment(counts),
615+
beforeRegistration: (service) =>
616+
Effect.gen(function* () {
617+
expect(yield* service.servers()).toEqual([])
618+
yield* service.start()
619+
expect(counts.spawned).toBe(0)
620+
}),
621+
},
622+
),
623+
),
624+
)
625+
expect(counts).toEqual({ spawned: 1, closed: 1 })
626+
}),
627+
)
628+
590629
testEffect(Layer.empty).effect(
591630
"releases discovery-only MCP scopes, caches metadata, and reacquires for real work",
592631
() =>

0 commit comments

Comments
 (0)