Merge branch 'v2/main' into v2/fix/2123-root-union-schemas
This commit is contained in:
commit
67d612a7eb
2 changed files with 578 additions and 56 deletions
86
clients/web/src/test/core/mcp/remote/parseSSE.test.ts
Normal file
86
clients/web/src/test/core/mcp/remote/parseSSE.test.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/**
|
||||
* `parseSSE` cross-chunk frame loss.
|
||||
*
|
||||
* The reproduction needs no browser and no network: `parseSSE` takes a reader,
|
||||
* so a test supplies the chunk boundary directly instead of hoping a real
|
||||
* transport happens to produce one. That is what makes this defect testable at
|
||||
* all — on the wire the split is a function of payload size, TCP segmentation
|
||||
* and any proxy in between, none of which a test can steer.
|
||||
*
|
||||
* The bug: `currentEvent` / `currentData` are declared INSIDE the read loop, so
|
||||
* a frame whose `data:` line arrives in one chunk and whose terminating blank
|
||||
* line arrives in the next is discarded. `buffer` already carries a partial
|
||||
* *line* across reads; nothing carries a partial *frame*.
|
||||
*/
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseSSE } from "@inspector/core/mcp/remote/remoteClientTransport";
|
||||
|
||||
/** A reader that yields exactly the chunks given, in order. */
|
||||
function readerFrom(chunks: string[]): ReadableStreamDefaultReader<Uint8Array> {
|
||||
const encoder = new TextEncoder();
|
||||
let i = 0;
|
||||
return {
|
||||
read: async () =>
|
||||
i < chunks.length
|
||||
? { done: false as const, value: encoder.encode(chunks[i++]!) }
|
||||
: { done: true as const, value: undefined },
|
||||
} as ReadableStreamDefaultReader<Uint8Array>;
|
||||
}
|
||||
|
||||
async function collect(chunks: string[]) {
|
||||
const out: { event: string; data: string }[] = [];
|
||||
for await (const frame of parseSSE(readerFrom(chunks))) out.push(frame);
|
||||
return out;
|
||||
}
|
||||
|
||||
const FRAME = 'event: message\ndata: {"id":1,"result":"payload"}\n\n';
|
||||
|
||||
describe("parseSSE chunk boundaries", () => {
|
||||
it("delivers a frame that arrives whole (the case that hides the bug)", async () => {
|
||||
// Chromium and Firefox happen to produce this shape at our payload sizes,
|
||||
// which is why the defect below has never been observed in practice.
|
||||
expect(await collect([FRAME])).toEqual([
|
||||
{ event: "message", data: '{"id":1,"result":"payload"}' },
|
||||
]);
|
||||
});
|
||||
|
||||
it("delivers a frame split between its data line and its terminator", async () => {
|
||||
// THE BUG. Chunk 1 ends after the data line's newline; chunk 2 carries only
|
||||
// the blank line that closes the frame. Nothing is malformed — this is one
|
||||
// valid frame, delivered in two reads.
|
||||
const split = ['event: message\ndata: {"id":1,"result":"payload"}\n', "\n"];
|
||||
expect(split.join("")).toBe(FRAME); // same bytes as above, different reads
|
||||
expect(await collect(split)).toEqual([
|
||||
{ event: "message", data: '{"id":1,"result":"payload"}' },
|
||||
]);
|
||||
});
|
||||
|
||||
it("delivers a frame split mid-payload", async () => {
|
||||
const split = ['event: message\ndata: {"id":1,"res', 'ult":"payload"}\n\n'];
|
||||
expect(split.join("")).toBe(FRAME);
|
||||
expect(await collect(split)).toEqual([
|
||||
{ event: "message", data: '{"id":1,"result":"payload"}' },
|
||||
]);
|
||||
});
|
||||
|
||||
it("delivers every frame when a burst is split at each boundary", async () => {
|
||||
// Bytes for three frames, re-cut so each split lands between a data line
|
||||
// and its terminator — the shape a large response most plausibly produces.
|
||||
const chunks: string[] = [];
|
||||
for (const id of [1, 2, 3]) {
|
||||
chunks.push(`event: message\ndata: {"id":${id}}\n`, "\n");
|
||||
}
|
||||
expect(await collect(chunks)).toEqual([
|
||||
{ event: "message", data: '{"id":1}' },
|
||||
{ event: "message", data: '{"id":2}' },
|
||||
{ event: "message", data: '{"id":3}' },
|
||||
]);
|
||||
});
|
||||
|
||||
it("flushes a final frame when the stream ends without a blank line", async () => {
|
||||
expect(await collect(['event: message\ndata: {"id":9}\n'])).toEqual([
|
||||
{ event: "message", data: '{"id":9}' },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import {
|
||||
discoverAuthorizationServerMetadataForServer,
|
||||
discoverScopes,
|
||||
getAuthorizationServerUrl,
|
||||
getAuthorizationServerUrlCandidates,
|
||||
} from "@inspector/core/auth/discovery.js";
|
||||
import type { OAuthProtectedResourceMetadata } from "@modelcontextprotocol/client";
|
||||
|
||||
|
|
@ -335,3 +337,172 @@ describe("getAuthorizationServerUrl", () => {
|
|||
expect(captured?.message).toMatch(/Invalid MCP server URL: "" \(.+\)/);
|
||||
});
|
||||
});
|
||||
|
||||
// #2110: an MCP server hosted under a path (Moodle, a WordPress plugin, any
|
||||
// PHP endpoint) publishes its OAuth metadata under that same path. Reducing the
|
||||
// fallback authorization-server URL to the origin threw that path away, so
|
||||
// discovery could only ever probe the domain root and 404'd.
|
||||
describe("path-hosted MCP servers (#2110)", () => {
|
||||
const pathServerUrl = "https://misc.example.com/mod/minilesson/mcp.php";
|
||||
|
||||
describe("getAuthorizationServerUrl", () => {
|
||||
it("keeps the server's path in the fallback URL", () => {
|
||||
expect(getAuthorizationServerUrl(pathServerUrl)).toEqual(
|
||||
new URL(pathServerUrl),
|
||||
);
|
||||
});
|
||||
|
||||
it("strips query and fragment, which an RFC 8414 issuer never carries", () => {
|
||||
expect(
|
||||
getAuthorizationServerUrl(`${pathServerUrl}?session=abc#frag`),
|
||||
).toEqual(new URL(pathServerUrl));
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAuthorizationServerUrlCandidates", () => {
|
||||
it("tries the path-scoped URL before the bare origin", () => {
|
||||
expect(getAuthorizationServerUrlCandidates(pathServerUrl)).toEqual([
|
||||
new URL(pathServerUrl),
|
||||
new URL("https://misc.example.com/"),
|
||||
]);
|
||||
});
|
||||
|
||||
it("offers a single candidate for a root-hosted server", () => {
|
||||
expect(
|
||||
getAuthorizationServerUrlCandidates("https://mcp.example.com"),
|
||||
).toEqual([new URL("https://mcp.example.com/")]);
|
||||
});
|
||||
|
||||
it("does not probe past an authorization server named by resource metadata", () => {
|
||||
const resourceMetadata: OAuthProtectedResourceMetadata = {
|
||||
resource: pathServerUrl,
|
||||
authorization_servers: ["https://auth.example.com/realms/r"],
|
||||
};
|
||||
expect(
|
||||
getAuthorizationServerUrlCandidates(pathServerUrl, resourceMetadata),
|
||||
).toEqual([new URL("https://auth.example.com/realms/r")]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("discoverAuthorizationServerMetadataForServer", () => {
|
||||
const metadata = {
|
||||
issuer: pathServerUrl,
|
||||
authorization_endpoint: "https://misc.example.com/mod/authorize.php",
|
||||
token_endpoint: "https://misc.example.com/mod/token.php",
|
||||
response_types_supported: ["code"],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("returns the path-scoped result without probing the origin", async () => {
|
||||
const { discoverAuthorizationServerMetadata } =
|
||||
await import("@modelcontextprotocol/client");
|
||||
vi.mocked(discoverAuthorizationServerMetadata).mockResolvedValue(
|
||||
metadata,
|
||||
);
|
||||
|
||||
await expect(
|
||||
discoverAuthorizationServerMetadataForServer(pathServerUrl),
|
||||
).resolves.toEqual(metadata);
|
||||
expect(discoverAuthorizationServerMetadata).toHaveBeenCalledTimes(1);
|
||||
expect(discoverAuthorizationServerMetadata).toHaveBeenCalledWith(
|
||||
new URL(pathServerUrl),
|
||||
{ fetchFn: undefined },
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to the origin when the path-scoped candidate has no metadata", async () => {
|
||||
const { discoverAuthorizationServerMetadata } =
|
||||
await import("@modelcontextprotocol/client");
|
||||
vi.mocked(discoverAuthorizationServerMetadata)
|
||||
.mockResolvedValueOnce(undefined)
|
||||
.mockResolvedValueOnce(metadata);
|
||||
|
||||
await expect(
|
||||
discoverAuthorizationServerMetadataForServer(pathServerUrl),
|
||||
).resolves.toEqual(metadata);
|
||||
expect(discoverAuthorizationServerMetadata).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
new URL("https://misc.example.com/"),
|
||||
{ fetchFn: undefined },
|
||||
);
|
||||
});
|
||||
|
||||
it("survives a throwing candidate while another remains", async () => {
|
||||
const { discoverAuthorizationServerMetadata } =
|
||||
await import("@modelcontextprotocol/client");
|
||||
vi.mocked(discoverAuthorizationServerMetadata)
|
||||
.mockRejectedValueOnce(new Error("issuer mismatch"))
|
||||
.mockResolvedValueOnce(metadata);
|
||||
|
||||
await expect(
|
||||
discoverAuthorizationServerMetadataForServer(pathServerUrl),
|
||||
).resolves.toEqual(metadata);
|
||||
});
|
||||
|
||||
it("rethrows the first error when every candidate fails", async () => {
|
||||
const { discoverAuthorizationServerMetadata } =
|
||||
await import("@modelcontextprotocol/client");
|
||||
vi.mocked(discoverAuthorizationServerMetadata)
|
||||
.mockRejectedValueOnce(new Error("path-scoped failed"))
|
||||
.mockRejectedValueOnce(new Error("origin failed"));
|
||||
|
||||
await expect(
|
||||
discoverAuthorizationServerMetadataForServer(pathServerUrl),
|
||||
).rejects.toThrow("path-scoped failed");
|
||||
});
|
||||
|
||||
it("resolves undefined when every candidate answers with no metadata", async () => {
|
||||
const { discoverAuthorizationServerMetadata } =
|
||||
await import("@modelcontextprotocol/client");
|
||||
vi.mocked(discoverAuthorizationServerMetadata).mockResolvedValue(
|
||||
undefined,
|
||||
);
|
||||
|
||||
await expect(
|
||||
discoverAuthorizationServerMetadataForServer(pathServerUrl),
|
||||
).resolves.toBeUndefined();
|
||||
expect(discoverAuthorizationServerMetadata).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("passes the caller's fetch through to every candidate", async () => {
|
||||
const { discoverAuthorizationServerMetadata } =
|
||||
await import("@modelcontextprotocol/client");
|
||||
vi.mocked(discoverAuthorizationServerMetadata).mockResolvedValue(
|
||||
undefined,
|
||||
);
|
||||
const fetchFn = vi.fn<typeof fetch>();
|
||||
|
||||
await discoverAuthorizationServerMetadataForServer(
|
||||
pathServerUrl,
|
||||
undefined,
|
||||
fetchFn,
|
||||
);
|
||||
|
||||
expect(discoverAuthorizationServerMetadata).toHaveBeenCalledWith(
|
||||
new URL(pathServerUrl),
|
||||
{ fetchFn },
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("discoverScopes reaches a path-hosted authorization server", async () => {
|
||||
const { discoverAuthorizationServerMetadata } =
|
||||
await import("@modelcontextprotocol/client");
|
||||
vi.mocked(discoverAuthorizationServerMetadata).mockResolvedValue({
|
||||
issuer: pathServerUrl,
|
||||
authorization_endpoint: "https://misc.example.com/mod/authorize.php",
|
||||
token_endpoint: "https://misc.example.com/mod/token.php",
|
||||
response_types_supported: ["code"],
|
||||
scopes_supported: ["read"],
|
||||
});
|
||||
|
||||
await expect(discoverScopes(pathServerUrl)).resolves.toBe("read");
|
||||
expect(discoverAuthorizationServerMetadata).toHaveBeenCalledWith(
|
||||
new URL(pathServerUrl),
|
||||
{ fetchFn: undefined },
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue