Migrate all three clients + core + test-servers from
@modelcontextprotocol/sdk@1.29 to SDK v2 (2.0.0-beta.4), keeping behavior
identical against legacy (2025-11-25) servers. Foundation for the SDK V2 +
New Spec stack.
Packages
- Split @modelcontextprotocol/sdk -> @modelcontextprotocol/client (Client,
transports, OAuth, error taxonomy, protocol types) + core (Zod schemas);
stdio -> client/stdio, Ajv -> client/validators/ajv. Test-servers ->
@modelcontextprotocol/server (+ server-legacy/sse). Deterministic import
rewrite (schemas -> core, everything else -> client); the codemod misroutes
client code to /server so it was not used for path routing.
- new Client(..., { versionNegotiation: { mode: 'legacy' } }) — nothing
2026-era goes on the wire.
Runtime (core/mcp/inspectorClient.ts)
- Error taxonomy: McpError -> ProtocolError, ErrorCode -> ProtocolErrorCode;
RequestTimeout -> SdkError/SdkErrorCode.
- Handlers take method strings; task methods (tasks/*, notifications/tasks/*)
use the 3-arg custom form (excluded from v2's spec-method set).
- Legacy tasks kept working (per project requirement): requestor tasks re-
implemented over raw client.request + explicit deprecated schemas;
experimental.tasks.callToolStream replaced by a local pollTaskToolCall
async generator (with _progressHandlers revival so task-execution progress
still reaches the caller). Receiver (server-initiated) tasks: the v2 Client
validates a sampling/elicit handler result and rejects the legacy { task }
response with -32602 — installReceiverTaskResponseBypass swaps the wrapped
entry in the private _requestHandlers map for the task-augmented branch so
{ task } reaches the wire (fixes server-initiated sampling-via-task).
- Pagination: list verbs use raw client.request({method}, List*ResultSchema)
for single-page behavior (v2's client.listX() auto-aggregate all pages).
Web / auth / apps
- Rename local JsonSchemaType -> InspectorFormSchema + add toFormSchema()
structural narrowing at the SDK-schema -> form boundary.
- ext-apps still peers on SDK v1: one compat boundary with documented casts
(remove when ext-apps#702 lands a v2 peer).
- OAuth provider saveClientInformation reconciled with v2's
OAuthClientInformationContext; InvalidClientMetadataError re-homed as a
local class (server-legacy/auth is not browser-safe).
- vitest.shared.mts dedupes @modelcontextprotocol/client+core to one copy so
vi.mock intercepts the SUT's import.
Accepted v2 behavior changes (per #1624 / spec §9.2): unknown-tool call
rejects with -32602; cancellation is stream-abort (no guaranteed
notifications/cancelled frame); output-schema strictness restored via the
Inspector's own Ajv check on the default path.
npm run ci green: validate (web 3087 / cli 212 / tui 274 / launcher 5),
coverage (>=90 per-file, all clients), smoke (launcher/cli/web), storybook
(410).
Closes #1624
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
208 lines
5.9 KiB
TypeScript
208 lines
5.9 KiB
TypeScript
import {
|
|
describe,
|
|
it,
|
|
expect,
|
|
vi,
|
|
beforeEach,
|
|
afterEach,
|
|
afterAll,
|
|
} from "vitest";
|
|
import * as path from "node:path";
|
|
import * as os from "node:os";
|
|
import * as fs from "node:fs/promises";
|
|
import { InspectorClient } from "@inspector/core/mcp/inspectorClient.js";
|
|
import { createTransportNode } from "@inspector/core/mcp/node/transport.js";
|
|
import type { MCPServerConfig } from "@inspector/core/mcp/types.js";
|
|
import { NodeOAuthStorage } from "@inspector/core/auth/node/storage-node.js";
|
|
import { createOAuthClientConfig } from "../helpers/oauth-client-fixtures.js";
|
|
import type { InspectorClientOptions } from "@inspector/core/mcp/inspectorClient.js";
|
|
|
|
const oauthTestStatePath = path.join(
|
|
os.tmpdir(),
|
|
`mcp-oauth-${process.pid}-inspectorClient-oauth-fetchFn.json`,
|
|
);
|
|
|
|
function createTestOAuthConfig(
|
|
options: Parameters<typeof createOAuthClientConfig>[0],
|
|
) {
|
|
return {
|
|
...createOAuthClientConfig(options),
|
|
storage: new NodeOAuthStorage(oauthTestStatePath),
|
|
};
|
|
}
|
|
|
|
const mockAuth = vi.fn();
|
|
// Partial mock: keep every real export (Client, transports, types, …) and
|
|
// override only `auth`. A full-replace factory would drop `Client`, which
|
|
// InspectorClient imports from the same module.
|
|
vi.mock("@modelcontextprotocol/client", async (importOriginal) => {
|
|
const actual =
|
|
await importOriginal<typeof import("@modelcontextprotocol/client")>();
|
|
return {
|
|
...actual,
|
|
auth: (...args: unknown[]) => mockAuth(...args),
|
|
};
|
|
});
|
|
|
|
describe("InspectorClient OAuth fetchFn", () => {
|
|
let client: InspectorClient;
|
|
|
|
afterAll(async () => {
|
|
try {
|
|
await fs.unlink(oauthTestStatePath);
|
|
} catch {
|
|
// Ignore if file does not exist or already removed
|
|
}
|
|
});
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.spyOn(console, "log").mockImplementation(() => {});
|
|
|
|
mockAuth.mockImplementation(
|
|
async (provider: { redirectToAuthorization: (url: URL) => void }) => {
|
|
provider.redirectToAuthorization(
|
|
new URL("http://example.com/oauth/authorize"),
|
|
);
|
|
return "REDIRECT";
|
|
},
|
|
);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (client) {
|
|
try {
|
|
await client.disconnect();
|
|
} catch {
|
|
// Ignore disconnect errors
|
|
}
|
|
}
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("should pass fetchFn to auth() when provided", async () => {
|
|
const mockFetchFn = vi.fn();
|
|
const oauthConfig = createTestOAuthConfig({
|
|
mode: "static",
|
|
clientId: "test-client",
|
|
redirectUrl: "http://localhost:3000/callback",
|
|
});
|
|
|
|
client = new InspectorClient(
|
|
{ type: "sse", url: "http://localhost:3000/sse" } as MCPServerConfig,
|
|
{
|
|
environment: {
|
|
transport: createTransportNode,
|
|
fetch: mockFetchFn,
|
|
oauth: {
|
|
storage: oauthConfig.storage,
|
|
navigation: oauthConfig.navigation,
|
|
redirectUrlProvider: oauthConfig.redirectUrlProvider,
|
|
},
|
|
},
|
|
oauth: {
|
|
clientId: oauthConfig.clientId,
|
|
clientSecret: oauthConfig.clientSecret,
|
|
clientMetadataUrl: oauthConfig.clientMetadataUrl,
|
|
scope: oauthConfig.scope,
|
|
},
|
|
},
|
|
);
|
|
|
|
const url = await client.authenticate();
|
|
|
|
expect(url).toBeInstanceOf(URL);
|
|
expect(mockAuth).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.objectContaining({
|
|
fetchFn: expect.any(Function),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("should pass fetchFn to auth() when not provided (uses default fetch)", async () => {
|
|
const oauthConfig = createTestOAuthConfig({
|
|
mode: "static",
|
|
clientId: "test-client",
|
|
redirectUrl: "http://localhost:3000/callback",
|
|
});
|
|
|
|
client = new InspectorClient(
|
|
{ type: "sse", url: "http://localhost:3000/sse" } as MCPServerConfig,
|
|
{
|
|
environment: {
|
|
transport: createTransportNode,
|
|
oauth: {
|
|
storage: oauthConfig.storage,
|
|
navigation: oauthConfig.navigation,
|
|
redirectUrlProvider: oauthConfig.redirectUrlProvider,
|
|
},
|
|
},
|
|
oauth: {
|
|
clientId: oauthConfig.clientId,
|
|
clientSecret: oauthConfig.clientSecret,
|
|
clientMetadataUrl: oauthConfig.clientMetadataUrl,
|
|
scope: oauthConfig.scope,
|
|
},
|
|
} as InspectorClientOptions,
|
|
);
|
|
|
|
await client.authenticate();
|
|
|
|
expect(mockAuth).toHaveBeenCalled();
|
|
const callArgs = mockAuth.mock.calls[0]!;
|
|
const options = callArgs[1];
|
|
expect(options).toHaveProperty("fetchFn");
|
|
expect(typeof options.fetchFn).toBe("function");
|
|
});
|
|
|
|
it("should pass fetchFn to auth() in completeOAuthFlow when provided", async () => {
|
|
const mockFetchFn = vi.fn();
|
|
mockAuth.mockImplementation(
|
|
async (provider: { saveTokens: (tokens: unknown) => void }) => {
|
|
provider.saveTokens({
|
|
access_token: "test-token",
|
|
token_type: "Bearer",
|
|
});
|
|
return "AUTHORIZED";
|
|
},
|
|
);
|
|
|
|
const oauthConfig = createTestOAuthConfig({
|
|
mode: "static",
|
|
clientId: "test-client",
|
|
redirectUrl: "http://localhost:3000/callback",
|
|
});
|
|
|
|
client = new InspectorClient(
|
|
{ type: "sse", url: "http://localhost:3000/sse" } as MCPServerConfig,
|
|
{
|
|
environment: {
|
|
transport: createTransportNode,
|
|
fetch: mockFetchFn,
|
|
oauth: {
|
|
storage: oauthConfig.storage,
|
|
navigation: oauthConfig.navigation,
|
|
redirectUrlProvider: oauthConfig.redirectUrlProvider,
|
|
},
|
|
},
|
|
oauth: {
|
|
clientId: oauthConfig.clientId,
|
|
clientSecret: oauthConfig.clientSecret,
|
|
clientMetadataUrl: oauthConfig.clientMetadataUrl,
|
|
scope: oauthConfig.scope,
|
|
},
|
|
},
|
|
);
|
|
|
|
await client.completeOAuthFlow("test-authorization-code");
|
|
|
|
expect(mockAuth).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.objectContaining({
|
|
authorizationCode: "test-authorization-code",
|
|
fetchFn: expect.any(Function),
|
|
}),
|
|
);
|
|
});
|
|
});
|