Merge pull request #2363 from modelcontextprotocol/v2/fix/2028-stdio-delayed-responses
fix: reset relay request-response waits on progress notifications (#2028)
This commit is contained in:
commit
3e728a3b8c
3 changed files with 450 additions and 27 deletions
115
clients/web/src/test/core/mcp/remote/progressToken.test.ts
Normal file
115
clients/web/src/test/core/mcp/remote/progressToken.test.ts
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/**
|
||||
* Unit tests for the shared `progressTokenOf` guard (#2028) — the string/safe
|
||||
* integer extractor both relay waits (browser `RemoteClientTransport` and Node
|
||||
* `RemoteSession`) use to correlate a `notifications/progress` with the request
|
||||
* whose timeout it should re-arm. Covers the protocol's accept set and, since
|
||||
* the guard delegates to the SDK's `ProgressTokenSchema`, the fractional and
|
||||
* past-`MAX_SAFE_INTEGER` values a bare `typeof` check would have let through.
|
||||
*/
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { progressTokenOf } from "@inspector/core/mcp/remote/progressToken.js";
|
||||
import type { JSONRPCMessage } from "@modelcontextprotocol/client";
|
||||
|
||||
// Cast helper: these fixtures are deliberately off-spec to exercise the guard
|
||||
// branches, which the typed JSONRPCMessage shape would otherwise forbid.
|
||||
const msg = (m: unknown): JSONRPCMessage => m as JSONRPCMessage;
|
||||
|
||||
describe("progressTokenOf (#2028)", () => {
|
||||
it("returns a numeric progressToken from a progress notification", () => {
|
||||
expect(
|
||||
progressTokenOf({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: 42, progress: 1, total: 3 },
|
||||
}),
|
||||
).toBe(42);
|
||||
});
|
||||
|
||||
it("returns a string progressToken", () => {
|
||||
expect(
|
||||
progressTokenOf({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: "abc", progress: 1 },
|
||||
}),
|
||||
).toBe("abc");
|
||||
});
|
||||
|
||||
it("returns undefined for a different notification method", () => {
|
||||
expect(
|
||||
progressTokenOf({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/message",
|
||||
params: { level: "info", data: {} },
|
||||
}),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined for a JSON-RPC response (no method)", () => {
|
||||
expect(
|
||||
progressTokenOf({ jsonrpc: "2.0", id: 1, result: {} }),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined when a progress note carries no params", () => {
|
||||
expect(
|
||||
progressTokenOf(
|
||||
msg({ jsonrpc: "2.0", method: "notifications/progress" }),
|
||||
),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined when params is null", () => {
|
||||
expect(
|
||||
progressTokenOf(
|
||||
msg({ jsonrpc: "2.0", method: "notifications/progress", params: null }),
|
||||
),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined when the progressToken is neither string nor number", () => {
|
||||
expect(
|
||||
progressTokenOf(
|
||||
msg({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: { nested: true } },
|
||||
}),
|
||||
),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("rejects a fractional numeric token (the protocol allows only integers)", () => {
|
||||
expect(
|
||||
progressTokenOf(
|
||||
msg({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: 1.5, progress: 1 },
|
||||
}),
|
||||
),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("rejects an integer past MAX_SAFE_INTEGER", () => {
|
||||
expect(
|
||||
progressTokenOf(
|
||||
msg({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: Number.MAX_SAFE_INTEGER + 1, progress: 1 },
|
||||
}),
|
||||
),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("accepts a safe integer at the boundary", () => {
|
||||
expect(
|
||||
progressTokenOf({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: Number.MAX_SAFE_INTEGER, progress: 1 },
|
||||
}),
|
||||
).toBe(Number.MAX_SAFE_INTEGER);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { describe, it, expect, vi } from "vitest";
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { RemoteClientTransport } from "@inspector/core/mcp/remote/remoteClientTransport.js";
|
||||
import type { MCPServerConfig } from "@inspector/core/mcp/types.js";
|
||||
|
||||
|
|
@ -686,4 +686,127 @@ describe("RemoteClientTransport", () => {
|
|||
await transport.close();
|
||||
});
|
||||
});
|
||||
|
||||
describe("progress notifications keep the SSE response wait alive (#2028)", () => {
|
||||
// A remote backend holds POST /api/mcp/send open until the JSON-RPC
|
||||
// response, and the browser bounds that wait with `sseResponseTimeoutMs`.
|
||||
// For a long tool that reports progress, that wait must re-arm on each
|
||||
// `notifications/progress` — mirroring the SDK client's
|
||||
// `resetTimeoutOnProgress` — or the web client alone times out a call that
|
||||
// every other transport rides out. Log messages must NOT re-arm it.
|
||||
function backendHoldingSend(): {
|
||||
fetchFn: typeof fetch;
|
||||
getSse: () => ReturnType<typeof createPushableSseStream>;
|
||||
getSentId: () => string | number | undefined;
|
||||
} {
|
||||
let sse!: ReturnType<typeof createPushableSseStream>;
|
||||
let sentId: string | number | undefined;
|
||||
const fetchFn = vi
|
||||
.fn<typeof fetch>()
|
||||
.mockImplementation(async (input, init) => {
|
||||
const url = String(input);
|
||||
if (url.endsWith("/api/mcp/connect") && init?.method === "POST") {
|
||||
return new Response(JSON.stringify({ sessionId: "s-progress" }), {
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
if (url.includes("/api/mcp/events")) {
|
||||
sse = createPushableSseStream();
|
||||
return sse.response;
|
||||
}
|
||||
if (url.endsWith("/api/mcp/send") && init?.method === "POST") {
|
||||
const body = JSON.parse(String(init.body)) as {
|
||||
message: { id?: string | number };
|
||||
};
|
||||
sentId = body.message.id;
|
||||
// Backend accepted the request; the response arrives later over SSE.
|
||||
return new Response(JSON.stringify({ ok: true }), { status: 200 });
|
||||
}
|
||||
return new Response("not found", { status: 404 });
|
||||
});
|
||||
return { fetchFn, getSse: () => sse, getSentId: () => sentId };
|
||||
}
|
||||
|
||||
// Fake timers so the deadline is driven deterministically rather than
|
||||
// raced against a real `setTimeout` — a real-sleep test can let the timeout
|
||||
// fire before a loaded runner processes the enqueued SSE frame (#1596: a
|
||||
// race is fixed with fake timers, never with headroom). Each push is
|
||||
// followed by a 0ms async advance to flush the SSE consumer's stream read
|
||||
// so the frame is parsed before time moves.
|
||||
const TIMEOUT_MS = 1000;
|
||||
const flushSse = () => vi.advanceTimersByTimeAsync(0);
|
||||
|
||||
beforeEach(() => vi.useFakeTimers());
|
||||
afterEach(() => vi.useRealTimers());
|
||||
|
||||
it("re-arms the wait on each progress note, so a call outliving the timeout still resolves", async () => {
|
||||
const { fetchFn, getSse, getSentId } = backendHoldingSend();
|
||||
const transport = new RemoteClientTransport(
|
||||
{ baseUrl, fetchFn, sseResponseTimeoutMs: TIMEOUT_MS },
|
||||
config,
|
||||
);
|
||||
await transport.start();
|
||||
|
||||
const sent = transport.send({
|
||||
jsonrpc: "2.0",
|
||||
id: 1,
|
||||
method: "tools/call",
|
||||
});
|
||||
await flushSse();
|
||||
|
||||
// Three progress notes, each after 900ms (< the 1000ms window): without
|
||||
// the re-arm the wait dies at 1000ms, well before the third lands.
|
||||
for (let i = 0; i < 3; i++) {
|
||||
getSse().pushMessage({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: getSentId(), progress: i, total: 3 },
|
||||
});
|
||||
await flushSse();
|
||||
await vi.advanceTimersByTimeAsync(900);
|
||||
}
|
||||
getSse().pushMessage({
|
||||
jsonrpc: "2.0",
|
||||
id: getSentId(),
|
||||
result: { ok: true },
|
||||
});
|
||||
await flushSse();
|
||||
|
||||
await expect(sent).resolves.toBeUndefined();
|
||||
await transport.close();
|
||||
});
|
||||
|
||||
it("does not re-arm on notifications/message, so a log-only call still times out", async () => {
|
||||
const { fetchFn, getSse, getSentId } = backendHoldingSend();
|
||||
const transport = new RemoteClientTransport(
|
||||
{ baseUrl, fetchFn, sseResponseTimeoutMs: TIMEOUT_MS },
|
||||
config,
|
||||
);
|
||||
await transport.start();
|
||||
|
||||
const sent = transport.send({
|
||||
jsonrpc: "2.0",
|
||||
id: 1,
|
||||
method: "tools/call",
|
||||
});
|
||||
// Hold the expected rejection now (as the RemoteSession timeout tests do)
|
||||
// so it is asserted rather than suppressed by an empty catch.
|
||||
const rejection = expect(sent).rejects.toThrow(
|
||||
/Timed out waiting for MCP response on SSE/,
|
||||
);
|
||||
await flushSse();
|
||||
|
||||
// A log message before the deadline must not extend it.
|
||||
getSse().pushMessage({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/message",
|
||||
params: { level: "info", data: { msg: "tick", token: getSentId() } },
|
||||
});
|
||||
await flushSse();
|
||||
await vi.advanceTimersByTimeAsync(TIMEOUT_MS);
|
||||
|
||||
await rejection;
|
||||
await transport.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,11 +7,16 @@
|
|||
*/
|
||||
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import type { Transport } from "@modelcontextprotocol/client";
|
||||
import type { Transport, JSONRPCMessage } from "@modelcontextprotocol/client";
|
||||
import { RemoteSession } from "@inspector/core/mcp/remote/node/remote-session.js";
|
||||
import type { FetchRequestEntryBase } from "@inspector/core/mcp/types.js";
|
||||
import { AuthChallengeError } from "@inspector/core/auth/challenge.js";
|
||||
|
||||
// Stage a deliberately off-spec message as `unknown` and narrow with a single
|
||||
// cast — the guard branches these fixtures exercise are ones the typed
|
||||
// `JSONRPCMessage` shape forbids, and `unknown` is the safe top type to pass in.
|
||||
const asMsg = (m: unknown): JSONRPCMessage => m as JSONRPCMessage;
|
||||
|
||||
function makeFetchEntry(
|
||||
overrides: Partial<FetchRequestEntryBase> = {},
|
||||
): FetchRequestEntryBase {
|
||||
|
|
@ -231,6 +236,76 @@ describe("RemoteSession", () => {
|
|||
await expect(wait).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("a matching progress notification re-arms the wait past its original deadline (#2028)", async () => {
|
||||
vi.useFakeTimers();
|
||||
const session = new RemoteSession("s-progress-reset");
|
||||
const wait = session.waitForRequestResponse(5, 1000);
|
||||
// Almost at the deadline, then a progress note for this request arrives.
|
||||
await vi.advanceTimersByTimeAsync(900);
|
||||
session.onMessage({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: 5, progress: 1, total: 10 },
|
||||
});
|
||||
// Past the *original* 1000ms deadline: without the re-arm this would have
|
||||
// already rejected, and the resolve below would be a no-op on a dead wait.
|
||||
await vi.advanceTimersByTimeAsync(900);
|
||||
session.onMessage({ jsonrpc: "2.0", id: 5, result: {} });
|
||||
await expect(wait).resolves.toBeUndefined();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("a notifications/message does NOT re-arm the wait (log messages don't extend the deadline, #2028)", async () => {
|
||||
vi.useFakeTimers();
|
||||
const session = new RemoteSession("s-message-no-reset");
|
||||
const wait = session.waitForRequestResponse(6, 1000);
|
||||
const rejection = expect(wait).rejects.toThrow(/timed out/);
|
||||
await vi.advanceTimersByTimeAsync(500);
|
||||
session.onMessage({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/message",
|
||||
params: { level: "info", data: { msg: "tick" } },
|
||||
});
|
||||
await vi.advanceTimersByTimeAsync(500);
|
||||
await rejection;
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("noteRequestProgress is a no-op for an id with no pending wait", () => {
|
||||
const session = new RemoteSession("s-progress-noop");
|
||||
expect(() => session.noteRequestProgress(999)).not.toThrow();
|
||||
});
|
||||
|
||||
it("a progress notification on a timeoutMs=0 wait leaves it timerless and it still resolves", async () => {
|
||||
const session = new RemoteSession("s-progress-notimer");
|
||||
const wait = session.waitForRequestResponse(7, 0);
|
||||
session.onMessage({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: 7, progress: 1 },
|
||||
});
|
||||
session.onMessage({ jsonrpc: "2.0", id: 7, result: {} });
|
||||
await expect(wait).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("a progress notification with a non-token progressToken re-arms nothing", async () => {
|
||||
vi.useFakeTimers();
|
||||
const session = new RemoteSession("s-progress-bad-token");
|
||||
const wait = session.waitForRequestResponse(8, 1000);
|
||||
const rejection = expect(wait).rejects.toThrow(/timed out/);
|
||||
// A malformed token (object) must be ignored, not coerced to a key.
|
||||
session.onMessage(
|
||||
asMsg({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/progress",
|
||||
params: { progressToken: { bad: true }, progress: 1 },
|
||||
}),
|
||||
);
|
||||
await vi.advanceTimersByTimeAsync(1000);
|
||||
await rejection;
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("endSend is a no-op when no send is active", () => {
|
||||
const session = new RemoteSession("s-endsend-noop");
|
||||
expect(session.hasActiveSend()).toBe(false);
|
||||
|
|
|
|||
Loading…
Reference in a new issue