Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions common/src/mcp/__tests__/client-connections.test.ts
Original file line number Diff line number Diff line change
@@ -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<void>((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<void>((resolve) => http.close(() => resolve()))
}
}, 10_000)
18 changes: 18 additions & 0 deletions common/src/mcp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
const STDERR_BUFFER_CAP = 8192

const runningClients: Record<string, Client> = {}
const connectingClients = new Map<string, Promise<string>>()
const listToolsCache: Record<
string,
ReturnType<typeof Client.prototype.listTools>
Expand Down Expand Up @@ -118,6 +119,23 @@ export async function getMCPClient(config: MCPConfig): Promise<string> {
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<string> {
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
Expand Down
Loading