(client: McpClient, serverName: string)
| 36 | type ToolsChangedListener = (event: ToolsChangedEvent) => void |
| 37 | |
| 38 | async function withConnectTimeout(client: McpClient, serverName: string): Promise<void> { |
| 39 | let timeoutId: ReturnType<typeof setTimeout> | undefined |
| 40 | let timedOut = false |
| 41 | const connectPromise = client.connect({ isCancelled: () => timedOut }) |
| 42 | try { |
| 43 | await Promise.race([ |
| 44 | connectPromise, |
| 45 | new Promise<never>((_, reject) => { |
| 46 | timeoutId = setTimeout(() => { |
| 47 | timedOut = true |
| 48 | reject(new Error(`Timed out connecting to MCP server ${serverName}`)) |
| 49 | }, CONNECT_TIMEOUT_MS) |
| 50 | }), |
| 51 | ]) |
| 52 | } catch (error) { |
| 53 | if (timedOut) { |
| 54 | void connectPromise.catch(() => {}) |
| 55 | } |
| 56 | await client.disconnect().catch(() => {}) |
| 57 | throw error |
| 58 | } finally { |
| 59 | if (timeoutId) clearTimeout(timeoutId) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Cache key for managed connections. |
no test coverage detected