The Open Framework for Building Agents
test(eve): pin MCP session leak and duplicate inbound SSE GET (#4379) (#4794)
roboeve[bot]932a56169b7e
diff --git a/.changeset/close-mcp-sessions-per-step.md b/.changeset/close-mcp-sessions-per-step.mdnew file mode 100644index 000000000..9229a7550--- /dev/null+++ b/.changeset/close-mcp-sessions-per-step.md@@ -0,0 +1,5 @@+---+"eve": patch+---++MCP connections close their remote session when the step that opened it ends, instead of leaking one session per step until the process exits. A step that ends while its connection is still connecting, or a sign-in that finishes after its step, can still leave one session open. HTTP MCP connections keep at most one inbound SSE stream per session: the stream opens only after the session exists, and reconnecting never opens a second one while a stream is attached, so servers that allow a single stream no longer answer `409`. Ending a session is bounded best effort: its `DELETE` gives up after 5 seconds, so a server that stops answering doesn't stall the step.diff --git a/packages/eve/scripts/vendor-compiled/@ai-sdk/mcp.mjs b/packages/eve/scripts/vendor-compiled/@ai-sdk/mcp.mjsindex 18451931c..680ff289e 100644--- a/packages/eve/scripts/vendor-compiled/@ai-sdk/mcp.mjs+++ b/packages/eve/scripts/vendor-compiled/@ai-sdk/mcp.mjs@@ -1,45 +1,115 @@import { createDeclarationCopier } from "../_shared.mjs";-const DISCOVERY_TIMEOUT_SOURCE = "timeout: DEFAULT_PROTOCOL_DISCOVERY_TIMEOUT";-const HTTP_DISCOVERY_TIMEOUT_SOURCE =- "timeout: this.transport instanceof HttpMCPTransport ? void 0 : DEFAULT_PROTOCOL_DISCOVERY_TIMEOUT";--// Streamable HTTP always answers, so a slow `server/discover` is not a legacy-// signal: the MCP spec falls back there only on a 4xx. Upstream caps the probe-// at 1 s on every transport and then downgrades modern-only servers to-// `initialize`. Remove once @ai-sdk/mcp ships vercel/ai#22042; the contract-// check below fails the build when that line changes.-function waitForHttpProtocolDiscoveryPlugin() {+/**+ * Applies exact upstream snippet replacements to `@ai-sdk/mcp/dist/index.js`.+ * The build fails when a snippet is missing, so a dependency bump cannot+ * silently drop the patch.+ */+function patchMcpSourcePlugin(name, replacements) {let patched = false;return {- name: "eve-ai-sdk-mcp-wait-for-http-discovery",+ name: `eve-ai-sdk-mcp-${name}`,transform(code, id) {if (!id.replaceAll("\\", "/").endsWith("/@ai-sdk/mcp/dist/index.js")) {return null;}- if (!code.includes(DISCOVERY_TIMEOUT_SOURCE)) {- throw new Error("@ai-sdk/mcp's server/discover timeout contract changed.");+ let next = code;+ for (const [source, replacement] of replacements) {+ if (!next.includes(source)) {+ throw new Error(`@ai-sdk/mcp source for the ${name} patch changed.`);+ }+ next = next.replace(source, replacement);}patched = true;- return {- code: code.replace(DISCOVERY_TIMEOUT_SOURCE, HTTP_DISCOVERY_TIMEOUT_SOURCE),- map: null,- };+ return { code: next, map: null };},buildEnd() {if (!patched) {- throw new Error("@ai-sdk/mcp's server/discover timeout was not patched.");+ throw new Error(`@ai-sdk/mcp ${name} patch was not applied.`);}},};}+// Streamable HTTP always answers, so a slow `server/discover` is not a legacy+// signal: the MCP spec falls back there only on a 4xx. Upstream caps the probe+// at 1 s on every transport and then downgrades modern-only servers to+// `initialize`. Remove once @ai-sdk/mcp ships vercel/ai#22042.+const waitForHttpProtocolDiscovery = patchMcpSourcePlugin("wait-for-http-discovery", [+ [+ "timeout: DEFAULT_PROTOCOL_DISCOVERY_TIMEOUT",+ "timeout: this.transport instanceof HttpMCPTransport ? void 0 : DEFAULT_PROTOCOL_DISCOVERY_TIMEOUT",+ ],+]);++// A 2025-era session allows one standalone GET stream; MCP SDK servers answer+// a second with 409 and stateful gateways drop the session (#4379). Upstream+// marks the stream only once a GET's response arrives, so one connect sends up+// to three GETs, the first before `initialize` has created the session, and+// its reconnect timer opens another GET even when a stream is attached.+// `inboundSseConnection` becomes the one stream slot: taken synchronously when+// a GET starts, released when that GET fails or its stream ends, and every+// opener (start, protocol switch, 202, reconnect timer) goes through+// `startInboundSse`, which returns while the slot is taken. A protocol switch+// opens the stream only once a session exists; a fresh session's+// `notifications/initialized` 202 opens it otherwise. Remove once+// @ai-sdk/mcp gives `HttpMCPTransport` a single inbound stream.+const singleInboundSseStream = patchMcpSourcePlugin("single-inbound-sse-stream", [+ [+ "\t\tif (!this.inboundSseConnection) this.startInboundSse();\n\t}\n\tisModernProtocol() {",+ "\t\tif (this.sessionId) this.startInboundSse();\n\t}\n\tisModernProtocol() {",+ ],+ [+ "\t\tif (this.isModernProtocol()) return;\n\t\tthis.openInboundSse(triedAuth, resumeToken).catch((error) => {",+ `\t\tif (this.isModernProtocol() || this.inboundSseConnection) return;+\t\tconst pending = { close() {} };+\t\tthis.inboundSseConnection = pending;+\t\tthis.openInboundSse(triedAuth, resumeToken).finally(() => {+\t\t\tif (this.inboundSseConnection === pending) this.inboundSseConnection = void 0;+\t\t}).catch((error) => {`,+ ],+ [+ "\t\t\tthis.inboundReconnectAttempts = 0;\n\t\t\tprocessEvents().catch((error) => {",+ `\t\t\tthis.inboundReconnectAttempts = 0;+\t\t\tconst connection = this.inboundSseConnection;+\t\t\tprocessEvents().finally(() => {+\t\t\t\tif (this.inboundSseConnection === connection) this.inboundSseConnection = void 0;+\t\t\t}).catch((error) => {`,+ ],+]);++// Ending a 2025-era session is one DELETE, and nothing bounds it upstream:+// `close()` sends it with no signal, so a server that stops answering stalls+// every caller that awaits the close, and eve awaits it when a step ends. The+// DELETE now runs under a 5 s deadline of its own that is cleared once it+// settles. The deadline is best effort: an unreachable server keeps the+// session until it expires it. Remove once @ai-sdk/mcp bounds session+// termination.+const boundedSessionTermination = patchMcpSourcePlugin("bounded-session-termination", [+ [+ `\t\t\t\tawait this.fetchFn(this.url.href, {+\t\t\t\t\tmethod: "DELETE",+\t\t\t\t\theaders,+\t\t\t\t\tsignal: options?.signal,+\t\t\t\t\tredirect: this.redirectMode+\t\t\t\t}).catch(() => void 0);`,+ `\t\t\t\tconst deadline = new AbortController();+\t\t\t\tconst timer = setTimeout(() => deadline.abort(), 5e3);+\t\t\t\tawait this.fetchFn(this.url.href, {+\t\t\t\t\tmethod: "DELETE",+\t\t\t\t\theaders,+\t\t\t\t\tsignal: options?.signal ?? deadline.signal,+\t\t\t\t\tredirect: this.redirectMode+\t\t\t\t}).catch(() => void 0).finally(() => clearTimeout(timer));`,+ ],+]);+export default {packageName: "@ai-sdk/mcp",compiledPath: "@ai-sdk/mcp",chunkGroup: "workflow",- plugins: [waitForHttpProtocolDiscoveryPlugin()],+ plugins: [waitForHttpProtocolDiscovery, singleInboundSseStream, boundedSessionTermination],copyDeclarations: createDeclarationCopier({rewrites: {"@ai-sdk/provider": { kind: "vendored", compiledPath: "@ai-sdk/provider" },diff --git a/packages/eve/src/context/provider.ts b/packages/eve/src/context/provider.tsindex ebf0a29cd..bf68345f3 100644--- a/packages/eve/src/context/provider.ts+++ b/packages/eve/src/context/provider.ts@@ -31,4 +31,10 @@ export interface FrameworkContextProvider<T> {/** Rolls back provider-owned effects when the callback or a later commit fails. */rollback?(value: T, cause: unknown): void | Promise<void>;++ /**+ * Releases resources the step-local value holds (e.g. open connections)+ * when the scope ends, on success and failure alike. Must not throw.+ */+ dispose?(value: T): void | Promise<void>;}diff --git a/packages/eve/src/context/providers/connection.ts b/packages/eve/src/context/providers/connection.tsindex 86aead0d1..3cfaa046f 100644--- a/packages/eve/src/context/providers/connection.ts+++ b/packages/eve/src/context/providers/connection.ts@@ -23,4 +23,8 @@ export const connectionProvider: FrameworkContextProvider<ConnectionRegistry> =return { value: new ConnectionRegistryImpl(connections) };},++ // Each step builds its own registry, so its clients must close with the+ // step or every step leaks one remote MCP session (#4379).+ dispose: (registry) => registry.dispose(),};diff --git a/packages/eve/src/context/run-step-connections.test.ts b/packages/eve/src/context/run-step-connections.test.tsnew file mode 100644index 000000000..0512316af--- /dev/null+++ b/packages/eve/src/context/run-step-connections.test.ts@@ -0,0 +1,116 @@+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import { ContextContainer } from "#context/container.js";+import { ConnectionRegistryKey } from "#context/providers/connection-key.js";+import { withContextScope } from "#context/run-step.js";+import type { HarnessSessionBase } from "#harness/types.js";+import {+ createStatefulMcpServer,+ type StatefulMcpServer,+} from "#internal/testing/stateful-mcp-server.js";+import { BundleKey } from "#runtime/sessions/runtime-context-keys.js";+import type { ResolvedConnectionDefinition } from "#runtime/types.js";++vi.mock("#context/providers/session.js", () => ({+ sessionProvider: { create: () => undefined, key: { name: "test.session" } },+}));+vi.mock("#context/providers/tool-stubs.js", () => ({+ toolStubProvider: { create: () => undefined, key: { name: "test.toolStubs" } },+}));+vi.mock("#context/providers/sandbox.js", () => ({+ sandboxProvider: { create: () => undefined, key: { name: "test.sandbox" } },+}));++const connection: ResolvedConnectionDefinition = {+ connectionName: "fixture",+ description: "stateful MCP fixture",+ logicalPath: "agent/connections/fixture.ts",+ protocol: "mcp",+ sourceId: "connections/fixture",+ sourceKind: "module",+ url: "http://127.0.0.1:8000/mcp",+};++/** A fresh step context whose agent has the one connection. */+function stepContext(): ContextContainer {+ const ctx = new ContextContainer();+ ctx.set(BundleKey, {+ graph: { root: { agent: { connections: [connection], dynamicConnectionResolvers: [] } } },+ } as never);+ return ctx;+}++/** Runs one step whose body connects to the MCP server, then returns or throws. */+async function runStepUsingConnection(outcome: "commit" | "fail"): Promise<void> {+ const ctx = stepContext();+ const scope = withContextScope(ctx, {} as HarnessSessionBase, async (session) => {+ await ctx.require(ConnectionRegistryKey).getClient("fixture").getToolMetadata();+ if (outcome === "fail") throw new Error("step failed");+ return { result: undefined, session };+ });+ if (outcome === "fail") {+ await expect(scope).rejects.toThrow("step failed");+ } else {+ await scope;+ }+}++describe("withContextScope MCP session lifecycle", () => {+ let server: StatefulMcpServer;++ beforeEach(() => {+ server = createStatefulMcpServer();+ vi.stubGlobal("fetch", server.fetch);+ });++ afterEach(() => {+ vi.useRealTimers();+ vi.unstubAllGlobals();+ });++ it.each(["commit", "fail"] as const)(+ "ends each step's MCP session when the step ends (%s)",+ async (outcome) => {+ await runStepUsingConnection(outcome);+ await runStepUsingConnection(outcome);++ const deletes = server.requests.filter((request) => request.method === "DELETE");+ expect(deletes.map((request) => request.status)).toEqual([200, 200]);+ expect(new Set(deletes.map((request) => request.sessionId)).size).toBe(2);+ expect(server.liveSessions.size).toBe(0);+ },+ );++ it("a step returns within the termination deadline when the server never answers DELETE", async () => {+ const ctx = stepContext();+ vi.useFakeTimers({ toFake: ["setTimeout"] });+ // A server that stopped answering: the DELETE arrives and never gets a response, but ends when aborted.+ let deleteArrived!: () => void;+ const terminationReached = new Promise<void>((resolve) => {+ deleteArrived = resolve;+ });+ vi.stubGlobal("fetch", (...[input, init]: Parameters<typeof fetch>) => {+ const signal = init?.signal;+ if (init?.method !== "DELETE") return server.fetch(input, init);+ deleteArrived();+ return new Promise<Response>((_resolve, reject) => {+ signal?.addEventListener("abort", () => reject(signal.reason), { once: true });+ });+ });+ let settled = false;+ const scope = withContextScope(ctx, {} as HarnessSessionBase, async (session) => {+ await ctx.require(ConnectionRegistryKey).getClient("fixture").getToolMetadata();+ return { result: undefined, session };+ }).finally(() => {+ settled = true;+ });++ await terminationReached;+ await vi.advanceTimersByTimeAsync(4_999);+ expect(settled).toBe(false);+ await vi.advanceTimersByTimeAsync(1);++ await scope;+ expect(settled).toBe(true);+ });+});diff --git a/packages/eve/src/context/run-step.ts b/packages/eve/src/context/run-step.tsindex aa93c47d2..82c819dac 100644--- a/packages/eve/src/context/run-step.ts+++ b/packages/eve/src/context/run-step.ts@@ -82,6 +82,10 @@ export async function withContextScope<T, S extends HarnessSessionBase>();}throw error;+ } finally {+ for (const provider of createdProviders.toReversed()) {+ await provider.dispose?.(ctx.require(provider.key));+ }}}diff --git a/packages/eve/src/internal/testing/stateful-mcp-server.ts b/packages/eve/src/internal/testing/stateful-mcp-server.tsnew file mode 100644index 000000000..444b394d0--- /dev/null+++ b/packages/eve/src/internal/testing/stateful-mcp-server.ts@@ -0,0 +1,101 @@+import { McpServer, WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/server";++export interface StatefulMcpRequest {+ readonly method: string;+ readonly sessionId: string | null;+ readonly status: number;+}++export interface StatefulMcpServer {+ /** Serves MCP Streamable HTTP in-process; pass it as the client's `fetch`. */+ readonly fetch: typeof fetch;+ /** Every request the server answered, in order. */+ readonly requests: readonly StatefulMcpRequest[];+ /** Sessions initialized and not yet closed by a client DELETE. */+ readonly liveSessions: ReadonlySet<string>;+ /** Resolves with the first answered request that matches, already answered or not. */+ waitForRequest(match: (request: StatefulMcpRequest) => boolean): Promise<StatefulMcpRequest>;+}++/**+ * A stateful 2025-era MCP server built from the MCP SDK's own transport, one+ * transport per session, routed by `mcp-session-id` the way the SDK's examples+ * do. It enforces the session rules real servers do: requests other than+ * `initialize` need a known session (400 without one, 404 for an unknown one),+ * a second standalone GET stream gets 409, and DELETE ends the session.+ */+export function createStatefulMcpServer(): StatefulMcpServer {+ const transports = new Map<string, WebStandardStreamableHTTPServerTransport>();+ const liveSessions = new Set<string>();+ const requests: StatefulMcpRequest[] = [];+ const waiters: {+ match: (request: StatefulMcpRequest) => boolean;+ resolve: (request: StatefulMcpRequest) => void;+ }[] = [];++ function record(request: Request, response: Response): Response {+ const entry = {+ method: request.method,+ sessionId: request.headers.get("mcp-session-id"),+ status: response.status,+ };+ requests.push(entry);+ for (const waiter of waiters.filter((candidate) => candidate.match(entry))) {+ waiters.splice(waiters.indexOf(waiter), 1);+ waiter.resolve(entry);+ }+ return response;+ }++ async function newSession(request: Request): Promise<Response> {+ const transport = new WebStandardStreamableHTTPServerTransport({+ sessionIdGenerator: () => crypto.randomUUID(),+ onsessioninitialized: (sessionId) => {+ transports.set(sessionId, transport);+ liveSessions.add(sessionId);+ },+ onsessionclosed: (sessionId) => {+ transports.delete(sessionId);+ liveSessions.delete(sessionId);+ },+ });+ const server = new McpServer({ name: "stateful-fixture", version: "1.0.0" });+ server.registerTool("ping", { description: "Replies pong." }, () => ({+ content: [{ type: "text", text: "pong" }],+ }));+ await server.connect(transport);+ return await transport.handleRequest(request);+ }++ async function handle(request: Request): Promise<Response> {+ const sessionId = request.headers.get("mcp-session-id");+ if (sessionId === null) {+ const body: unknown = request.method === "POST" ? await request.clone().json() : undefined;+ if (typeof body === "object" && body !== null && "method" in body) {+ if (body.method === "initialize") return await newSession(request);+ }+ return Response.json({ error: "No valid session ID provided" }, { status: 400 });+ }+ const transport = transports.get(sessionId);+ if (transport === undefined) {+ return Response.json({ error: "Session not found" }, { status: 404 });+ }+ return await transport.handleRequest(request);+ }++ return {+ fetch: async (input, init) => {+ const request = new Request(input, init);+ return record(request, await handle(request));+ },+ liveSessions,+ requests,+ waitForRequest: (match) => {+ const answered = requests.find(match);+ if (answered !== undefined) return Promise.resolve(answered);+ return new Promise((resolve) => {+ waiters.push({ match, resolve });+ });+ },+ };+}diff --git a/packages/eve/src/runtime/connections/mcp-client.ts b/packages/eve/src/runtime/connections/mcp-client.tsindex 69115642e..dcfde43e0 100644--- a/packages/eve/src/runtime/connections/mcp-client.ts+++ b/packages/eve/src/runtime/connections/mcp-client.ts@@ -66,7 +66,7 @@ async function checkAuthorizationResponse(/*** Wraps one `MCPClient` from `@ai-sdk/mcp` for a single connection.*- * Created lazily per-connection per-session. Caches tool metadata after+ * Created lazily per connection per step. Caches tool metadata after* the first `getToolMetadata()` call.** When the connection declares `authorization` but no token is availablediff --git a/packages/eve/src/runtime/connections/mcp-inbound-sse.test.ts b/packages/eve/src/runtime/connections/mcp-inbound-sse.test.tsnew file mode 100644index 000000000..314dc0942--- /dev/null+++ b/packages/eve/src/runtime/connections/mcp-inbound-sse.test.ts@@ -0,0 +1,79 @@+import { afterEach, describe, expect, it, vi } from "vitest";++import { createMCPClient } from "#compiled/@ai-sdk/mcp/index.js";+import {+ createStatefulMcpServer,+ type StatefulMcpServer,+} from "#internal/testing/stateful-mcp-server.js";++const SERVER_URL = "http://127.0.0.1:8000/mcp";++function inboundGets(server: StatefulMcpServer) {+ return server.requests.filter((request) => request.method === "GET");+}++// A 2025-era session allows one standalone GET stream. The MCP SDK server+// answers a second with 409, and stateful gateways drop the session (#4379).+describe("compiled HttpMCPTransport inbound SSE stream", () => {+ afterEach(() => {+ vi.useRealTimers();+ });++ it("opens one stream per connect, after the session exists", async () => {+ const server = createStatefulMcpServer();+ const client = await createMCPClient({+ onUncaughtError: vi.fn(),+ transport: { fetch: server.fetch, type: "http", url: SERVER_URL },+ });+ const [sessionId] = server.liveSessions;+ try {+ await server.waitForRequest((request) => request.method === "GET" && request.status === 200);+ } finally {+ await client.close();+ }++ expect(inboundGets(server)).toEqual([{ method: "GET", sessionId, status: 200 }]);+ });++ it("does not reopen the stream from a reconnect timer once a stream is attached", async () => {+ vi.useFakeTimers({ toFake: ["setTimeout"] });+ const server = createStatefulMcpServer();+ // The first session GET fails at the network layer, which schedules a+ // reconnect. The `notifications/initialized` 202 then opens a stream+ // before that reconnect fires.+ let markDropped!: () => void;+ const dropped = new Promise<void>((resolve) => {+ markDropped = resolve;+ });+ let droppedGets = 0;+ const fetch: typeof globalThis.fetch = async (input, init) => {+ const request = new Request(input, init);+ if (request.method === "GET" && request.headers.has("mcp-session-id") && droppedGets === 0) {+ droppedGets += 1;+ markDropped();+ throw new TypeError("fetch failed");+ }+ if (request.method === "POST") {+ const message = (await request.clone().json()) as { method?: string };+ if (message.method === "notifications/initialized") await dropped;+ }+ return await server.fetch(request);+ };++ const client = await createMCPClient({+ onUncaughtError: vi.fn(),+ transport: { fetch, type: "http", url: SERVER_URL },+ });+ const [sessionId] = server.liveSessions;+ try {+ await server.waitForRequest((request) => request.method === "GET" && request.status === 200);+ // Past every reconnect delay the transport could have scheduled.+ await vi.advanceTimersByTimeAsync(60_000);+ } finally {+ await client.close();+ }++ expect(droppedGets).toBe(1);+ expect(inboundGets(server)).toEqual([{ method: "GET", sessionId, status: 200 }]);+ });+});diff --git a/packages/eve/src/runtime/connections/registry-types.ts b/packages/eve/src/runtime/connections/registry-types.tsindex a93d7350b..61bd066ee 100644--- a/packages/eve/src/runtime/connections/registry-types.ts+++ b/packages/eve/src/runtime/connections/registry-types.ts@@ -2,7 +2,10 @@ import type { Approval } from "#approval/definition.js";import type { ConnectionClient } from "#shared/connection-types.js";import type { ResolvedConnectionDefinition } from "#runtime/types.js";-/** Per-session container mapping compiled connection names to live clients. */+/**+ * Per-step container mapping compiled connection names to live clients. The+ * step that creates it disposes it when the step ends.+ */export interface ConnectionRegistry {dispose(): Promise<void>;getClient(connectionName: string): ConnectionClient;diff --git a/packages/eve/src/runtime/connections/registry.ts b/packages/eve/src/runtime/connections/registry.tsindex 8d1f0ffba..87a5c7f92 100644--- a/packages/eve/src/runtime/connections/registry.ts+++ b/packages/eve/src/runtime/connections/registry.ts@@ -7,8 +7,9 @@ import type { ConnectionClient } from "#shared/connection-types.js";import type { ConnectionRegistry } from "#runtime/connections/registry-types.js";/**- * Per-session container mapping connection names to lazily-initialized- * client wrappers.+ * Per-step container mapping connection names to lazily-initialized+ * client wrappers. `withContextScope` builds one per step, and the connection+ * provider disposes it when the step ends.** The registry is protocol-agnostic: it dispatches to the client* implementation matching each connection's `protocol` (MCP or OpenAPI).