( agents: AgentDefinition[], )
| 464 | * @returns Array of AgentMcpServerInfo, grouped by server name with list of source agents |
| 465 | */ |
| 466 | export function extractAgentMcpServers( |
| 467 | agents: AgentDefinition[], |
| 468 | ): AgentMcpServerInfo[] { |
| 469 | // Map: server name -> { config, sourceAgents } |
| 470 | const serverMap = new Map< |
| 471 | string, |
| 472 | { |
| 473 | config: McpServerConfig & { name: string } |
| 474 | sourceAgents: string[] |
| 475 | } |
| 476 | >() |
| 477 | |
| 478 | for (const agent of agents) { |
| 479 | if (!agent.mcpServers?.length) continue |
| 480 | |
| 481 | for (const spec of agent.mcpServers) { |
| 482 | // Skip string references - these refer to servers already in global config |
| 483 | if (typeof spec === 'string') continue |
| 484 | |
| 485 | // Inline definition as { [name]: config } |
| 486 | const entries = Object.entries(spec) |
| 487 | if (entries.length !== 1) continue |
| 488 | |
| 489 | const [serverName, serverConfig] = entries[0]! |
| 490 | const existing = serverMap.get(serverName) |
| 491 | |
| 492 | if (existing) { |
| 493 | // Add this agent as another source |
| 494 | if (!existing.sourceAgents.includes(agent.agentType)) { |
| 495 | existing.sourceAgents.push(agent.agentType) |
| 496 | } |
| 497 | } else { |
| 498 | // New server |
| 499 | serverMap.set(serverName, { |
| 500 | config: { ...serverConfig, name: serverName } as McpServerConfig & { |
| 501 | name: string |
| 502 | }, |
| 503 | sourceAgents: [agent.agentType], |
| 504 | }) |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | // Convert map to array of AgentMcpServerInfo |
| 510 | // Only include transport types supported by AgentMcpServerInfo |
| 511 | const result: AgentMcpServerInfo[] = [] |
| 512 | for (const [name, { config, sourceAgents }] of serverMap) { |
| 513 | // Use type guards to properly narrow the discriminated union type |
| 514 | // Only include transport types that are supported by AgentMcpServerInfo |
| 515 | if (isStdioConfig(config)) { |
| 516 | result.push({ |
| 517 | name, |
| 518 | sourceAgents, |
| 519 | transport: 'stdio', |
| 520 | command: config.command, |
| 521 | needsAuth: false, |
| 522 | }) |
| 523 | } else if (isSSEConfig(config)) { |
no test coverage detected