MCPcopy Index your code
hub / github.com/simstudioai/sim / withConnectTimeout

Function withConnectTimeout

apps/sim/lib/mcp/connection-manager.ts:38–61  ·  view source on GitHub ↗
(client: McpClient, serverName: string)

Source from the content-addressed store, hash-verified

36type ToolsChangedListener = (event: ToolsChangedEvent) => void
37
38async 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.

Callers 1

connectMethod · 0.85

Calls 2

connectMethod · 0.45
disconnectMethod · 0.45

Tested by

no test coverage detected