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[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(); 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), }), ); }); });