Skip to content

Commit 5de6a13

Browse files
rekram1-nodeexe.dev user
andauthored
fix(core): cap the MCP startup wait before a model call at two seconds (#62386)
Co-authored-by: exe.dev user <exedev@anton.exe.xyz>
1 parent 9cc63dd commit 5de6a13

2 files changed

Lines changed: 6 additions & 37 deletions

File tree

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ type AttachedEntry = {
117117
tools: ReadonlyArray<Tool>
118118
}
119119

120-
// How long a model call may wait for a server that is still connecting and configures no `timeout.startup`,
121-
// counted from when it began. A server with a startup timeout gets that window instead.
120+
// How long a model call may wait for a server that is still connecting, counted from when it began.
122121
const STARTUP_GRACE_MS = 2_000
123122

124123
// Bounds re-listing the tools of an attached connection that takes over the top of its stack.
@@ -161,8 +160,7 @@ export interface Interface extends State.Transformable<Editor> {
161160
readonly tools: () => Effect.Effect<Tool[]>
162161
/**
163162
* Waits for servers that are still connecting, each until it settles or until its startup grace
164-
* window ends: `timeout.startup` when the server configures one, else `STARTUP_GRACE_MS`. Servers
165-
* past their window never wait.
163+
* window ends. Servers past their window never wait.
166164
*/
167165
readonly awaitStartup: () => Effect.Effect<void>
168166
readonly callTool: (input: {
@@ -804,10 +802,7 @@ export const layer = (options?: Options) =>
804802
yield* Effect.all(
805803
Array.from(entries.values()).flatMap((entry) => {
806804
if (entry.status.status !== "pending" || !entry.attempt) return []
807-
// A configured startup timeout is the window the connection itself is allowed, so wait as long
808-
// as the caller asked for rather than dropping a server that is still mid-handshake.
809-
const grace = entry.config.timeout?.startup ?? STARTUP_GRACE_MS
810-
const remaining = entry.attempt.started + grace - now
805+
const remaining = entry.attempt.started + STARTUP_GRACE_MS - now
811806
return remaining > 0 ? [entry.attempt.settled.await.pipe(Effect.raceFirst(Effect.sleep(remaining)))] : []
812807
}),
813808
{ concurrency: "unbounded", discard: true },

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,13 +2745,9 @@ const gatedServer = Effect.gen(function* () {
27452745
})
27462746

27472747
// The real Mcp layer behind the tool registry; the bus never delivers, so only flush can register tools.
2748-
const startupRegistry = (url: string, timeout?: ConfigMCP.Timeout) =>
2748+
const startupRegistry = (url: string) =>
27492749
AppNodeBuilder.build(LayerNode.group([Mcp.node, Tool.node, McpTool.node]), [
2750-
Mcp.node.replace(
2751-
resourceMcpLayer(
2752-
new ConfigMCP.Remote({ type: "remote", url, oauth: false, codemode: false, ...(timeout ? { timeout } : {}) }),
2753-
),
2754-
),
2750+
Mcp.node.replace(resourceMcpLayer(new ConfigMCP.Remote({ type: "remote", url, oauth: false, codemode: false }))),
27552751
Permission.node.replace(permissions),
27562752
Bus.node.replace(events),
27572753
Image.node.replace(imagePassthrough),
@@ -2779,7 +2775,7 @@ testEffect(Layer.empty).effect("flush waits for a server that connects within it
27792775
}),
27802776
)
27812777

2782-
testEffect(Layer.empty).effect("flush stops waiting for a hung server when the default startup window ends", () =>
2778+
testEffect(Layer.empty).effect("flush stops waiting for a hung server when its startup window ends", () =>
27832779
Effect.gen(function* () {
27842780
const gated = yield* gatedServer
27852781
yield* Effect.gen(function* () {
@@ -2803,28 +2799,6 @@ testEffect(Layer.empty).effect("flush stops waiting for a hung server when the d
28032799
}),
28042800
)
28052801

2806-
testEffect(Layer.empty).effect("flush waits as long as a server's own startup timeout", () =>
2807-
Effect.gen(function* () {
2808-
const gated = yield* gatedServer
2809-
yield* Effect.gen(function* () {
2810-
const registry = yield* Tool.Service
2811-
const registration = yield* McpTool.Service
2812-
yield* Deferred.await(gated.started)
2813-
const flush = yield* registration.flush.pipe(Effect.forkChild({ startImmediately: true }))
2814-
// Well past the default window, which would have dropped a server that is still handshaking.
2815-
yield* TestClock.adjust("3500 millis")
2816-
expect(flush.pollUnsafe()).toBeUndefined()
2817-
2818-
yield* Deferred.succeed(gated.release, undefined)
2819-
yield* Fiber.join(flush)
2820-
expect((yield* toolDefinitions(registry)).map((tool) => tool.name)).toContain("resources_echo")
2821-
}).pipe(
2822-
Effect.ensuring(Deferred.succeed(gated.release, undefined)),
2823-
Effect.provide(startupRegistry(gated.server.url, new ConfigMCP.Timeout({ startup: 10_000 }))),
2824-
)
2825-
}),
2826-
)
2827-
28282802
it.effect("advertises MCP output schemas to Code Mode", () =>
28292803
Effect.gen(function* () {
28302804
const registry = yield* Tool.Service

0 commit comments

Comments
 (0)