Skip to content

Commit 2a40055

Browse files
committed
fix(node-host): stop MCP recovery after abort
1 parent 3e11c65 commit 2a40055

2 files changed

Lines changed: 56 additions & 36 deletions

File tree

‎src/node-host/mcp.test.ts‎

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -541,33 +541,41 @@ describe("node host MCP manager", () => {
541541
});
542542

543543
it("closes a server when startup is aborted during paginated listing", async () => {
544-
const controller = new AbortController();
545-
const client = createClient({
546-
list: async () =>
547-
await new Promise<{ tools: Tool[] }>(() => {
548-
// The startup abort owns closing the client behind this pending SDK request.
549-
}),
550-
});
551-
const warn = vi.fn();
552-
const starting = startNodeHostMcpManager(
553-
{ docs: { command: "docs" } },
554-
{
555-
createClient: () => client,
556-
resolveTransport: () => transport,
557-
signal: controller.signal,
558-
warn,
559-
},
560-
);
561-
await vi.waitFor(() => expect(client.listTools).toHaveBeenCalledOnce());
544+
vi.useFakeTimers();
545+
try {
546+
const controller = new AbortController();
547+
const client = createClient({
548+
list: async () =>
549+
await new Promise<{ tools: Tool[] }>(() => {
550+
// The startup abort owns closing the client behind this pending SDK request.
551+
}),
552+
});
553+
const createClientMock = vi.fn(() => client);
554+
const warn = vi.fn();
555+
const starting = startNodeHostMcpManager(
556+
{ docs: { command: "docs" } },
557+
{
558+
createClient: createClientMock,
559+
resolveTransport: () => transport,
560+
signal: controller.signal,
561+
warn,
562+
},
563+
);
564+
await vi.waitFor(() => expect(client.listTools).toHaveBeenCalledOnce());
562565

563-
controller.abort();
564-
const manager = await starting;
566+
controller.abort();
567+
const manager = await starting;
565568

566-
expect(client.close).toHaveBeenCalledOnce();
567-
expect(manager.descriptors).toEqual([]);
568-
expect(warn).not.toHaveBeenCalled();
569-
await manager.close();
570-
expect(client.close).toHaveBeenCalledOnce();
569+
expect(client.close).toHaveBeenCalledOnce();
570+
expect(manager.descriptors).toEqual([]);
571+
expect(warn).not.toHaveBeenCalled();
572+
await vi.advanceTimersByTimeAsync(5 * 60_000);
573+
expect(createClientMock).toHaveBeenCalledOnce();
574+
await manager.close();
575+
expect(client.close).toHaveBeenCalledOnce();
576+
} finally {
577+
vi.useRealTimers();
578+
}
571579
});
572580

573581
it("cancels an in-flight MCP tool when its node invocation is aborted", async () => {

‎src/node-host/mcp.ts‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ export async function startNodeHostMcpManager(
279279
const resolveTransport = deps.resolveTransport ?? resolveMcpTransport;
280280
const descriptors: NodePluginToolDescriptor[] = [];
281281
const lifecycleAbortController = new AbortController();
282+
const lifecycleSignal = deps.signal
283+
? AbortSignal.any([deps.signal, lifecycleAbortController.signal])
284+
: lifecycleAbortController.signal;
282285
const states = new Map<string, NodeHostMcpServerState>(
283286
listEnabledNodeHostMcpServers(servers).map(([serverName, config]) => [
284287
serverName,
@@ -336,7 +339,13 @@ export async function startNodeHostMcpManager(
336339
};
337340

338341
const scheduleRetry = (state: NodeHostMcpServerState): void => {
339-
if (closed || states.get(state.serverName) !== state || state.retryTimer || state.current) {
342+
if (
343+
closed ||
344+
lifecycleSignal.aborted ||
345+
states.get(state.serverName) !== state ||
346+
state.retryTimer ||
347+
state.current
348+
) {
340349
return;
341350
}
342351
const delayMs = state.retryDelayMs;
@@ -431,9 +440,9 @@ export async function startNodeHostMcpManager(
431440
return;
432441
}
433442
try {
434-
await connectAndList(state, lifecycleAbortController.signal);
443+
await connectAndList(state, lifecycleSignal);
435444
} catch (error) {
436-
if (!closed) {
445+
if (!closed && !lifecycleSignal.aborted) {
437446
warn(
438447
`node host MCP server "${state.serverName}" reconnect failed: ${formatMcpError(error)}`,
439448
);
@@ -451,15 +460,15 @@ export async function startNodeHostMcpManager(
451460
session.client,
452461
session.requestTimeoutMs,
453462
(toolName) => isMcpToolAllowed(state.config.toolFilter, toolName),
454-
AbortSignal.any([lifecycleAbortController.signal, session.abortController.signal]),
463+
AbortSignal.any([lifecycleSignal, session.abortController.signal]),
455464
);
456465
if (closed || state.current !== session) {
457466
return;
458467
}
459468
state.listedTools = tools;
460469
rebuildDescriptors();
461470
} catch (error) {
462-
if (closed || state.current !== session) {
471+
if (closed || lifecycleSignal.aborted || state.current !== session) {
463472
return;
464473
}
465474
warn(
@@ -472,7 +481,13 @@ export async function startNodeHostMcpManager(
472481
};
473482

474483
function requestRefresh(state: NodeHostMcpServerState, session: NodeHostMcpSession): void {
475-
if (closed || state.current !== session || !session.connected || state.refreshQueued) {
484+
if (
485+
closed ||
486+
lifecycleSignal.aborted ||
487+
state.current !== session ||
488+
!session.connected ||
489+
state.refreshQueued
490+
) {
476491
return;
477492
}
478493
state.refreshQueued = true;
@@ -482,19 +497,16 @@ export async function startNodeHostMcpManager(
482497
});
483498
}
484499

485-
const startupSignal = deps.signal
486-
? AbortSignal.any([deps.signal, lifecycleAbortController.signal])
487-
: lifecycleAbortController.signal;
488500
const tasks = Array.from(states.values(), (state) => async () => {
489501
if (state.config.auth === "oauth" || state.config.oauth) {
490502
states.delete(state.serverName);
491503
warn(`node host MCP server "${state.serverName}" skipped: OAuth is not supported`);
492504
return;
493505
}
494506
try {
495-
await connectAndList(state, startupSignal);
507+
await connectAndList(state, lifecycleSignal);
496508
} catch (error) {
497-
if (!startupSignal.aborted) {
509+
if (!lifecycleSignal.aborted) {
498510
warn(`node host MCP server "${state.serverName}" failed: ${formatMcpError(error)}`);
499511
}
500512
}

0 commit comments

Comments
 (0)