From 8cfa24108d6b081a40b624291a0a5a2e9fc94c05 Mon Sep 17 00:00:00 2001 From: Sanchit Rk <0xsanchit@gmail.com> Date: Sun, 1 Nov 2026 18:52:55 +0530 Subject: [PATCH] fix(harness): end MCP lists at a blank cursor and hide TypeError messages A server's `nextCursor: ""` is now dropped from every list result, so the SDK's page walk and the resource tools treat it as the last page instead of restarting until the page cap. A TypeError without a system code is described as `invalid request (TypeError)`: the runtime's own errors, such as an invalid header name, may quote the rejected value. Co-Authored-By: Claude Opus 5.5 --- packages/harness/src/mcp/client.ts | 4 + .../__artifacts__/mcp-review-followups.json | 76 +++++++++++++ .../test/__artifacts__/mcp.client.json | 8 +- packages/harness/test/fixtures/mcp/server.mjs | 36 ++++-- packages/harness/test/mcp.client.test.ts | 4 +- packages/harness/test/mcp.followups.test.ts | 104 ++++++++++++++++++ 6 files changed, 219 insertions(+), 13 deletions(-) create mode 100644 packages/harness/test/__artifacts__/mcp-review-followups.json create mode 100644 packages/harness/test/mcp.followups.test.ts diff --git a/packages/harness/src/mcp/client.ts b/packages/harness/src/mcp/client.ts index ebd0dbc5..18f1fe94 100644 --- a/packages/harness/src/mcp/client.ts +++ b/packages/harness/src/mcp/client.ts @@ -196,6 +196,8 @@ const isInvalidResult = (error: unknown) => error instanceof SdkError && error.c const describe = (error: unknown) => { if (!(error instanceof Error)) return String(error); if (isInvalidResult(error)) return condense(error.message); + // The runtime's own TypeErrors (a bad header, say) may quote what they rejected. + if (error instanceof TypeError && systemCode(error) === undefined) return "invalid request (TypeError)"; const detail = error instanceof SdkHttpError ? `HTTP ${error.status}` @@ -410,6 +412,8 @@ export const connect = Effect.fn("McpClient.connect")(function* (input: ConnectI }; const receive = transport.onmessage; transport.onmessage = (message, extra) => { + // A blank cursor ends the list: following it restarts most servers' paging. + if (isJSONRPCResultResponse(message) && message.result.nextCursor === "") delete message.result.nextCursor; if (isJSONRPCResultResponse(message) && listings.has(message.id)) { const page = screen(client, message.result); if (page !== undefined) pages.set(listings.get(message.id)!, page); diff --git a/packages/harness/test/__artifacts__/mcp-review-followups.json b/packages/harness/test/__artifacts__/mcp-review-followups.json new file mode 100644 index 00000000..e5a62fcc --- /dev/null +++ b/packages/harness/test/__artifacts__/mcp-review-followups.json @@ -0,0 +1,76 @@ +{ + "declared": [ + "mcp__paged__first", + "mcp__paged__second", + "list_mcp_resources", + "list_mcp_resource_templates", + "read_mcp_resource" + ], + "calls": { + "resources-page": "{\"server\":\"paged\",\"resources\":[{\"server\":\"paged\",\"name\":\"one\",\"uri\":\"fixture://one\"}],\"nextCursor\":\"page-2\"}", + "resources-next": "{\"server\":\"paged\",\"resources\":[{\"server\":\"paged\",\"name\":\"two\",\"uri\":\"fixture://two\"}]}", + "resources-all": "{\"resources\":[{\"server\":\"paged\",\"name\":\"one\",\"uri\":\"fixture://one\"},{\"server\":\"paged\",\"name\":\"two\",\"uri\":\"fixture://two\"}]}", + "templates-page": "{\"server\":\"paged\",\"resourceTemplates\":[{\"server\":\"paged\",\"name\":\"one\",\"uriTemplate\":\"fixture://one/{id}\"}],\"nextCursor\":\"page-2\"}", + "templates-next": "{\"server\":\"paged\",\"resourceTemplates\":[{\"server\":\"paged\",\"name\":\"two\",\"uriTemplate\":\"fixture://two/{id}\"}]}", + "templates-all": "{\"resourceTemplates\":[{\"server\":\"paged\",\"name\":\"one\",\"uriTemplate\":\"fixture://one/{id}\"},{\"server\":\"paged\",\"name\":\"two\",\"uriTemplate\":\"fixture://two/{id}\"}]}", + "bad-resources": "MCP server \"bad\" has no resources. Servers with resources: paged" + }, + "status": [ + { + "server": "paged", + "state": "connected", + "tools": [ + { + "name": "first", + "exposure": "direct" + }, + { + "name": "second", + "exposure": "direct" + } + ], + "resources": true + }, + { + "server": "bad", + "state": "failed", + "error": "invalid request (TypeError)", + "tools": [], + "resources": false + } + ], + "events": { + "main": { + "bad": [ + { + "server": "bad", + "state": "connecting" + }, + { + "server": "bad", + "state": "failed", + "error": "invalid request (TypeError)" + } + ], + "paged": [ + { + "server": "paged", + "state": "connecting" + }, + { + "server": "paged", + "state": "connected", + "tools": [ + "first", + "second" + ] + } + ] + } + }, + "log": [], + "warnings": [ + "[\"mcp transport error\",{\"server\":\"bad\",\"error\":\"invalid request (TypeError)\"}]", + "[\"mcp connect failed\",{\"server\":\"bad\",\"error\":\"invalid request (TypeError)\"}]" + ] +} diff --git a/packages/harness/test/__artifacts__/mcp.client.json b/packages/harness/test/__artifacts__/mcp.client.json index 79214f0f..9325486f 100644 --- a/packages/harness/test/__artifacts__/mcp.client.json +++ b/packages/harness/test/__artifacts__/mcp.client.json @@ -443,10 +443,10 @@ second line", ], }, "emptyCursor": { - "error": { - "message": "tools/list: exceeded listMaxPages (64); server pagination did not terminate", - "tag": "Mcp.RequestError", - }, + "tools": [ + "first", + "second", + ], }, "nullCursor": { "error": { diff --git a/packages/harness/test/fixtures/mcp/server.mjs b/packages/harness/test/fixtures/mcp/server.mjs index 1138d97f..bb96b9ba 100644 --- a/packages/harness/test/fixtures/mcp/server.mjs +++ b/packages/harness/test/fixtures/mcp/server.mjs @@ -1,17 +1,20 @@ // Stdio MCP fixture server built on the SDK; run as `node server.mjs`. -// MCP_FIXTURE picks the variant: unset (tools with listChanged), "plain" (tools without listChanged) -// or "resources" (resources only, paginated, without a templates/list handler). +// MCP_FIXTURE picks the variant: unset (tools with listChanged), "plain" (tools without listChanged), +// "resources" (resources only, paginated, without a templates/list handler) or "blank" (tools, resources +// and templates in two pages each, the second ending with a blank cursor). import { Server } from "@modelcontextprotocol/server"; import { StdioServerTransport } from "@modelcontextprotocol/server/stdio"; import { install } from "./tools.mjs"; const variant = process.env.MCP_FIXTURE; const capabilities = - variant === "resources" - ? { resources: { listChanged: true } } - : variant === "plain" - ? { tools: {} } - : { tools: { listChanged: true }, logging: {} }; + variant === "blank" + ? { tools: {}, resources: {} } + : variant === "resources" + ? { resources: { listChanged: true } } + : variant === "plain" + ? { tools: {} } + : { tools: { listChanged: true }, logging: {} }; const server = new Server( { name: "stdio-fixture", version: "1.0.0" }, { capabilities, instructions: " Use the echo tool. \n" }, @@ -28,6 +31,25 @@ if (variant === "resources") { if (uri === "fixture://changed") await server.sendResourceListChanged(); return { contents: [{ uri, mimeType: "text/plain", text: `contents of ${uri}` }] }; }); +} else if (variant === "blank") { + const paged = (first, second) => (request) => + request.params?.cursor === "page-2" ? { ...second, nextCursor: "" } : { ...first, nextCursor: "page-2" }; + const tool = (name) => ({ name, inputSchema: { type: "object" } }); + server.setRequestHandler("tools/list", paged({ tools: [tool("first")] }, { tools: [tool("second")] })); + server.setRequestHandler( + "resources/list", + paged( + { resources: [{ uri: "fixture://one", name: "one" }] }, + { resources: [{ uri: "fixture://two", name: "two" }] }, + ), + ); + server.setRequestHandler( + "resources/templates/list", + paged( + { resourceTemplates: [{ uriTemplate: "fixture://one/{id}", name: "one" }] }, + { resourceTemplates: [{ uriTemplate: "fixture://two/{id}", name: "two" }] }, + ), + ); } else install(server); // A message sent right after the handshake, before the client has listed anything. diff --git a/packages/harness/test/mcp.client.test.ts b/packages/harness/test/mcp.client.test.ts index ebd30920..684f9aa1 100644 --- a/packages/harness/test/mcp.client.test.ts +++ b/packages/harness/test/mcp.client.test.ts @@ -681,8 +681,8 @@ describe("mcp client", () => { tag: "Mcp.RequestError", message: "Invalid result for tools/list: nextCursor: Invalid input: expected string, received null", }); - // An empty cursor is followed as a real one, so the pages cycle until the SDK's cap. - expect(outcome(emptyCursor).error?.message).toContain("exceeded listMaxPages"); + // An empty cursor ends the listing, as the absent one would. + expect(outcome(emptyCursor)).toEqual({ tools: ["first", "second"] }); // The SDK stops quietly at a repeated cursor and returns the pages it has. expect(outcome(duplicate)).toEqual({ tools: ["t-start", "t-again"] }); for (const run of [emptyCursor, duplicate]) expect(run.elapsed).toBeLessThan(5_000); diff --git a/packages/harness/test/mcp.followups.test.ts b/packages/harness/test/mcp.followups.test.ts new file mode 100644 index 00000000..f02a7ac7 --- /dev/null +++ b/packages/harness/test/mcp.followups.test.ts @@ -0,0 +1,104 @@ +import "./utils/env.ts"; +import { Effect, Logger } from "effect"; +import fs from "node:fs/promises"; +import { join } from "node:path"; +import { expect, it } from "vite-plus/test"; +import { Harness } from "../src/effect/harness.ts"; +import { Session } from "../src/effect/session.ts"; +import { Mcp } from "../src/mcp/mcp.ts"; +import { defaultPromptPlugin } from "../src/plugin/builtin/prompt/default.ts"; +import { listen, raw } from "./fixtures/mcp/http.ts"; +import { results, scripted, statusEvents, stdio, write } from "./fixtures/mcp/turns.ts"; +import { tmpdir } from "./fixtures/tempdir.ts"; +import { pendingCall } from "./tools.fixture.ts"; + +it("mcp: a blank cursor ends every list, and a header fetch rejects is reported without its name or value", async () => { + await using dir = await tmpdir(); + const home = join(dir.path, "home"); + const work = join(dir.path, "work"); + await fs.mkdir(work, { recursive: true }); + + const remote = raw(); + const warnings: string[] = []; + const collector = Logger.make(({ logLevel, message }) => { + if (logLevel === "Warn") warnings.push(JSON.stringify(message)); + }); + const model = scripted(); + const result = await Effect.gen(function* () { + const url = yield* listen(remote.handler); + yield* Effect.promise(() => + write(join(home, "mcp.json"), { + mcpServers: { + paged: stdio({}, { MCP_FIXTURE: "blank" }, "server.mjs"), + // fetch rejects the name with a TypeError that carries no system code. + bad: { url: url.href, headers: { "bad name": "s3cret-value" } }, + }, + }), + ); + const session = yield* Session.create({ directory: work, model: { provider: "openai", id: "gpt-5.6-luna" } }); + const main = session.main; + const seen = yield* statusEvents((threadId) => (threadId === main.id ? "main" : threadId)); + model.turn(main.id, [ + pendingCall("list_mcp_resources", { server: "paged" }, "resources-page"), + pendingCall("list_mcp_resources", { server: "paged", cursor: "page-2" }, "resources-next"), + pendingCall("list_mcp_resources", {}, "resources-all"), + pendingCall("list_mcp_resource_templates", { server: "paged" }, "templates-page"), + pendingCall("list_mcp_resource_templates", { server: "paged", cursor: "page-2" }, "templates-next"), + pendingCall("list_mcp_resource_templates", {}, "templates-all"), + pendingCall("list_mcp_resources", { server: "bad" }, "bad-resources"), + ]); + yield* main.run("List."); + const status = yield* Mcp.Service.use((mcp) => mcp.status(main.id)); + const log = yield* Effect.promise(() => fs.readFile(join(home, "mcp.log"), "utf8").catch(() => "")); + return { + declared: model.declared(), + calls: yield* results(main), + status, + events: seen.read(), + // Timestamps vary; the rest of each line is fixed. + log: log + .split("\n") + .filter(Boolean) + .map((line) => line.replace(/^\S+ /, "")), + requests: JSON.stringify(model.requests), + }; + }).pipe( + Effect.provide( + Harness.layer({ + home, + hostCwd: dir.path, + database: ":memory:", + plugins: [defaultPromptPlugin], + llm: model.open, + }), + ), + Effect.scoped, + Effect.provide(Logger.layer([collector])), + Effect.runPromise, + ); + + const json = (id: string) => JSON.parse(result.calls[id] ?? "null") as Record; + expect(result.declared.filter((name) => name.startsWith("mcp__paged__"))).toEqual([ + "mcp__paged__first", + "mcp__paged__second", + ]); + expect(json("resources-page").nextCursor).toBe("page-2"); + expect(json("resources-next")).not.toHaveProperty("nextCursor"); + expect(json("resources-all")).not.toHaveProperty("errors"); + expect(json("templates-next")).not.toHaveProperty("nextCursor"); + expect(json("templates-all")).not.toHaveProperty("errors"); + + const bad = result.status.find((one) => one.server === "bad"); + expect(bad).toMatchObject({ state: "failed", error: "invalid request (TypeError)" }); + // The request never left the client. + expect(remote.seen).toEqual([]); + const { requests, ...rest } = result; + for (const text of [JSON.stringify(rest), requests, ...warnings]) { + expect(text).not.toContain("s3cret"); + expect(text).not.toContain("bad name"); + } + const badWarnings = warnings.filter((line) => line.includes('"bad"')); + await expect(`${JSON.stringify({ ...rest, warnings: badWarnings }, null, "\t")}\n`).toMatchFileSnapshot( + "./__artifacts__/mcp-review-followups.json", + ); +}, 60_000);