()
| 542 | typeof error.response?.data === 'object' ? error.response.data.message : error.response?.data || error.message |
| 543 | |
| 544 | const addNewServer = async () => { |
| 545 | if (!validateServerUrl(serverUrl)) return |
| 546 | const body = { |
| 547 | name: serverName, |
| 548 | serverUrl, |
| 549 | iconSrc: iconSrc || undefined, |
| 550 | color: color || generateRandomGradient(), |
| 551 | authType |
| 552 | } |
| 553 | if (authType === MCP_AUTH_TYPE.CUSTOM_HEADERS) { |
| 554 | const hdrs = {} |
| 555 | headers.forEach(({ key, value }) => { |
| 556 | if (key) hdrs[key] = value |
| 557 | }) |
| 558 | if (Object.keys(hdrs).length > 0) body.authConfig = { headers: hdrs } |
| 559 | } |
| 560 | |
| 561 | // Step 1: create |
| 562 | let createdId |
| 563 | try { |
| 564 | const resp = await customMcpServersApi.createCustomMcpServer(body) |
| 565 | createdId = resp?.data?.id |
| 566 | if (!createdId) throw new Error('Create returned no id') |
| 567 | } catch (error) { |
| 568 | showSnackbar(`Failed to add MCP Server: ${getErrorMsg(error)}`, 'error') |
| 569 | onCancel() |
| 570 | return |
| 571 | } |
| 572 | |
| 573 | // Step 2: authorize |
| 574 | setAuthorizing(true) |
| 575 | try { |
| 576 | const auth = await customMcpServersApi.authorizeCustomMcpServer(createdId) |
| 577 | let toolsCount = 0 |
| 578 | if (auth?.data?.tools) { |
| 579 | try { |
| 580 | const parsed = JSON.parse(auth.data.tools) || {} |
| 581 | toolsCount = Array.isArray(parsed?.tools) ? parsed.tools.length : 0 |
| 582 | } catch { |
| 583 | /* ignore */ |
| 584 | } |
| 585 | } |
| 586 | showSnackbar(`MCP Server added and connected! Discovered ${toolsCount} tools`) |
| 587 | if (typeof onCreated === 'function') onCreated(createdId) |
| 588 | else onConfirm(createdId) // fallback if parent didn't wire onCreated |
| 589 | } catch (error) { |
| 590 | showSnackbar(`Added, but failed to connect: ${getErrorMsg(error)}`, 'error') |
| 591 | if (typeof onCreated === 'function') onCreated(createdId) |
| 592 | } finally { |
| 593 | setAuthorizing(false) |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | const saveServer = async () => { |
| 598 | if (!validateServerUrl(serverUrl)) return |
no test coverage detected