Merge remote-tracking branch 'origin/v2/main' into v2/fix/2172-oidc-discovery-compat
Signed-off-by: cliffhall <cliff@futurescale.com> # Conflicts: # README.md # core/mcp/inspectorClient.ts # test-servers/src/test-server-oauth.ts
This commit is contained in:
commit
2eeab05d00
5 changed files with 8524 additions and 1598 deletions
272
clients/web/src/test/integration/auth/revocation-e2e.test.ts
Normal file
272
clients/web/src/test/integration/auth/revocation-e2e.test.ts
Normal file
|
|
@ -0,0 +1,272 @@
|
|||
/**
|
||||
* RFC 7009 revocation against the real OAuth test server (#2144).
|
||||
*
|
||||
* The unit suite (`src/test/core/auth/revocation.test.ts`) pins what is *sent*;
|
||||
* this pins what the request actually *does*. The two halves that only a real
|
||||
* authorization server can show are both here: that the Inspector's request is
|
||||
* accepted at all (its client authentication and form encoding are right), and
|
||||
* that §2.1's grant linkage holds — one request naming the refresh token kills
|
||||
* the access token issued alongside it, which is the whole reason the Inspector
|
||||
* sends one request rather than two.
|
||||
*/
|
||||
import { describe, it, expect, beforeAll, afterAll } from "vitest";
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
TestServerHttp,
|
||||
createOAuthTestServerConfig,
|
||||
getDefaultServerConfig,
|
||||
waitForOAuthWellKnown,
|
||||
} from "@modelcontextprotocol/inspector-test-server";
|
||||
// NOT `BrowserOAuthStorage`: the `integration` project runs in the node
|
||||
// environment, where `sessionStorage` does not exist. Node 22 gates Web Storage
|
||||
// behind a flag and newer Node exposes it by default, so a browser-backed store
|
||||
// passes on a developer's machine and fails in CI — which is exactly what
|
||||
// happened here.
|
||||
import { NodeOAuthStorage } from "@inspector/core/auth/node/storage-node.js";
|
||||
import {
|
||||
clearAndPlanRevocation,
|
||||
executeOAuthRevocation,
|
||||
} from "@inspector/core/auth/revocation.js";
|
||||
import type { TokenRevocationOutcome } from "@inspector/core/auth/revocation.js";
|
||||
import type { OAuthMetadata } from "@modelcontextprotocol/client";
|
||||
|
||||
const CLIENT_ID = "test-2144-revocation";
|
||||
const CLIENT_SECRET = "test-2144-secret";
|
||||
const REDIRECT_URL = "http://localhost:3000/oauth/callback";
|
||||
/** A second registered client, used to prove tokens are not cross-revocable. */
|
||||
const OTHER_CLIENT_ID = "test-2144-other";
|
||||
const OTHER_CLIENT_SECRET = "test-2144-other-secret";
|
||||
|
||||
function base64Url(buffer: Buffer): string {
|
||||
return buffer
|
||||
.toString("base64")
|
||||
.replace(/\+/g, "-")
|
||||
.replace(/\//g, "_")
|
||||
.replace(/=/g, "");
|
||||
}
|
||||
|
||||
describe("OAuth token revocation (RFC 7009)", () => {
|
||||
let mcpServer: TestServerHttp | null = null;
|
||||
let serverUrl = "";
|
||||
let storageDir = "";
|
||||
let metadata: OAuthMetadata;
|
||||
|
||||
beforeAll(async () => {
|
||||
mcpServer = new TestServerHttp({
|
||||
...getDefaultServerConfig(),
|
||||
serverType: "streamable-http" as const,
|
||||
...createOAuthTestServerConfig({
|
||||
requireAuth: true,
|
||||
staticClients: [
|
||||
{
|
||||
clientId: CLIENT_ID,
|
||||
clientSecret: CLIENT_SECRET,
|
||||
redirectUris: [REDIRECT_URL],
|
||||
},
|
||||
{
|
||||
clientId: OTHER_CLIENT_ID,
|
||||
clientSecret: OTHER_CLIENT_SECRET,
|
||||
redirectUris: [REDIRECT_URL],
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
const port = await mcpServer.start();
|
||||
serverUrl = `http://localhost:${port}`;
|
||||
storageDir = mkdtempSync(join(tmpdir(), "mcp-inspector-2144-"));
|
||||
await waitForOAuthWellKnown(serverUrl);
|
||||
metadata = (await (
|
||||
await fetch(`${serverUrl}/.well-known/oauth-authorization-server`)
|
||||
).json()) as OAuthMetadata;
|
||||
}, 30_000);
|
||||
|
||||
afterAll(async () => {
|
||||
await mcpServer?.stop();
|
||||
mcpServer = null;
|
||||
if (storageDir) {
|
||||
rmSync(storageDir, { recursive: true, force: true });
|
||||
storageDir = "";
|
||||
}
|
||||
}, 30_000);
|
||||
|
||||
/** Run a real authorization-code exchange and return the issued tokens. */
|
||||
async function authorize(): Promise<{
|
||||
access_token: string;
|
||||
refresh_token: string;
|
||||
}> {
|
||||
const verifier = base64Url(randomBytes(32));
|
||||
const challenge = base64Url(createHash("sha256").update(verifier).digest());
|
||||
|
||||
const authorizeResponse = await fetch(`${serverUrl}/oauth/authorize`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
redirect: "manual",
|
||||
body: new URLSearchParams({
|
||||
client_id: CLIENT_ID,
|
||||
redirect_uri: REDIRECT_URL,
|
||||
response_type: "code",
|
||||
scope: "mcp",
|
||||
code_challenge: challenge,
|
||||
code_challenge_method: "S256",
|
||||
}),
|
||||
});
|
||||
const location = authorizeResponse.headers.get("location");
|
||||
expect(location).toBeTruthy();
|
||||
const code = new URL(location!).searchParams.get("code");
|
||||
expect(code).toBeTruthy();
|
||||
|
||||
const tokenResponse = await fetch(`${serverUrl}/oauth/token`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({
|
||||
grant_type: "authorization_code",
|
||||
code: code!,
|
||||
redirect_uri: REDIRECT_URL,
|
||||
client_id: CLIENT_ID,
|
||||
client_secret: CLIENT_SECRET,
|
||||
code_verifier: verifier,
|
||||
}),
|
||||
});
|
||||
expect(tokenResponse.status).toBe(200);
|
||||
return (await tokenResponse.json()) as {
|
||||
access_token: string;
|
||||
refresh_token: string;
|
||||
};
|
||||
}
|
||||
|
||||
/** Whether the MCP endpoint still accepts this bearer token. */
|
||||
async function tokenAccepted(accessToken: string): Promise<boolean> {
|
||||
const response = await fetch(`${serverUrl}/mcp`, {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
return response.status !== 401;
|
||||
}
|
||||
|
||||
async function seededStorage(tokens: {
|
||||
access_token: string;
|
||||
refresh_token?: string;
|
||||
}): Promise<NodeOAuthStorage> {
|
||||
const storage = new NodeOAuthStorage(join(storageDir, "oauth.json"));
|
||||
await storage.clear(serverUrl);
|
||||
// Issuer-bound and matching the discovered metadata: an unkeyed grant
|
||||
// records no authorization server and is deliberately refused.
|
||||
await storage.saveTokens(
|
||||
serverUrl,
|
||||
{ token_type: "Bearer", ...tokens },
|
||||
{ issuer: metadata.issuer },
|
||||
);
|
||||
await storage.saveServerMetadata(serverUrl, metadata);
|
||||
// Preconfigured, so it goes in the preregistered slot — the same one a
|
||||
// server with `oauth.clientId` uses. That is the slot the revocation path
|
||||
// must read first, or a confidential client sends no authentication at all.
|
||||
await storage.savePreregisteredClientInformation(serverUrl, {
|
||||
client_id: CLIENT_ID,
|
||||
client_secret: CLIENT_SECRET,
|
||||
});
|
||||
return storage;
|
||||
}
|
||||
|
||||
// The fixture enforces RFC 7009 §2.1 client authentication, so this is what
|
||||
// makes the passing cases below mean something: a request with the wrong
|
||||
// credentials is refused, and the endpoint is not simply answering 200 to
|
||||
// anything.
|
||||
it("refuses a revocation request with the wrong client secret", async () => {
|
||||
const response = await fetch(`${serverUrl}/oauth/revoke`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Authorization: `Basic ${Buffer.from(`${CLIENT_ID}:wrong`).toString("base64")}`,
|
||||
},
|
||||
body: new URLSearchParams({ token: "anything" }),
|
||||
});
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
|
||||
it("refuses a revocation request naming no client at all", async () => {
|
||||
const response = await fetch(`${serverUrl}/oauth/revoke`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({ token: "anything" }),
|
||||
});
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
|
||||
// RFC 7009 §2.1: only the client a token was issued to may revoke it. The
|
||||
// response stays 200 (§2.2 — it must not tell one client whether another's
|
||||
// token exists), so the assertion is that the token still works.
|
||||
it("does not revoke a token belonging to a different client", async () => {
|
||||
const tokens = await authorize();
|
||||
const response = await fetch(`${serverUrl}/oauth/revoke`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Authorization: `Basic ${Buffer.from(`${OTHER_CLIENT_ID}:${OTHER_CLIENT_SECRET}`).toString("base64")}`,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
token: tokens.refresh_token,
|
||||
token_type_hint: "refresh_token",
|
||||
}),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(await tokenAccepted(tokens.access_token)).toBe(true);
|
||||
});
|
||||
|
||||
/**
|
||||
* The caller's real sequence — take-and-clear atomically, then send — so this
|
||||
* exercises the ordering the product uses rather than a convenience wrapper.
|
||||
*/
|
||||
async function clearAndRevoke(
|
||||
storage: NodeOAuthStorage,
|
||||
): Promise<TokenRevocationOutcome> {
|
||||
const plan = await clearAndPlanRevocation({ serverUrl, storage });
|
||||
return executeOAuthRevocation(plan, { fetchFn: fetch });
|
||||
}
|
||||
|
||||
it("advertises a revocation endpoint", () => {
|
||||
expect(metadata.revocation_endpoint).toBe(`${serverUrl}/oauth/revoke`);
|
||||
});
|
||||
|
||||
// The failure this feature exists to fix: without the request, the token is
|
||||
// still accepted after the Inspector has forgotten it.
|
||||
it("revokes the whole grant from the stored refresh token", async () => {
|
||||
const tokens = await authorize();
|
||||
expect(await tokenAccepted(tokens.access_token)).toBe(true);
|
||||
|
||||
const outcome = await clearAndRevoke(await seededStorage(tokens));
|
||||
|
||||
expect(outcome).toMatchObject({
|
||||
status: "revoked",
|
||||
tokenTypeHint: "refresh_token",
|
||||
});
|
||||
// RFC 7009 §2.1 — the access token issued under the same grant dies too,
|
||||
// which is why one request is enough.
|
||||
expect(await tokenAccepted(tokens.access_token)).toBe(false);
|
||||
});
|
||||
|
||||
it("revokes an access token when no refresh token was issued", async () => {
|
||||
const tokens = await authorize();
|
||||
const outcome = await clearAndRevoke(
|
||||
await seededStorage({ access_token: tokens.access_token }),
|
||||
);
|
||||
|
||||
expect(outcome).toMatchObject({
|
||||
status: "revoked",
|
||||
tokenTypeHint: "access_token",
|
||||
});
|
||||
expect(await tokenAccepted(tokens.access_token)).toBe(false);
|
||||
});
|
||||
|
||||
// RFC 7009 §2.2: an unknown token is a success, so revoking a grant the
|
||||
// server has already expired must not be reported as a failure.
|
||||
it("treats an already-unknown token as revoked", async () => {
|
||||
const storage = await seededStorage({ access_token: "never-issued" });
|
||||
await expect(clearAndRevoke(storage)).resolves.toMatchObject({
|
||||
status: "revoked",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -20,6 +20,7 @@ import {
|
|||
createMrtrEdgeCaseTool,
|
||||
loadConfig,
|
||||
resolveConfig,
|
||||
type ToolDefinition,
|
||||
} from "@modelcontextprotocol/inspector-test-server";
|
||||
import type { ServerConfig } from "@modelcontextprotocol/inspector-test-server";
|
||||
import type {
|
||||
|
|
@ -437,6 +438,146 @@ describe("modern-era negotiation (2026-07-28)", () => {
|
|||
expect(result.result!.isError).toBeFalsy();
|
||||
});
|
||||
|
||||
// #2140: on the 2026-07-28 era, closing the request's own response stream IS
|
||||
// the cancellation signal for Streamable HTTP; `notifications/cancelled` is
|
||||
// the stdio mechanism and the spec says it is neither required nor expected
|
||||
// here. The SDK implements that fork off `transport.hasPerRequestStream` —
|
||||
// but every Inspector connection is wrapped in `MessageTrackingTransport`,
|
||||
// which was not forwarding it, so all three clients POSTed the notification
|
||||
// and a spec-compliant server acknowledged it `202` and kept running.
|
||||
//
|
||||
// This asserts at the far end, on the server's own request signal: nothing on
|
||||
// the client side distinguishes a cancel that reached the server from one
|
||||
// that was dropped, which is exactly how this shipped.
|
||||
it("cancels by aborting the request's stream, which the server observes", async () => {
|
||||
let sawAbort!: (value: boolean) => void;
|
||||
const aborted = new Promise<boolean>((resolve) => {
|
||||
sawAbort = resolve;
|
||||
});
|
||||
let started!: () => void;
|
||||
const running = new Promise<void>((resolve) => {
|
||||
started = resolve;
|
||||
});
|
||||
|
||||
const slowServer = createTestServerHttp({
|
||||
serverInfo: createTestServerInfo("cancel-era-test", "1.0.0"),
|
||||
tools: [
|
||||
{
|
||||
name: "slow_task",
|
||||
description: "Runs until the client cancels it",
|
||||
handler: async (_params, _context, extra) => {
|
||||
extra?.signal?.addEventListener("abort", () => sawAbort(true), {
|
||||
once: true,
|
||||
});
|
||||
started();
|
||||
// Never settles on its own, so a regression is a timeout rather
|
||||
// than a pass.
|
||||
return new Promise(() => {});
|
||||
},
|
||||
},
|
||||
],
|
||||
modern: {},
|
||||
});
|
||||
await slowServer.start();
|
||||
server = slowServer;
|
||||
|
||||
const connected = await connectWithEra(slowServer.url, "modern");
|
||||
const { tools } = await connected.listTools();
|
||||
const tool = tools.find((t) => t.name === "slow_task");
|
||||
|
||||
// Hold the rejection immediately so it is never seen as unhandled while we
|
||||
// wait on the server side.
|
||||
const settled = connected.callTool(tool!, {}).catch((err: unknown) => err);
|
||||
await running;
|
||||
expect(connected.cancelToolCall()).toBe(true);
|
||||
|
||||
await expect(settled).resolves.toBeInstanceOf(ToolCallCancelledError);
|
||||
await expect(aborted).resolves.toBe(true);
|
||||
}, 30_000);
|
||||
|
||||
// The test above builds its own never-returning tool, which is the sharpest
|
||||
// way to assert on the server's abort signal but says nothing about the
|
||||
// artifacts a human actually uses. This one drives the documented showcase
|
||||
// through the SAME path they would — the checked-in config, resolved through
|
||||
// the preset registry — so a misspelt preset name, a config naming a dead
|
||||
// preset, or a regression in the `slow_task` handler's own abort loop fails
|
||||
// here rather than only when someone runs the repro by hand.
|
||||
//
|
||||
// It observes the handler's own return value, not the client's view. Counting
|
||||
// progress notifications would be a false negative: the SDK drops a cancelled
|
||||
// request's progress handler locally, so the client stops seeing ticks the
|
||||
// moment it cancels whether or not the server ever stopped working — the very
|
||||
// silence this bug hid behind.
|
||||
it("stops the showcase tool's work on cancel (slow_task, via the showcase config)", async () => {
|
||||
const config = loadConfig(
|
||||
join(repoRoot, "test-servers/configs/cancellation-modern-http.json"),
|
||||
);
|
||||
expect(config.tools).toContainEqual({ preset: "slow_task" });
|
||||
|
||||
const resolved = resolveConfig(config);
|
||||
expect(resolved.tools?.map((tool) => tool.name)).toEqual([
|
||||
"slow_task",
|
||||
"echo",
|
||||
]);
|
||||
|
||||
// Wrap the resolved preset rather than replacing it, so what runs is the
|
||||
// real registered handler and only its outcome is observed. `tools` is a
|
||||
// union with the task-tool shape, whose handler has a different signature,
|
||||
// so narrow to the plain one this preset actually is.
|
||||
const preset = resolved.tools!.find(
|
||||
(tool): tool is ToolDefinition => tool.name === "slow_task",
|
||||
)!;
|
||||
let reportOutcome!: (text: string) => void;
|
||||
const outcome = new Promise<string>((resolve) => {
|
||||
reportOutcome = resolve;
|
||||
});
|
||||
let firstTick!: () => void;
|
||||
const running = new Promise<void>((resolve) => {
|
||||
firstTick = resolve;
|
||||
});
|
||||
|
||||
const started = createTestServerHttp({
|
||||
...resolved,
|
||||
tools: resolved.tools!.map((tool) =>
|
||||
tool.name === "slow_task"
|
||||
? {
|
||||
...preset,
|
||||
handler: async (params, context, extra) => {
|
||||
extra?.signal?.addEventListener("abort", () => firstTick(), {
|
||||
once: true,
|
||||
});
|
||||
const result = await preset.handler(params, context, extra);
|
||||
reportOutcome(JSON.stringify(result));
|
||||
return result;
|
||||
},
|
||||
}
|
||||
: tool,
|
||||
),
|
||||
});
|
||||
await started.start();
|
||||
server = started;
|
||||
const connected = await connectWithEra(started.url, "modern");
|
||||
|
||||
const { tools } = await connected.listTools();
|
||||
const tool = tools.find((t) => t.name === "slow_task");
|
||||
expect(tool).toBeDefined();
|
||||
|
||||
// Hold the rejection immediately so it is never seen as unhandled.
|
||||
const settled = connected
|
||||
.callTool(tool!, { seconds: 30 })
|
||||
.catch((err: unknown) => err);
|
||||
|
||||
// Let it get past its first tick, then cancel.
|
||||
await new Promise((resolve) => setTimeout(resolve, 1500));
|
||||
expect(connected.cancelToolCall()).toBe(true);
|
||||
await expect(settled).resolves.toBeInstanceOf(ToolCallCancelledError);
|
||||
await running;
|
||||
|
||||
// The handler must return promptly, saying it was cancelled — not run on to
|
||||
// its 30th second, which is what it did before the fix.
|
||||
await expect(outcome).resolves.toContain("cancelled after");
|
||||
}, 30_000);
|
||||
|
||||
it("cancels an in-flight MRTR call while its embedded request is pending", async () => {
|
||||
const started = await startMrtrServer(createMrtrTool());
|
||||
const connected = await connectWithEra(started.url, "modern");
|
||||
|
|
|
|||
|
|
@ -5447,8 +5447,12 @@ describe("InspectorClient", () => {
|
|||
expect(c.getOAuthFlowStep()).toBeUndefined();
|
||||
expect(c.getOAuthFlowState()).toBeUndefined();
|
||||
await expect(c.getOAuthState()).resolves.toBeUndefined();
|
||||
// clearOAuthTokens is a no-op when there is no manager
|
||||
await expect(c.clearOAuthTokens()).resolves.toBeUndefined();
|
||||
// clearOAuthTokens is a no-op when there is no manager; it still
|
||||
// reports an outcome so callers have one shape to read (#2144).
|
||||
await expect(c.clearOAuthTokens()).resolves.toEqual({
|
||||
status: "skipped",
|
||||
reason: "no_tokens",
|
||||
});
|
||||
});
|
||||
|
||||
it("setOAuthConfig throws when oauthManager is unset", () => {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,199 @@
|
|||
/**
|
||||
* #2140 — the Cancel button must send the transport's cancellation signal.
|
||||
*
|
||||
* On a 2026-07-28 Streamable HTTP connection the spec makes *closing the
|
||||
* request's SSE response stream* the cancellation signal, and says a
|
||||
* `notifications/cancelled` is neither required nor expected. The SDK
|
||||
* implements that fork itself: `Protocol.request` aborts a per-request
|
||||
* `AbortController` — handed to the transport as
|
||||
* `TransportSendOptions.requestSignal` — when the connection is modern **and**
|
||||
* the transport advertises `hasPerRequestStream`; otherwise it POSTs the
|
||||
* notification.
|
||||
*
|
||||
* The web client lost it at the proxy boundary. The real upstream transport
|
||||
* lives on the Node backend, and the browser's `RemoteClientTransport` neither
|
||||
* advertised the flag nor applied a `requestSignal` to its
|
||||
* `POST /api/mcp/send` — so every web cancel took the stdio branch, the server
|
||||
* answered the notification `202 Accepted` and dropped it, and the tool kept
|
||||
* running to completion. Only a manual disconnect actually cancelled anything.
|
||||
*
|
||||
* This drives the whole chain the browser drives — InspectorClient ->
|
||||
* RemoteClientTransport -> the Hono backend -> a real upstream Streamable HTTP
|
||||
* transport -> a modern test server — and asserts at the far end, on the
|
||||
* server's own request abort signal. Nothing shallower reaches it: each of the
|
||||
* three hops can drop the signal on its own, and a unit test of any one of them
|
||||
* passes while the chain stays broken.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, afterEach } from "vitest";
|
||||
import { serve } from "@hono/node-server";
|
||||
import type { ServerType } from "@hono/node-server";
|
||||
import { InspectorClient } from "@inspector/core/mcp/inspectorClient.js";
|
||||
import { createRemoteTransport } from "@inspector/core/mcp/remote/createRemoteTransport.js";
|
||||
import { createRemoteApp } from "@inspector/core/mcp/remote/node/server.js";
|
||||
import { ToolCallCancelledError } from "@inspector/core/mcp/toolCallCancelledError.js";
|
||||
import { eraToVersionNegotiation } from "@inspector/core/mcp/types.js";
|
||||
import {
|
||||
createTestServerHttp,
|
||||
createTestServerInfo,
|
||||
type TestServerHttp,
|
||||
type ToolDefinition,
|
||||
} from "@modelcontextprotocol/inspector-test-server";
|
||||
|
||||
function deferred<T>(): { promise: Promise<T>; resolve: (value: T) => void } {
|
||||
let resolve!: (value: T) => void;
|
||||
const promise = new Promise<T>((r) => {
|
||||
resolve = r;
|
||||
});
|
||||
return { promise, resolve };
|
||||
}
|
||||
|
||||
/**
|
||||
* A tool that never returns on its own. It reports when it started, and whether
|
||||
* the server-side request signal — which the SDK aborts when the client closes
|
||||
* this request's response stream — ever fired.
|
||||
*/
|
||||
function createSlowTool(): {
|
||||
tool: ToolDefinition;
|
||||
started: Promise<void>;
|
||||
aborted: Promise<boolean>;
|
||||
} {
|
||||
const started = deferred<void>();
|
||||
const aborted = deferred<boolean>();
|
||||
const tool: ToolDefinition = {
|
||||
name: "slow_task",
|
||||
description: "Runs until the client cancels it",
|
||||
handler: async (_params, _context, extra) => {
|
||||
extra?.signal?.addEventListener("abort", () => aborted.resolve(true), {
|
||||
once: true,
|
||||
});
|
||||
started.resolve();
|
||||
// Never settles: this call ends by cancellation or not at all, so a
|
||||
// regression shows up as the test's own timeout rather than as a pass.
|
||||
return new Promise(() => {});
|
||||
},
|
||||
};
|
||||
return { tool, started: started.promise, aborted: aborted.promise };
|
||||
}
|
||||
|
||||
async function startRemoteBackend(): Promise<{
|
||||
baseUrl: string;
|
||||
server: ServerType;
|
||||
authToken: string;
|
||||
}> {
|
||||
const { app, authToken } = createRemoteApp({
|
||||
initialConfig: { defaultEnvironment: {} },
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = serve(
|
||||
{ fetch: app.fetch, port: 0, hostname: "127.0.0.1" },
|
||||
(info) => {
|
||||
const port =
|
||||
info && typeof info === "object" && "port" in info
|
||||
? (info as { port: number }).port
|
||||
: 0;
|
||||
resolve({ baseUrl: `http://127.0.0.1:${port}`, server, authToken });
|
||||
},
|
||||
);
|
||||
server.on("error", reject);
|
||||
});
|
||||
}
|
||||
|
||||
describe("web-client cancellation over a per-request stream (#2140)", () => {
|
||||
let client: InspectorClient | null = null;
|
||||
let backend: ServerType | null = null;
|
||||
let upstream: TestServerHttp | null = null;
|
||||
|
||||
afterEach(async () => {
|
||||
if (client) {
|
||||
try {
|
||||
await client.disconnect();
|
||||
} catch {
|
||||
// Ignore disconnect errors.
|
||||
}
|
||||
client = null;
|
||||
}
|
||||
if (backend) {
|
||||
const server = backend;
|
||||
backend = null;
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.close((err) => (err ? reject(err) : resolve()));
|
||||
});
|
||||
}
|
||||
if (upstream) {
|
||||
try {
|
||||
await upstream.stop();
|
||||
} catch {
|
||||
// Ignore stop errors.
|
||||
}
|
||||
upstream = null;
|
||||
}
|
||||
});
|
||||
|
||||
it("aborts the upstream request stream, which the server observes as cancellation", async () => {
|
||||
const slow = createSlowTool();
|
||||
upstream = createTestServerHttp({
|
||||
serverInfo: createTestServerInfo("cancel-test", "1.0.0"),
|
||||
tools: [slow.tool],
|
||||
modern: {},
|
||||
});
|
||||
await upstream.start();
|
||||
|
||||
const { baseUrl, server, authToken } = await startRemoteBackend();
|
||||
backend = server;
|
||||
|
||||
const connected = new InspectorClient(
|
||||
{ type: "streamable-http", url: upstream.url },
|
||||
{
|
||||
environment: {
|
||||
transport: createRemoteTransport({ baseUrl, authToken }),
|
||||
},
|
||||
versionNegotiation: eraToVersionNegotiation("modern"),
|
||||
},
|
||||
);
|
||||
await connected.connect();
|
||||
client = connected;
|
||||
expect(connected.getProtocolEra()).toBe("modern");
|
||||
|
||||
const { tools } = await connected.listTools();
|
||||
const tool = tools.find((t) => t.name === "slow_task");
|
||||
expect(tool).toBeDefined();
|
||||
|
||||
// Hold the rejection from the first turn so the runner never sees it as
|
||||
// unhandled while we wait on the server side.
|
||||
const settled = connected.callTool(tool!, {}).catch((err: unknown) => err);
|
||||
|
||||
await slow.started;
|
||||
expect(connected.cancelToolCall()).toBe(true);
|
||||
|
||||
// The user-facing contract is unchanged: a deliberate cancel still surfaces
|
||||
// as ToolCallCancelledError, whichever wire signal carried it.
|
||||
await expect(settled).resolves.toBeInstanceOf(ToolCallCancelledError);
|
||||
|
||||
// The assertion that matters. Before the fix the server saw only a
|
||||
// `notifications/cancelled` it was free to ignore (and did), its request
|
||||
// signal never fired, and this promise never resolved.
|
||||
await expect(slow.aborted).resolves.toBe(true);
|
||||
}, 30_000);
|
||||
|
||||
it("advertises the per-request stream only for streamable-http", () => {
|
||||
// The flag is what the SDK forks on, and the factory is where a server's
|
||||
// configured type reaches the transport. stdio and SSE multiplex every
|
||||
// request over one shared channel: there is no per-request stream to close,
|
||||
// and aborting anything would take the whole session down — so they must
|
||||
// keep the `notifications/cancelled` mechanism.
|
||||
const create = createRemoteTransport({ baseUrl: "http://unused.example" });
|
||||
expect(
|
||||
create({ type: "stdio", command: "echo", args: [] }, {}).transport
|
||||
.hasPerRequestStream,
|
||||
).toBe(false);
|
||||
expect(
|
||||
create({ type: "sse", url: "http://unused.example/sse" }, {}).transport
|
||||
.hasPerRequestStream,
|
||||
).toBe(false);
|
||||
expect(
|
||||
create({ type: "streamable-http", url: "http://unused.example/mcp" }, {})
|
||||
.transport.hasPerRequestStream,
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
|
@ -639,6 +639,11 @@ describe("server.ts supplemental coverage", () => {
|
|||
expect((await res.json()).error).toMatch(/oauthRequestRefreshToken/);
|
||||
});
|
||||
|
||||
it("rejects a non-boolean oauthRevokeOnClear (#2144)", async () => {
|
||||
const res = await postSettings({ ...base, oauthRevokeOnClear: "no" });
|
||||
expect((await res.json()).error).toMatch(/oauthRevokeOnClear/);
|
||||
});
|
||||
|
||||
it("rejects malformed roots", async () => {
|
||||
const res = await postSettings({ ...base, roots: [{ uri: 1 }] });
|
||||
expect((await res.json()).error).toMatch(/roots/);
|
||||
|
|
@ -708,6 +713,32 @@ describe("server.ts supplemental coverage", () => {
|
|||
expect(body.mcpServers.srv?.oauth?.requestRefreshToken).toBeUndefined();
|
||||
});
|
||||
|
||||
// #2144 — same reasoning as the refresh-token pair above: a 200 only proves
|
||||
// the payload validated, not that the field survived the write-through.
|
||||
it("persists the revoke-on-clear opt-out through a save", async () => {
|
||||
expect(
|
||||
(await postSettings({ ...base, oauthRevokeOnClear: false })).status,
|
||||
).toBe(200);
|
||||
|
||||
const res = await fetch(`${h.baseUrl}/api/servers`);
|
||||
const body = (await res.json()) as {
|
||||
mcpServers: Record<string, { oauth?: { revokeOnClear?: boolean } }>;
|
||||
};
|
||||
expect(body.mcpServers.srv?.oauth?.revokeOnClear).toBe(false);
|
||||
});
|
||||
|
||||
it("writes no revoke-on-clear field when the setting is on", async () => {
|
||||
expect(
|
||||
(await postSettings({ ...base, oauthRevokeOnClear: true })).status,
|
||||
).toBe(200);
|
||||
|
||||
const res = await fetch(`${h.baseUrl}/api/servers`);
|
||||
const body = (await res.json()) as {
|
||||
mcpServers: Record<string, { oauth?: Record<string, unknown> }>;
|
||||
};
|
||||
expect(body.mcpServers.srv?.oauth?.revokeOnClear).toBeUndefined();
|
||||
});
|
||||
|
||||
it("accepts a fully-populated valid settings payload", async () => {
|
||||
const res = await postSettings({
|
||||
...base,
|
||||
|
|
@ -724,6 +755,7 @@ describe("server.ts supplemental coverage", () => {
|
|||
oauthScopes: "a b",
|
||||
enterpriseManaged: true,
|
||||
oauthRequestRefreshToken: false,
|
||||
oauthRevokeOnClear: false,
|
||||
roots: [{ uri: "file:///x", name: "x" }],
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
|
|
@ -892,6 +924,30 @@ describe("server.ts supplemental coverage", () => {
|
|||
}
|
||||
});
|
||||
|
||||
// #2144 — same all-or-nothing rule for the revocation opt-out.
|
||||
it("drops oauth whose revokeOnClear is not a boolean", async () => {
|
||||
const h = await start({
|
||||
seedConfig: JSON.stringify({
|
||||
mcpServers: {
|
||||
srv: {
|
||||
type: "streamable-http",
|
||||
url: "https://x.test/mcp",
|
||||
oauth: { revokeOnClear: "no" },
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
try {
|
||||
const res = await fetch(`${h.baseUrl}/api/servers`);
|
||||
const body = (await res.json()) as {
|
||||
mcpServers: Record<string, Record<string, unknown>>;
|
||||
};
|
||||
expect(body.mcpServers.srv).not.toHaveProperty("oauth");
|
||||
} finally {
|
||||
await stop(h);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps a well-formed requestRefreshToken opt-out on read (#2068)", async () => {
|
||||
const h = await start({
|
||||
seedConfig: JSON.stringify({
|
||||
|
|
|
|||
Loading…
Reference in a new issue