|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// MCP connection pooling regression test |
| 3 | +// |
| 4 | +// Production telemetry (Axiom, 2026-04-30): `plugin.mcp.connection.acquire` |
| 5 | +// p99 = 3.59s, with `plugin.mcp.connection.handshake` p99 = 3.28s firing on |
| 6 | +// ~17% of acquires. The cold MCP handshake is the dominant tail-latency |
| 7 | +// source for tool invocations. The MCP plugin already pools connections via |
| 8 | +// a `ScopedCache` keyed on `${transport}:${invokerScope}:${endpoint}` (see |
| 9 | +// `plugin.ts#makeRuntime`), so within a single executor / session DO the |
| 10 | +// SECOND and onward invocations against the same source config MUST reuse |
| 11 | +// the cached connection without performing a fresh transport handshake. |
| 12 | +// |
| 13 | +// This file pins that contract behind a deterministic test harness that |
| 14 | +// counts inbound MCP server sessions — each MCP server `connect` is exactly |
| 15 | +// one cold handshake on the wire. After the first invoke kicks off the cold |
| 16 | +// connection, all subsequent calls (regardless of which tool on the source |
| 17 | +// they target) MUST land on the same MCP session. |
| 18 | +// |
| 19 | +// If this test starts failing, the cache key, the runtime ref, or the |
| 20 | +// `Effect.scoped` boundary inside `invokeMcpTool` has regressed and prod |
| 21 | +// will see the cold-handshake p99 climb proportionally. |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | + |
| 24 | +import { describe, expect, it } from "@effect/vitest"; |
| 25 | +import { Effect } from "effect"; |
| 26 | +import * as http from "node:http"; |
| 27 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 28 | +import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; |
| 29 | +import { z } from "zod"; |
| 30 | + |
| 31 | +import { createExecutor, makeTestConfig } from "@executor/sdk"; |
| 32 | + |
| 33 | +import { mcpPlugin } from "./plugin"; |
| 34 | + |
| 35 | +// --------------------------------------------------------------------------- |
| 36 | +// Test MCP server — counts session connects (each = one cold handshake) |
| 37 | +// --------------------------------------------------------------------------- |
| 38 | + |
| 39 | +function createTestMcpServer() { |
| 40 | + const server = new McpServer( |
| 41 | + { name: "pool-test-server", version: "1.0.0" }, |
| 42 | + { capabilities: {} }, |
| 43 | + ); |
| 44 | + |
| 45 | + server.registerTool( |
| 46 | + "echo", |
| 47 | + { description: "Echo a value", inputSchema: { value: z.string() } }, |
| 48 | + async ({ value }: { value: string }) => ({ |
| 49 | + content: [{ type: "text" as const, text: value }], |
| 50 | + }), |
| 51 | + ); |
| 52 | + |
| 53 | + server.registerTool( |
| 54 | + "echo2", |
| 55 | + { description: "Echo a value", inputSchema: { value: z.string() } }, |
| 56 | + async ({ value }: { value: string }) => ({ |
| 57 | + content: [{ type: "text" as const, text: `b:${value}` }], |
| 58 | + }), |
| 59 | + ); |
| 60 | + |
| 61 | + return server; |
| 62 | +} |
| 63 | + |
| 64 | +type TestServer = { |
| 65 | + readonly url: string; |
| 66 | + readonly httpServer: http.Server; |
| 67 | + /** Number of MCP sessions created (1 per cold transport handshake). */ |
| 68 | + readonly sessionCount: () => number; |
| 69 | +}; |
| 70 | + |
| 71 | +const serveMcpServer = Effect.acquireRelease( |
| 72 | + Effect.async<TestServer, Error>((resume) => { |
| 73 | + const transports = new Map<string, StreamableHTTPServerTransport>(); |
| 74 | + let sessions = 0; |
| 75 | + |
| 76 | + const httpServer = http.createServer(async (req, res) => { |
| 77 | + const sessionId = req.headers["mcp-session-id"] as string | undefined; |
| 78 | + |
| 79 | + if (sessionId) { |
| 80 | + const transport = transports.get(sessionId); |
| 81 | + if (!transport) { |
| 82 | + res.writeHead(404); |
| 83 | + res.end("Session not found"); |
| 84 | + return; |
| 85 | + } |
| 86 | + await transport.handleRequest(req, res); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const mcpServer = createTestMcpServer(); |
| 91 | + sessions++; |
| 92 | + |
| 93 | + const transport = new StreamableHTTPServerTransport({ |
| 94 | + sessionIdGenerator: () => crypto.randomUUID(), |
| 95 | + onsessioninitialized: (sid) => { |
| 96 | + transports.set(sid, transport); |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + await mcpServer.connect(transport); |
| 101 | + await transport.handleRequest(req, res); |
| 102 | + }); |
| 103 | + |
| 104 | + httpServer.listen(0, () => { |
| 105 | + const addr = httpServer.address(); |
| 106 | + const port = typeof addr === "object" && addr ? addr.port : 0; |
| 107 | + resume( |
| 108 | + Effect.succeed({ |
| 109 | + url: `http://127.0.0.1:${port}`, |
| 110 | + httpServer, |
| 111 | + sessionCount: () => sessions, |
| 112 | + }), |
| 113 | + ); |
| 114 | + }); |
| 115 | + }), |
| 116 | + ({ httpServer }) => |
| 117 | + Effect.sync(() => { |
| 118 | + httpServer.close(); |
| 119 | + }), |
| 120 | +); |
| 121 | + |
| 122 | +// --------------------------------------------------------------------------- |
| 123 | +// Helper — one executor, one mcp source pointed at the test server |
| 124 | +// --------------------------------------------------------------------------- |
| 125 | + |
| 126 | +const makeTestExecutor = (serverUrl: string) => |
| 127 | + createExecutor( |
| 128 | + makeTestConfig({ |
| 129 | + plugins: [mcpPlugin()] as const, |
| 130 | + }), |
| 131 | + ).pipe( |
| 132 | + Effect.tap((executor) => |
| 133 | + executor.mcp.addSource({ |
| 134 | + transport: "remote", |
| 135 | + scope: "test-scope", |
| 136 | + name: "pool-test", |
| 137 | + endpoint: serverUrl, |
| 138 | + }), |
| 139 | + ), |
| 140 | + ); |
| 141 | + |
| 142 | +// --------------------------------------------------------------------------- |
| 143 | +// Tests |
| 144 | +// --------------------------------------------------------------------------- |
| 145 | + |
| 146 | +describe("MCP connection pooling (regression)", () => { |
| 147 | + it.scoped( |
| 148 | + "five sequential invokes against the same source perform exactly one transport handshake", |
| 149 | + () => |
| 150 | + Effect.gen(function* () { |
| 151 | + const server = yield* serveMcpServer; |
| 152 | + const executor = yield* makeTestExecutor(server.url); |
| 153 | + const tools = yield* executor.tools.list(); |
| 154 | + const echo = tools.find((t) => t.name === "echo")!; |
| 155 | + expect(echo).toBeDefined(); |
| 156 | + |
| 157 | + // Discovery during addSource performs ONE handshake on a separate, |
| 158 | + // closed connection — that's expected. Capture the baseline so we |
| 159 | + // assert against the invoke-path delta only. |
| 160 | + const sessionsAfterAddSource = server.sessionCount(); |
| 161 | + expect(sessionsAfterAddSource).toBeGreaterThanOrEqual(1); |
| 162 | + |
| 163 | + // First invoke is necessarily a cache miss — it cold-handshakes the |
| 164 | + // pooled connection. Every subsequent invoke must hit the cache and |
| 165 | + // produce zero new MCP server sessions. |
| 166 | + yield* executor.tools.invoke( |
| 167 | + echo.id, |
| 168 | + { value: "1" }, |
| 169 | + { onElicitation: "accept-all" }, |
| 170 | + ); |
| 171 | + const sessionsAfterFirstInvoke = server.sessionCount(); |
| 172 | + expect(sessionsAfterFirstInvoke).toBe(sessionsAfterAddSource + 1); |
| 173 | + |
| 174 | + for (let i = 2; i <= 5; i++) { |
| 175 | + yield* executor.tools.invoke( |
| 176 | + echo.id, |
| 177 | + { value: String(i) }, |
| 178 | + { onElicitation: "accept-all" }, |
| 179 | + ); |
| 180 | + // Cache must reuse the same MCP session for every subsequent |
| 181 | + // call. If this assertion fails the session DO is paying the |
| 182 | + // cold-handshake p99 (3.28s in prod) on every tool call. |
| 183 | + expect(server.sessionCount()).toBe(sessionsAfterFirstInvoke); |
| 184 | + } |
| 185 | + }), |
| 186 | + ); |
| 187 | + |
| 188 | + it.scoped( |
| 189 | + "different tools on the same source share the cached connection", |
| 190 | + () => |
| 191 | + Effect.gen(function* () { |
| 192 | + const server = yield* serveMcpServer; |
| 193 | + const executor = yield* makeTestExecutor(server.url); |
| 194 | + const tools = yield* executor.tools.list(); |
| 195 | + const echo = tools.find((t) => t.name === "echo")!; |
| 196 | + const echo2 = tools.find((t) => t.name === "echo2")!; |
| 197 | + expect(echo).toBeDefined(); |
| 198 | + expect(echo2).toBeDefined(); |
| 199 | + |
| 200 | + const sessionsAfterAddSource = server.sessionCount(); |
| 201 | + |
| 202 | + // Cold handshake on first call. |
| 203 | + yield* executor.tools.invoke( |
| 204 | + echo.id, |
| 205 | + { value: "a" }, |
| 206 | + { onElicitation: "accept-all" }, |
| 207 | + ); |
| 208 | + const baseline = server.sessionCount(); |
| 209 | + expect(baseline).toBe(sessionsAfterAddSource + 1); |
| 210 | + |
| 211 | + // Different tool on the SAME source — same cache key, same conn. |
| 212 | + yield* executor.tools.invoke( |
| 213 | + echo2.id, |
| 214 | + { value: "b" }, |
| 215 | + { onElicitation: "accept-all" }, |
| 216 | + ); |
| 217 | + expect(server.sessionCount()).toBe(baseline); |
| 218 | + |
| 219 | + // Back to the first tool — still pooled. |
| 220 | + yield* executor.tools.invoke( |
| 221 | + echo.id, |
| 222 | + { value: "c" }, |
| 223 | + { onElicitation: "accept-all" }, |
| 224 | + ); |
| 225 | + expect(server.sessionCount()).toBe(baseline); |
| 226 | + }), |
| 227 | + ); |
| 228 | +}); |
0 commit comments