()
| 468 | * Handler for getAllMcpConfig - exported so it can be called on app startup |
| 469 | */ |
| 470 | export async function getAllMcpConfigHandler() { |
| 471 | try { |
| 472 | const totalStart = Date.now() |
| 473 | |
| 474 | // Clear cache before repopulating |
| 475 | workingMcpServers.clear() |
| 476 | |
| 477 | const config = await readClaudeConfig() |
| 478 | |
| 479 | const convertServers = async ( |
| 480 | servers: Record<string, McpServerConfig> | undefined, |
| 481 | scope: string | null, |
| 482 | ) => { |
| 483 | if (!servers) return [] |
| 484 | |
| 485 | const results = await Promise.all( |
| 486 | Object.entries(servers).map(async ([name, serverConfig]) => { |
| 487 | const configObj = serverConfig as Record<string, unknown> |
| 488 | let status = getServerStatusFromConfig(serverConfig) |
| 489 | const headers = serverConfig.headers as |
| 490 | | Record<string, string> |
| 491 | | undefined |
| 492 | |
| 493 | let tools: McpToolInfo[] = [] |
| 494 | let needsAuth = false |
| 495 | |
| 496 | try { |
| 497 | tools = await fetchToolsForServer(serverConfig) |
| 498 | } catch (error) { |
| 499 | console.error(`[MCP] Failed to fetch tools for ${name}:`, error) |
| 500 | } |
| 501 | |
| 502 | const cacheKey = mcpCacheKey(scope, name) |
| 503 | if (tools.length > 0) { |
| 504 | status = "connected" |
| 505 | workingMcpServers.set(cacheKey, true) |
| 506 | } else { |
| 507 | workingMcpServers.set(cacheKey, false) |
| 508 | if (serverConfig.url) { |
| 509 | try { |
| 510 | const baseUrl = getMcpBaseUrl(serverConfig.url) |
| 511 | const metadata = await fetchOAuthMetadata(baseUrl) |
| 512 | needsAuth = !!metadata && !!metadata.authorization_endpoint |
| 513 | } catch { |
| 514 | // If probe fails, assume no auth needed |
| 515 | } |
| 516 | } else if ( |
| 517 | serverConfig.authType === "oauth" || |
| 518 | serverConfig.authType === "bearer" |
| 519 | ) { |
| 520 | needsAuth = true |
| 521 | } |
| 522 | |
| 523 | if (needsAuth && !headers?.Authorization) { |
| 524 | status = "needs-auth" |
| 525 | } else { |
| 526 | // No tools and doesn't need auth - server failed to connect or has no tools |
| 527 | status = "failed" |
no test coverage detected