Skip to content

Commit 0a6812a

Browse files
committed
fix: register onMcpServersChange callback for real-time MCP status updates
The previous implementation manually fetched MCP servers after connect/disconnect, which returned stale 'disconnected' state since the connection is asynchronous. SDK's onMcpServersChange callback fires after the connection completes, but was not registered. Fix: - Register onMcpServersChange in ChatSession's AgentCallbacks (wave-agent pattern) - Forward callback through ChatProvider to webview via 'mcpServersUpdate' message - ConfigurationDialog now handles both mcpServersResponse (initial) and mcpServersUpdate (push) commands - Remove redundant manual getMcpServers calls in messageHandler - Update notification message to clarify it indicates request sent, not completion
1 parent 3d42bed commit 0a6812a

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

src/chatProvider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ export class ChatProvider implements vscode.WebviewViewProvider {
160160
},
161161
onError: (error) => {
162162
this.sendErrorToView(error, viewType, windowId);
163+
},
164+
onMcpServersChange: (servers) => {
165+
this.webviewManager.postMessage({ command: 'mcpServersUpdate', servers }, viewType, windowId);
163166
}
164167
});
165168
}

src/session/chatSession.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface ChatSessionCallbacks {
1313
onPermissionModeChange: (mode: PermissionMode) => void;
1414
onToolPermissionRequest: (context: ToolPermissionContext) => Promise<PermissionDecision>;
1515
onError: (error: any) => void;
16+
onMcpServersChange?: (servers: McpServerStatus[]) => void;
1617
}
1718

1819
export class ChatSession {
@@ -92,6 +93,9 @@ export class ChatSession {
9293
onQueuedMessagesChange: (messages: QueuedMessage[]) => {
9394
this.messageQueue = messages;
9495
this.callbacks.onQueueChange(messages);
96+
},
97+
onMcpServersChange: (servers: McpServerStatus[]) => {
98+
this.callbacks.onMcpServersChange?.(servers);
9599
}
96100
};
97101

src/session/messageHandler.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,9 @@ export class MessageHandler {
714714
const session = this.context.getChatSession(viewType || 'tab', windowId);
715715
try {
716716
const success = await session.connectMcpServer(serverName);
717-
const servers = session.getMcpServers();
718-
this.context.postMessage({
719-
command: 'mcpServersResponse',
720-
servers
721-
}, viewType, windowId);
717+
// SDK's onMcpServersChange callback will push the updated state to frontend
722718
if (success) {
723-
vscode.window.showInformationMessage(`MCP 服务器 "${serverName}" 已连接`);
719+
vscode.window.showInformationMessage(`MCP 服务器 "${serverName}" 连接请求已发送`);
724720
}
725721
} catch (error) {
726722
console.error('连接 MCP 服务器失败:', error);
@@ -732,13 +728,9 @@ export class MessageHandler {
732728
const session = this.context.getChatSession(viewType || 'tab', windowId);
733729
try {
734730
const success = await session.disconnectMcpServer(serverName);
735-
const servers = session.getMcpServers();
736-
this.context.postMessage({
737-
command: 'mcpServersResponse',
738-
servers
739-
}, viewType, windowId);
731+
// SDK's onMcpServersChange callback will push the updated state to frontend
740732
if (success) {
741-
vscode.window.showInformationMessage(`MCP 服务器 "${serverName}" 已断开`);
733+
vscode.window.showInformationMessage(`MCP 服务器 "${serverName}" 断开请求已发送`);
742734
}
743735
} catch (error) {
744736
console.error('断开 MCP 服务器失败:', error);

webview/src/components/ConfigurationDialog.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ const ConfigurationDialog: React.FC<ConfigurationDialogProps & { vscode: any }>
102102
setMcpServers(message.servers || []);
103103
setMcpConnecting({});
104104
break;
105+
case 'mcpServersUpdate':
106+
// Push update from backend (e.g., after connect/disconnect completes)
107+
setMcpServers(message.servers || []);
108+
setMcpConnecting({});
109+
break;
105110
}
106111
};
107112
window.addEventListener('message', handleMessage);

0 commit comments

Comments
 (0)