MCPcopy
hub / github.com/codeaashu/claude-code / updateSdkMcp

Function updateSdkMcp

src/cli/print.ts:1389–1459  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1387 }
1388
1389 async function updateSdkMcp() {
1390 // Check if SDK MCP servers need to be updated (new servers added or removed)
1391 const currentServerNames = new Set(Object.keys(sdkMcpConfigs))
1392 const connectedServerNames = new Set(sdkClients.map(c => c.name))
1393
1394 // Check if there are any differences (additions or removals)
1395 const hasNewServers = Array.from(currentServerNames).some(
1396 name => !connectedServerNames.has(name),
1397 )
1398 const hasRemovedServers = Array.from(connectedServerNames).some(
1399 name => !currentServerNames.has(name),
1400 )
1401 // Check if any SDK clients are pending and need to be upgraded
1402 const hasPendingSdkClients = sdkClients.some(c => c.type === 'pending')
1403 // Check if any SDK clients failed their handshake and need to be retried.
1404 // Without this, a client that lands in 'failed' (e.g. handshake timeout on
1405 // a WS reconnect race) stays failed forever — its name satisfies the
1406 // connectedServerNames diff but it contributes zero tools.
1407 const hasFailedSdkClients = sdkClients.some(c => c.type === 'failed')
1408
1409 const haveServersChanged =
1410 hasNewServers ||
1411 hasRemovedServers ||
1412 hasPendingSdkClients ||
1413 hasFailedSdkClients
1414
1415 if (haveServersChanged) {
1416 // Clean up removed servers
1417 for (const client of sdkClients) {
1418 if (!currentServerNames.has(client.name)) {
1419 if (client.type === 'connected') {
1420 await client.cleanup()
1421 }
1422 }
1423 }
1424
1425 // Re-initialize all SDK MCP servers with current config
1426 const sdkSetup = await setupSdkMcpClients(
1427 sdkMcpConfigs,
1428 (serverName, message) =>
1429 structuredIO.sendMcpMessage(serverName, message),
1430 )
1431 sdkClients = sdkSetup.clients
1432 sdkTools = sdkSetup.tools
1433
1434 // Store SDK MCP tools in appState so subagents can access them via
1435 // assembleToolPool. Only tools are stored here — SDK clients are already
1436 // merged separately in the query loop (allMcpClients) and mcp_status handler.
1437 // Use both old (connectedServerNames) and new (currentServerNames) to remove
1438 // stale SDK tools when servers are added or removed.
1439 const allSdkNames = uniq([...connectedServerNames, ...currentServerNames])
1440 setAppState(prev => ({
1441 ...prev,
1442 mcp: {
1443 ...prev.mcp,
1444 tools: [
1445 ...prev.mcp.tools.filter(
1446 t =>

Callers 3

runHeadlessStreamingFunction · 0.85
applyPluginMcpDiffFunction · 0.85
runFunction · 0.85

Calls 8

setupSdkMcpClientsFunction · 0.85
uniqFunction · 0.85
getMcpPrefixFunction · 0.85
setupVscodeSdkMcpFunction · 0.85
keysMethod · 0.80
sendMcpMessageMethod · 0.80
hasMethod · 0.45
cleanupMethod · 0.45

Tested by

no test coverage detected