All four findings taken (one visible, three in the suppressed block), plus the
`excludedTools` sibling leak the round-1 reply had deferred:
- `rawItemsOf` read a missing or non-array list member as an empty page, so a
top-level violation (`{ tools: "invalid" }`) on a later page was discarded
and the call returned a silently truncated list. It now returns `undefined`
for "not a list at all", and the walk rethrows the original strict error.
- A failure of the re-walk ITSELF (a malformed `nextCursor`, a connection lost
mid-walk) escaped in place of the original error, whose response is the one
`rejectedResponseId` was captured for. The walk is now wrapped: the original
surfaces, the secondary is logged so it stays diagnosable.
- The exclusion scan's lenient fallback discarded its malformed entries despite
the comment claiming they were reported. Observable when the aggregate is
served from cache: the salvage never runs, so nothing else reports them. The
scan now returns them and `refreshExcludedTools` reports them — only ever
adding, so a clean scan can't clear what the salvage path just wrote.
- An empty-string cursor is a valid cursor, and `cursor ? {cursor} : {}` dropped
it, re-requesting page one and duplicating its entries until the
repeated-cursor guard fired. Both walks now test `!== undefined`.
- `excludedTools` had the same session leak fixed for `malformedListItems` last
round: cleared only on an explicit `disconnect()`, so the `onerror`-with-no-
`onclose` route carried the previous server's set into the next connection.
Cleared on the start-clean path too — safe because every `listAllTools`
recomputes it, so this can only drop a stale set.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HDdo1rNRVQnVRRdsqSrvG3
Signed-off-by: cliffhall <cliff@futurescale.com>
197 lines
7.2 KiB
TypeScript
197 lines
7.2 KiB
TypeScript
import { describe, it, expect, afterEach, vi } from "vitest";
|
|
import { InspectorClient } from "@inspector/core/mcp/inspectorClient.js";
|
|
import { createTransportNode } from "@inspector/core/mcp/node/transport.js";
|
|
import { eraToVersionNegotiation } from "@inspector/core/mcp/types.js";
|
|
import type { ExcludedTool } from "@inspector/core/mcp/types.js";
|
|
import {
|
|
createTestServerHttp,
|
|
type TestServerHttp,
|
|
createTestServerInfo,
|
|
createEchoTool,
|
|
createGetWeatherTool,
|
|
createInvalidHeaderTool,
|
|
} from "@modelcontextprotocol/inspector-test-server";
|
|
import type { ServerConfig } from "@modelcontextprotocol/inspector-test-server";
|
|
|
|
/**
|
|
* Live coverage of the SEP-2243 excluded-tools surface (#1632). A modern
|
|
* Streamable HTTP client MUST drop a tool whose `x-mcp-header` annotation
|
|
* violates the spec; the SDK does so silently. `refreshExcludedTools()` re-lists
|
|
* the RAW (unfiltered) `tools/list` and reports the dropped tools with the
|
|
* constraint they broke, so the Tools tab can show *why* a tool vanished.
|
|
*/
|
|
describe("excluded tools (SEP-2243 x-mcp-header)", () => {
|
|
let client: InspectorClient | null = null;
|
|
let server: TestServerHttp | null = null;
|
|
|
|
afterEach(async () => {
|
|
if (client) {
|
|
try {
|
|
await client.disconnect();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
client = null;
|
|
}
|
|
if (server) {
|
|
try {
|
|
await server.stop();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
server = null;
|
|
}
|
|
});
|
|
|
|
async function start(
|
|
modern: ServerConfig["modern"] | undefined,
|
|
maxPageSize?: number,
|
|
): Promise<TestServerHttp> {
|
|
const started = createTestServerHttp({
|
|
serverInfo: createTestServerInfo("excluded-tools-test", "1.0.0"),
|
|
// A valid tool, a valid-header tool, and an invalid-header tool.
|
|
tools: [
|
|
createEchoTool(),
|
|
createGetWeatherTool(),
|
|
createInvalidHeaderTool(),
|
|
],
|
|
...(modern ? { modern } : {}),
|
|
...(maxPageSize ? { maxPageSize: { tools: maxPageSize } } : {}),
|
|
});
|
|
await started.start();
|
|
server = started;
|
|
return started;
|
|
}
|
|
|
|
async function connect(
|
|
url: string,
|
|
era: "legacy" | "modern",
|
|
): Promise<InspectorClient> {
|
|
const connected = new InspectorClient(
|
|
{ type: "streamable-http", url },
|
|
{
|
|
environment: { transport: createTransportNode },
|
|
versionNegotiation: eraToVersionNegotiation(era),
|
|
},
|
|
);
|
|
await connected.connect();
|
|
client = connected;
|
|
return connected;
|
|
}
|
|
|
|
it("excludes the invalid-header tool from the managed list on a modern connection", async () => {
|
|
const started = await start({});
|
|
const connected = await connect(started.url, "modern");
|
|
|
|
const { tools } = await connected.listAllTools();
|
|
const names = tools.map((t) => t.name);
|
|
expect(names).toContain("echo");
|
|
expect(names).toContain("get_weather");
|
|
// The SDK drops the invalid-header tool from the aggregated list.
|
|
expect(names).not.toContain("invalid_header_tool");
|
|
});
|
|
|
|
it("surfaces the excluded tool with its reason after listAllTools (modern)", async () => {
|
|
const started = await start({});
|
|
const connected = await connect(started.url, "modern");
|
|
|
|
const events: ExcludedTool[][] = [];
|
|
connected.addEventListener("excludedToolsChange", (e) => {
|
|
events.push(e.detail);
|
|
});
|
|
|
|
// listAllTools recomputes excluded tools as a side effect.
|
|
await connected.listAllTools();
|
|
|
|
const excluded = connected.getExcludedTools();
|
|
expect(excluded.map((x) => x.tool.name)).toEqual(["invalid_header_tool"]);
|
|
expect(excluded[0]?.reason).toContain("RFC 9110 token");
|
|
// The change event fired with the same set.
|
|
expect(events.at(-1)?.map((x) => x.tool.name)).toEqual([
|
|
"invalid_header_tool",
|
|
]);
|
|
});
|
|
|
|
it("refreshExcludedTools returns the excluded set directly (modern)", async () => {
|
|
const started = await start({});
|
|
const connected = await connect(started.url, "modern");
|
|
|
|
const excluded = await connected.refreshExcludedTools();
|
|
expect(excluded.map((x) => x.tool.name)).toEqual(["invalid_header_tool"]);
|
|
});
|
|
|
|
it("does not carry an excluded set into the next connection (#1909)", async () => {
|
|
// `resetSessionState()` is the start-clean path, and it is the ONLY one the
|
|
// route out that tears down nothing (`onerror` with no `onclose`) reaches.
|
|
// A set left behind names tools of the PREVIOUS server — and would persist
|
|
// indefinitely against a server that never lists tools again.
|
|
const started = await start({});
|
|
const connected = await connect(started.url, "modern");
|
|
await connected.listAllTools();
|
|
expect(connected.getExcludedTools()).toHaveLength(1);
|
|
|
|
const events: number[] = [];
|
|
connected.addEventListener("excludedToolsChange", (e) => {
|
|
events.push(e.detail.length);
|
|
});
|
|
// Reconnecting runs the start-clean reset; assert before anything re-lists.
|
|
await connected.disconnect();
|
|
await connected.connect();
|
|
|
|
expect(connected.getExcludedTools()).toEqual([]);
|
|
expect(events).toContain(0);
|
|
});
|
|
|
|
it("reports no excluded tools on a legacy connection (no exclusion there)", async () => {
|
|
const started = await start(undefined);
|
|
const connected = await connect(started.url, "legacy");
|
|
|
|
// Legacy servers don't exclude — the invalid-header tool stays in the list.
|
|
const { tools } = await connected.listAllTools();
|
|
expect(tools.map((t) => t.name)).toContain("invalid_header_tool");
|
|
expect(connected.getExcludedTools()).toEqual([]);
|
|
expect(await connected.refreshExcludedTools()).toEqual([]);
|
|
});
|
|
|
|
it("clears excluded tools on disconnect", async () => {
|
|
const started = await start({});
|
|
const connected = await connect(started.url, "modern");
|
|
await connected.listAllTools();
|
|
expect(connected.getExcludedTools().length).toBe(1);
|
|
|
|
await connected.disconnect();
|
|
client = null;
|
|
expect(connected.getExcludedTools()).toEqual([]);
|
|
});
|
|
|
|
it("walks every raw page to find the excluded tool (pagination)", async () => {
|
|
// Page size 1 → the raw `tools/list` walk spans three pages; the invalid
|
|
// tool is on the last one, so this exercises the cursor loop.
|
|
const started = await start({}, 1);
|
|
const connected = await connect(started.url, "modern");
|
|
|
|
const excluded = await connected.refreshExcludedTools();
|
|
expect(excluded.map((x) => x.tool.name)).toEqual(["invalid_header_tool"]);
|
|
});
|
|
|
|
it("listAllTools still returns tools if the excluded recompute throws", async () => {
|
|
const started = await start({});
|
|
const connected = await connect(started.url, "modern");
|
|
// The excluded-tools recompute is best-effort: a failure must not fail the
|
|
// tools list itself.
|
|
vi.spyOn(connected, "refreshExcludedTools").mockRejectedValueOnce(
|
|
new Error("boom"),
|
|
);
|
|
const { tools } = await connected.listAllTools();
|
|
expect(tools.map((t) => t.name)).toContain("echo");
|
|
});
|
|
|
|
it("is a no-op returning [] before connect (not modern)", async () => {
|
|
const fresh = new InspectorClient(
|
|
{ type: "streamable-http", url: "http://localhost:1/never" },
|
|
{ environment: { transport: createTransportNode } },
|
|
);
|
|
// No round trip: the gate is false (era not modern), so it returns [].
|
|
expect(await fresh.refreshExcludedTools()).toEqual([]);
|
|
});
|
|
});
|