Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/fix-mcp-reconnect-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Fix MCP servers reporting as connected while their previous client is still closing during reconnect.
5 changes: 3 additions & 2 deletions packages/agent-core-v2/src/mcpCore/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,15 @@ export class McpConnectionManager implements McpConnectionView {
throw new Error2(ErrorCodes.MCP_SERVER_DISABLED, `MCP server is disabled: ${name}`);
}
const attemptId = this.beginConnectAttempt(entry);
await this.closeClient(entry);
if (!this.isCurrent(entry, attemptId)) return;
const closing = this.closeClient(entry);
entry.status = 'pending';
entry.tools = undefined;
entry.enabledNames = undefined;
entry.rawTools = undefined;
entry.error = undefined;
this.emit(entry);
await closing;
if (!this.isCurrent(entry, attemptId)) return;
await this.connectOne(entry, attemptId);
}

Expand Down
49 changes: 49 additions & 0 deletions packages/agent-core-v2/test/mcpCore/connection-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { z } from 'zod';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { Error2 } from '#/errors';
import { StdioMcpClient } from '#/mcpCore/client-stdio';
import { KIMI_MCP_CLIENT_NAME } from '#/mcpCore/client-shared';
import { McpConnectionManager, type McpServerEntry } from '#/mcpCore/connection-manager';
import { McpOAuthService } from '#/mcpCore/oauth/service';
Expand Down Expand Up @@ -240,6 +241,54 @@ describe('McpConnectionManager', () => {
}
}, 15000);

it('reconnect reports pending while the previous client is still closing', async () => {
const cm = new McpConnectionManager();
let releaseClose!: () => void;
let reportCloseStarted!: () => void;
const closeGate = new Promise<void>((resolve) => {
releaseClose = resolve;
});
const closeStarted = new Promise<void>((resolve) => {
reportCloseStarted = resolve;
});
const originalClose = StdioMcpClient.prototype.close;
const closeSpy = vi
.spyOn(StdioMcpClient.prototype, 'close')
.mockImplementation(async function (this: StdioMcpClient) {
reportCloseStarted();
await closeGate;
await originalClose.call(this);
});
const seen: Array<[McpServerEntry['status'], number, string | undefined]> = [];
let reconnect: Promise<void> | undefined;

try {
await cm.connectAll({ alpha: stdioConfig() });
cm.onStatusChange((entry) => {
seen.push([entry.status, entry.toolCount, entry.error]);
});

reconnect = cm.reconnect('alpha');
await closeStarted;

expect(cm.get('alpha')).toMatchObject({
status: 'pending',
toolCount: 0,
error: undefined,
});
expect(cm.resolved('alpha')).toBeUndefined();
expect(seen).toEqual([['pending', 0, undefined]]);
} finally {
releaseClose();
await reconnect?.catch(() => undefined);
try {
await cm.shutdown();
} finally {
closeSpy.mockRestore();
}
}
}, 15000);

it('reconnect cycles a failed server back through pending and into connected when fixed', async () => {
const cm = new McpConnectionManager();
try {
Expand Down
5 changes: 3 additions & 2 deletions packages/agent-core/src/mcp/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,15 @@ export class McpConnectionManager {
throw new KimiError(ErrorCodes.MCP_SERVER_DISABLED, `MCP server is disabled: ${name}`);
}
const attemptId = this.beginConnectAttempt(entry);
await this.closeClient(entry);
if (!this.isCurrent(entry, attemptId)) return;
const closing = this.closeClient(entry);
entry.status = 'pending';
entry.tools = undefined;
entry.rawTools = undefined;
entry.enabledNames = undefined;
entry.error = undefined;
this.emit(entry);
await closing;
if (!this.isCurrent(entry, attemptId)) return;
await this.connectOne(entry, attemptId);
}

Expand Down
49 changes: 49 additions & 0 deletions packages/agent-core/test/mcp/connection-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
import { z } from 'zod';

import { KimiError } from '../../src/errors';
import { StdioMcpClient } from '../../src/mcp/client-stdio';
import { ProviderManager } from '../../src/session/provider-manager';
import {
MCP_STARTUP_TIMEOUT_ENV,
Expand Down Expand Up @@ -206,6 +207,54 @@ describe('McpConnectionManager', () => {
}
}, 15000);

it('reconnect reports pending while the previous client is still closing', async () => {
const cm = new McpConnectionManager();
let releaseClose!: () => void;
let reportCloseStarted!: () => void;
const closeGate = new Promise<void>((resolve) => {
releaseClose = resolve;
});
const closeStarted = new Promise<void>((resolve) => {
reportCloseStarted = resolve;
});
const originalClose = StdioMcpClient.prototype.close;
const closeSpy = vi
.spyOn(StdioMcpClient.prototype, 'close')
.mockImplementation(async function (this: StdioMcpClient) {
reportCloseStarted();
await closeGate;
await originalClose.call(this);
});
const seen: Array<[McpServerEntry['status'], number, string | undefined]> = [];
let reconnect: Promise<void> | undefined;

try {
await cm.connectAll({ alpha: stdioConfig() });
cm.onStatusChange((entry) => {
seen.push([entry.status, entry.toolCount, entry.error]);
});

reconnect = cm.reconnect('alpha');
await closeStarted;

expect(cm.get('alpha')).toMatchObject({
status: 'pending',
toolCount: 0,
error: undefined,
});
expect(cm.resolved('alpha')).toBeUndefined();
expect(seen).toEqual([['pending', 0, undefined]]);
} finally {
releaseClose();
await reconnect?.catch(() => undefined);
try {
await cm.shutdown();
} finally {
closeSpy.mockRestore();
}
}
}, 15000);

it('reconnect cycles a failed server back through pending and into connected when fixed', async () => {
const cm = new McpConnectionManager();
try {
Expand Down