diff --git a/common/src/mcp/__tests__/client-connections.test.ts b/common/src/mcp/__tests__/client-connections.test.ts new file mode 100644 index 0000000000..e9e8935036 --- /dev/null +++ b/common/src/mcp/__tests__/client-connections.test.ts @@ -0,0 +1,92 @@ +import { createServer } from 'node:http' + +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' +import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js' +import { expect, test } from 'bun:test' + +import { getMCPClient, listMCPTools } from '../client' + +/** + * Real loopback MCP handshakes can take over a second. Concurrent initialization + * must not open duplicate connections, including when callers retry a failure. + */ +test('shares an in-flight MCP connection and retries after its failure', async () => { + let handshakes = 0 + let failHandshake = true + const http = createServer(async (req, res) => { + if (req.method !== 'POST') { + res.writeHead(405).end() + return + } + try { + const chunks: Buffer[] = [] + for await (const chunk of req) chunks.push(Buffer.from(chunk)) + const body = JSON.parse(Buffer.concat(chunks).toString('utf8')) + if (body.method === 'initialize') { + handshakes++ + if (failHandshake) { + res.writeHead(503).end('fixture not ready') + return + } + } + const rpc = new McpServer({ + name: 'connection-sharing', + version: '1.0.0', + }) + rpc.registerTool('ping', { inputSchema: {} }, async () => ({ + content: [{ type: 'text', text: 'pong' }], + })) + const transport = new StreamableHTTPServerTransport({ + sessionIdGenerator: undefined, + enableJsonResponse: true, + }) + res.on('close', () => { + void rpc.close() + }) + await rpc.connect(transport) + await transport.handleRequest(req, res, body) + } catch (error) { + if (!res.headersSent) res.writeHead(500).end(String(error)) + } + }) + + try { + await new Promise((resolve, reject) => { + http.once('error', reject) + http.listen(0, '127.0.0.1', resolve) + }) + const address = http.address() + if (!address || typeof address === 'string') throw new Error('Missing port') + const config = { + type: 'http' as const, + url: `http://127.0.0.1:${address.port}/mcp`, + params: {}, + headers: {}, + } + const failed = await Promise.allSettled([ + getMCPClient(config), + getMCPClient(config), + ]) + expect(failed.map((result) => result.status)).toEqual([ + 'rejected', + 'rejected', + ]) + expect(handshakes).toBe(1) + + failHandshake = false + const [first, second] = await Promise.all([ + getMCPClient(config), + getMCPClient(config), + ]) + expect(handshakes).toBe(2) + expect(first).toBe(second) + expect((await listMCPTools(first)).tools.map((tool) => tool.name)).toEqual([ + 'ping', + ]) + expect(await getMCPClient(config)).toBe(first) + expect(handshakes).toBe(2) + } finally { + http.closeAllConnections() + await new Promise((resolve) => http.close(() => resolve())) + } +}, 10_000) diff --git a/common/src/mcp/client.ts b/common/src/mcp/client.ts index ca38ae2fd6..56327cb003 100644 --- a/common/src/mcp/client.ts +++ b/common/src/mcp/client.ts @@ -20,6 +20,7 @@ import type { const STDERR_BUFFER_CAP = 8192 const runningClients: Record = {} +const connectingClients = new Map>() const listToolsCache: Record< string, ReturnType @@ -118,6 +119,23 @@ export async function getMCPClient(config: MCPConfig): Promise { return key } + const pending = connectingClients.get(key) + if (pending) return pending + + const connection = connectMCPClient(config, key, imported) + connectingClients.set(key, connection) + try { + return await connection + } finally { + connectingClients.delete(key) + } +} + +async function connectMCPClient( + config: MCPConfig, + key: string, + imported: boolean, +): Promise { let transport: Transport // Buffer the child process's stderr so that a server which crashes during // startup produces an actionable error instead of the opaque MCP SDK message