From 1c33b207e5c502d569a6af91c8b4aa26b79d07e0 Mon Sep 17 00:00:00 2001 From: Ori <18102267+oritwoen@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:49:10 +0100 Subject: [PATCH] fix(mcp): serve 2026 clients --- src/commands/mcp.ts | 17 ++++++++--- test/cli.test.ts | 73 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/src/commands/mcp.ts b/src/commands/mcp.ts index b513e1c..cb5b14e 100644 --- a/src/commands/mcp.ts +++ b/src/commands/mcp.ts @@ -1,12 +1,21 @@ /** - * Serve the MCP server over stdio in the harness's directory. The SDK loads here and nowhere else. + * Serve the MCP server over stdio in the harness's directory, to 2025 and 2026 clients alike. + * The SDK loads here and nowhere else. * - * @returns {Promise} Once the transport is connected. + * @returns {Promise} Once the server listens. + * @throws {Error} When the scope or fields env is malformed. */ export async function serveMcp(): Promise { - const [{ createMcpServer }, { StdioServerTransport }] = await Promise.all([ + const [{ createMcpServer }, { serveStdio }] = await Promise.all([ import("../mcp.ts"), import("@modelcontextprotocol/server/stdio"), ]); - await createMcpServer({ cwd: process.cwd() }).connect(new StdioServerTransport()); + const options = { cwd: process.cwd() }; + /** Built up front, so a bad env still fails the start. */ + let first: ReturnType | undefined = createMcpServer(options); + serveStdio(() => { + const server = first ?? createMcpServer(options); + first = undefined; + return server; + }); } diff --git a/test/cli.test.ts b/test/cli.test.ts index 5c14d8b..60db1bc 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -11,6 +11,8 @@ import { import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { pathToFileURL } from "node:url"; +import { Client } from "@modelcontextprotocol/client"; +import { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { describe, expect, it } from "vite-plus/test"; const root = resolve(import.meta.dirname, ".."); @@ -91,6 +93,41 @@ function loadedFrom(loaded: readonly string[], directory: string): string[] { return loaded.filter((url) => url.includes(directory)); } +/** Connect a client of one protocol era to `memory mcp` from the built bin. */ +async function connectBin( + mode: "legacy" | Readonly<{ pin: string }>, + env: Readonly>, +): Promise { + const client = new Client( + { name: "memory-test", version: "1.0.0" }, + { versionNegotiation: { mode } }, + ); + await client.connect( + new StdioClientTransport({ + command: process.execPath, + args: [bin, "mcp"], + cwd: root, + env: { ...environment, ...env } as Record, + stderr: "ignore", + }), + ); + return client; +} + +/** List the tool names over one connection, then close it. */ +async function listedNames(client: Client): Promise { + try { + return (await client.listTools()).tools.map((tool) => tool.name); + } finally { + await client.close(); + } +} + +const builds = [ + { from: "source", env: {} }, + { from: "bundle", env: { MEMORY_DIST: "1" } }, +] as const; + describe("memory usage paths", () => { it.each([ { @@ -145,9 +182,45 @@ describe("memory usage paths", () => { expect(serverName(result.stdout)).toBe("memory"); expect(loadedFrom(result.loaded, "/node_modules/@modelcontextprotocol/")).not.toEqual([]); }); + + it("stops before serving when the scope is half set", () => { + const result = runCli(["mcp"], { + env: { AGNTN_MEMORY_NAMESPACE: "default", AGNTN_MEMORY_PROFILE: "" }, + input: initialize, + }); + + expect(result.status).toBe(1); + expect(result.stdout).toBe(""); + expect(result.stderr).toContain( + "AGNTN_MEMORY_NAMESPACE and AGNTN_MEMORY_PROFILE must be set together", + ); + }); }); describe.skipIf(!existsSync(bin))("memory mcp from the built bin", () => { + it.each(builds)("lets a client pinned to 2026 listen, from the $from", async (build) => { + const legacy = await listedNames(await connectBin("legacy", build.env)); + const client = await connectBin({ pin: "2026-07-28" }, build.env); + try { + expect(client.getNegotiatedProtocolVersion()).toBe("2026-07-28"); + const subscription = await client.listen({ toolsListChanged: true }); + await subscription.close(); + } catch (error) { + await client.close(); + throw error; + } + + expect(legacy).toContain("memory_remember"); + expect(await listedNames(client)).toEqual(legacy); + }); + + it.each(builds)("still opens a 2025 client with initialize, from the $from", async (build) => { + const client = await connectBin("legacy", build.env); + + expect(client.getNegotiatedProtocolVersion()).toMatch(/^2025-/u); + expect(await listedNames(client)).toContain("memory_remember"); + }); + it("serves the live source inside a checkout", () => { const result = runCli(["mcp"], { entry: bin, input: initialize });