Skip to content
Merged
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
4 changes: 4 additions & 0 deletions packages/harness/src/mcp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -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);
Expand Down
76 changes: 76 additions & 0 deletions packages/harness/test/__artifacts__/mcp-review-followups.json
Original file line number Diff line number Diff line change
@@ -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)\"}]"
]
}
8 changes: 4 additions & 4 deletions packages/harness/test/__artifacts__/mcp.client.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
36 changes: 29 additions & 7 deletions packages/harness/test/fixtures/mcp/server.mjs
Original file line number Diff line number Diff line change
@@ -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" },
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions packages/harness/test/mcp.client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
104 changes: 104 additions & 0 deletions packages/harness/test/mcp.followups.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>;
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);
Loading