| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459 |
- import {
- createMcpDeadline,
- McpTimeoutError,
- waitWithinMcpDeadline,
- withTimeout,
- } from "../src/mcp/timeout.js";
- import { createMcpConnectionPool } from "../src/mcp/pool.js";
- import { delimiter, dirname } from "node:path";
- import { beforeEach, describe, expect, it, vi } from "vitest";
- import { SdkError, SdkErrorCode, UnauthorizedError } from "@modelcontextprotocol/client";
- import { ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY } from "@zcode/shared";
- import type { SharedZCodeCredentialStore } from "../src/auth/shared-credentials.js";
- const {
- capturedClientInfo,
- capturedClients,
- capturedHttpOptions,
- capturedSseOptions,
- capturedStdioOptions,
- capturedStdioStderrHandlers,
- capturedClientOptions,
- clientCloseMock,
- clientCallToolMock,
- clientConnectMock,
- clientListToolsMock,
- clientPingMock,
- clientSubscribeMock,
- clientUnsubscribeMock,
- transportCloseMock,
- transportFinishAuthMock,
- authMock,
- } = vi.hoisted(() => ({
- capturedClientInfo: [] as Array<{ name: string; version: string }>,
- capturedClients: [] as Array<{
- notificationHandlers: Map<string, (notification: unknown) => void>;
- onclose?: () => void;
- }>,
- capturedHttpOptions: [] as unknown[],
- capturedSseOptions: [] as unknown[],
- capturedStdioOptions: [] as unknown[],
- capturedStdioStderrHandlers: [] as Array<(chunk: Buffer) => void>,
- capturedClientOptions: [] as unknown[],
- clientCloseMock: vi.fn(async () => {}),
- clientCallToolMock: vi.fn(async () => ({
- content: [{ type: "text", text: "ok" }],
- })),
- clientConnectMock: vi.fn(async () => {}),
- clientListToolsMock: vi.fn(async () => ({ tools: [] })),
- clientPingMock: vi.fn(async () => ({})),
- clientSubscribeMock: vi.fn(async () => ({})),
- clientUnsubscribeMock: vi.fn(async () => ({})),
- transportCloseMock: vi.fn(async () => {}),
- transportFinishAuthMock: vi.fn(async () => {}),
- authMock: vi.fn(async () => "AUTHORIZED" as const),
- }));
- vi.mock("@modelcontextprotocol/client", async (importOriginal) => {
- const actual = await importOriginal<typeof import("@modelcontextprotocol/client")>();
- return {
- ...actual,
- auth: authMock,
- Client: class {
- readonly close = clientCloseMock;
- readonly callTool = clientCallToolMock;
- readonly connect = clientConnectMock;
- readonly listTools = clientListToolsMock;
- readonly ping = clientPingMock;
- readonly subscribeResource = clientSubscribeMock;
- readonly unsubscribeResource = clientUnsubscribeMock;
- getServerCapabilities() {
- return { resources: { subscribe: true } };
- }
- readonly notificationHandlers = new Map<string, (notification: unknown) => void>();
- onclose?: () => void;
- setNotificationHandler(method: string, handler: (notification: unknown) => void) {
- this.notificationHandlers.set(method, handler);
- }
- constructor(info: { name: string; version: string }, options?: unknown) {
- capturedClientInfo.push(info);
- capturedClientOptions.push(options);
- capturedClients.push(this);
- }
- getProtocolEra() {
- const options = capturedClientOptions.at(-1) as {
- versionNegotiation?: { mode?: unknown };
- };
- return options?.versionNegotiation?.mode === "legacy" ? "legacy" : "modern";
- }
- getNegotiatedProtocolVersion() {
- const options = capturedClientOptions.at(-1) as {
- versionNegotiation?: { mode?: unknown };
- };
- return options?.versionNegotiation?.mode === "legacy" ? "2025-11-25" : "2026-07-28";
- }
- },
- SSEClientTransport: class {
- readonly close = transportCloseMock;
- readonly finishAuth = transportFinishAuthMock;
- constructor(_url: URL, options: unknown) {
- capturedSseOptions.push(options);
- }
- },
- StreamableHTTPClientTransport: class {
- readonly close = transportCloseMock;
- readonly finishAuth = transportFinishAuthMock;
- constructor(_url: URL, options: unknown) {
- capturedHttpOptions.push(options);
- }
- },
- };
- });
- vi.mock("@modelcontextprotocol/client/stdio", () => ({
- StdioClientTransport: class {
- readonly close = transportCloseMock;
- readonly pid = 42_424;
- readonly stderr = {
- on: vi.fn((event: "data", handler: (chunk: Buffer) => void) => {
- if (event === "data") capturedStdioStderrHandlers.push(handler);
- }),
- };
- constructor(options: unknown) {
- capturedStdioOptions.push(options);
- }
- },
- }));
- import { createMcpAdapter } from "../src/mcp/index.js";
- import { createMcpTelemetryTracker } from "../src/mcp/telemetry.js";
- // 宿主内建的 stdio server(node_repl):source 只由宿主生成。
- const BUILT_IN_STDIO_CONFIG = {
- command: "node",
- source: { kind: "builtin" as const },
- type: "stdio" as const,
- };
- /**
- * 把 Phase 1 建连做成认证失败,并接管 Phase 2 的 `auth()`:调用 provider 的
- * `redirectToAuthorization()` 暴露授权 URL,然后阻塞在 gate 上,直到测试调用 `release()`。
- *
- * 修复原因:授权 URL 的归属已经从「Phase 1 provider 自己 redirect」改为「Phase 2 事务发布」,
- * 所以这些用例必须走 connect 失败 → 分类 → Phase 2 这条新链路。
- */
- function stubInteractiveAuthorization(
- authorizationUrl: string,
- beforeAuthorization?: Promise<void>,
- ): { release: () => void; ready: Promise<void> } {
- let markReady!: () => void;
- const ready = new Promise<void>((resolve) => {
- markReady = resolve;
- });
- let release!: () => void;
- const gate = new Promise<void>((resolve) => {
- release = resolve;
- });
- let connectAttempts = 0;
- clientConnectMock.mockImplementation(async () => {
- connectAttempts += 1;
- // 第一次建连以确定性认证错误失败,触发 Phase 2;授权完成后的重连必须成功。
- if (connectAttempts === 1) throw new UnauthorizedError();
- });
- authMock.mockImplementation(async (provider: unknown) => {
- const interactiveProvider = provider as {
- redirectToAuthorization(url: URL): Promise<void>;
- };
- await beforeAuthorization;
- await interactiveProvider.redirectToAuthorization(new URL(authorizationUrl));
- markReady();
- await gate;
- return "AUTHORIZED" as const;
- });
- return { release, ready };
- }
- async function observePromiseWithin<T>(
- promise: Promise<T>,
- timeoutMs: number,
- ): Promise<
- | { status: "fulfilled"; value: T }
- | { reason: unknown; status: "rejected" }
- | { status: "pending" }
- > {
- let timeoutId: ReturnType<typeof setTimeout> | undefined;
- const observed = promise.then(
- (value) => ({ status: "fulfilled" as const, value }),
- (reason: unknown) => ({ reason, status: "rejected" as const }),
- );
- try {
- return await Promise.race([
- observed,
- new Promise<{ status: "pending" }>((resolve) => {
- timeoutId = setTimeout(() => resolve({ status: "pending" }), timeoutMs);
- }),
- ]);
- } finally {
- if (timeoutId !== undefined) clearTimeout(timeoutId);
- }
- }
- describe("createMcpAdapter", () => {
- beforeEach(() => {
- capturedClientInfo.length = 0;
- capturedHttpOptions.length = 0;
- capturedSseOptions.length = 0;
- capturedStdioOptions.length = 0;
- capturedStdioStderrHandlers.length = 0;
- capturedClientOptions.length = 0;
- capturedClients.length = 0;
- clientCloseMock.mockClear();
- clientCallToolMock.mockClear();
- clientConnectMock.mockClear();
- clientListToolsMock.mockReset();
- clientSubscribeMock.mockReset().mockResolvedValue({});
- clientUnsubscribeMock.mockReset().mockResolvedValue({});
- clientPingMock.mockClear();
- clientPingMock.mockResolvedValue({});
- transportCloseMock.mockClear();
- transportFinishAuthMock.mockClear();
- authMock.mockClear();
- authMock.mockResolvedValue("AUTHORIZED");
- clientConnectMock.mockResolvedValue(undefined);
- clientListToolsMock.mockResolvedValue({ tools: [] });
- clientCallToolMock.mockResolvedValue({
- content: [{ type: "text", text: "ok" }],
- });
- });
- it("close waits for cancelled handshake resource cleanup before returning", async () => {
- const started = Promise.withResolvers<void>();
- const cleanupStarted = Promise.withResolvers<void>();
- const cleaned = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(async () => {
- started.resolve();
- await new Promise(() => {});
- });
- clientCloseMock.mockImplementationOnce(async () => {
- cleanupStarted.resolve();
- await cleaned.promise;
- });
- const adapter = createMcpAdapter();
- const connecting = adapter.connectServer("slow", {
- type: "http",
- url: "https://mcp.example.test/mcp",
- });
- await started.promise;
- let settled = false;
- const closing = adapter.close().then(() => {
- settled = true;
- });
- await cleanupStarted.promise;
- for (let i = 0; i < 5; i++) await Promise.resolve();
- expect(settled).toBe(false);
- cleaned.resolve();
- await closing;
- await connecting;
- expect(await adapter.status()).toEqual({});
- });
- it("does not start a connection while the adapter is closing", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("first", { type: "stdio", command: "node" });
- const closeGate = Promise.withResolvers<void>();
- const connectGate = Promise.withResolvers<void>();
- clientCloseMock.mockImplementationOnce(() => closeGate.promise);
- const closing = adapter.close();
- await vi.waitFor(() => expect(clientCloseMock).toHaveBeenCalled());
- clientConnectMock.mockClear();
- // 持久实现由 beforeEach 复位;未被调用的 once 实现会串到下一个用例。
- clientConnectMock.mockImplementation(() => connectGate.promise);
- // 修复前关闭期间仍会登记新记录;close 清空记录后,连接完成时读不到记录而抛 TypeError。
- const late = adapter.connectServer("late", { type: "stdio", command: "node" });
- closeGate.resolve();
- await closing;
- connectGate.resolve();
- await expect(late).resolves.toMatchObject({ status: "disconnected" });
- expect(clientConnectMock).not.toHaveBeenCalled();
- expect(await adapter.status()).toEqual({});
- });
- it("waits for the existing App connection without exposing a premature snapshot or reconnecting", async () => {
- const gate = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(() => gate.promise);
- const adapter = createMcpAdapter();
- const connecting = adapter.connectServer("fixture", { type: "stdio", command: "node" });
- await vi.waitFor(() => expect(clientConnectMock).toHaveBeenCalledOnce());
- expect(adapter.appConnectionSnapshot?.("fixture")).toBeNull();
- const settled = vi.fn();
- const opening = adapter.waitForAppConnection!("fixture").then(settled);
- await Promise.resolve();
- expect(settled).not.toHaveBeenCalled();
- gate.resolve();
- await connecting;
- await opening;
- expect(settled).toHaveBeenCalledWith(adapter.appConnectionSnapshot?.("fixture"));
- expect(settled.mock.calls[0]?.[0]).not.toBeNull();
- expect(clientConnectMock).toHaveBeenCalledOnce();
- await adapter.close();
- });
- it("does not issue an App connection for an absent or failed server", async () => {
- const adapter = createMcpAdapter();
- expect(await adapter.waitForAppConnection!("missing")).toBeNull();
- clientConnectMock.mockRejectedValueOnce(new Error("fixture startup failure"));
- await adapter.connectServer("fixture", { type: "stdio", command: "node" });
- expect(await adapter.waitForAppConnection!("fixture")).toBeNull();
- expect(clientConnectMock).toHaveBeenCalledOnce();
- await adapter.close();
- });
- // 相同配置复用当前连接任务,只有配置变更才换代;换代后旧等待者不能拿到新连接。
- it("rejects an App startup waiter after the connection generation is replaced", async () => {
- const gate = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(() => gate.promise);
- const adapter = createMcpAdapter();
- const config = { type: "stdio" as const, command: "node" };
- const connecting = adapter.connectServer("fixture", config);
- await vi.waitFor(() => expect(clientConnectMock).toHaveBeenCalledOnce());
- const opening = adapter.waitForAppConnection!("fixture");
- const replaced = adapter.connectServer("fixture", { ...config, env: { VERSION: "2" } });
- gate.resolve();
- await Promise.all([connecting, replaced]);
- expect(await opening).toBeNull();
- expect(adapter.appConnectionSnapshot?.("fixture")).not.toBeNull();
- await adapter.close();
- });
- /** 在用例内收集未处理 rejection,不依赖测试框架的全局检测。 */
- async function collectUnhandledRejections(run: () => Promise<void>): Promise<unknown[]> {
- const leaked: unknown[] = [];
- const listener = (reason: unknown) => leaked.push(reason);
- process.on("unhandledRejection", listener);
- try {
- await run();
- // 未处理 rejection 在下一轮事件循环前派发。
- await new Promise<void>((resolve) => setImmediate(resolve));
- } finally {
- process.off("unhandledRejection", listener);
- }
- return leaked;
- }
- it.each(["cancelled", "expired"] as const)(
- "observes a shared task rejection after its waiter is already %s",
- async (mode) => {
- const task = Promise.withResolvers<void>();
- const abort = new AbortController();
- abort.abort(new Error("Caller left"));
- const waiting = Promise.resolve().then(() =>
- mode === "cancelled"
- ? withTimeout(task.promise, 1000, "Deadline expired", abort.signal)
- : waitWithinMcpDeadline(task.promise, createMcpDeadline(0), "Deadline expired"),
- );
- const leaked = await collectUnhandledRejections(async () => {
- await expect(waiting).rejects.toThrow(
- mode === "cancelled" ? "Caller left" : "Deadline expired",
- );
- task.reject(new Error("Shared task failed after waiter left"));
- });
- expect(leaked).toEqual([]);
- },
- );
- it("observes subscription work when cancellation and disconnect overlap", async () => {
- const adapter = createMcpAdapter();
- try {
- await adapter.connectServer("demo", { type: "http", url: "https://example.com/mcp" });
- const abort = new AbortController();
- const subscription = adapter.subscribeResource!(
- {
- serverName: "demo",
- uri: "test://closing",
- subscriberKey: "A:scope:1",
- },
- { signal: abort.signal },
- ).catch((error: unknown) => error);
- const leaked = await collectUnhandledRejections(async () => {
- abort.abort(new Error("Caller closing"));
- await adapter.disconnectServer("demo");
- expect(await subscription).toEqual(new Error("Caller closing"));
- });
- expect(leaked).toEqual([]);
- } finally {
- await adapter.close();
- }
- });
- it("preserves a committed subscription when a duplicate caller is cancelled", async () => {
- const adapter = createMcpAdapter();
- const request = {
- serverName: "demo",
- uri: "test://existing",
- subscriberKey: "session:scope:1",
- };
- try {
- await adapter.connectServer("demo", { type: "http", url: "https://example.com/mcp" });
- await adapter.subscribeResource!(request);
- const abort = new AbortController();
- const duplicate = adapter.subscribeResource!(request, { signal: abort.signal });
- abort.abort(new Error("Duplicate cancelled"));
- await expect(duplicate).rejects.toThrow("Duplicate cancelled");
- expect(await adapter.unsubscribeResourcesBySubscriber!("session:")).toBe(1);
- await adapter.unsubscribeResource!(request);
- expect(clientUnsubscribeMock).toHaveBeenCalledTimes(1);
- expect(clientSubscribeMock).toHaveBeenCalledTimes(1);
- } finally {
- await adapter.close();
- }
- });
- it("keeps another pending subscription with the same key when one caller leaves", async () => {
- const entered = Promise.withResolvers<void>(),
- release = Promise.withResolvers<void>();
- clientSubscribeMock.mockImplementationOnce(async () => {
- entered.resolve();
- await release.promise;
- return {};
- });
- const adapter = createMcpAdapter();
- const request = {
- serverName: "demo",
- uri: "test://concurrent",
- subscriberKey: "session:scope:1",
- };
- try {
- await adapter.connectServer("demo", { type: "http", url: "https://example.com/mcp" });
- const abort = new AbortController();
- const first = adapter.subscribeResource!(request, { signal: abort.signal });
- await entered.promise;
- const second = adapter.subscribeResource!(request);
- // 首个 RPC 仍由 gate 阻塞,让两个 caller 都登记完再取消其中一个。
- await new Promise<void>((resolve) => setImmediate(resolve));
- abort.abort(new Error("First caller left"));
- await expect(first).rejects.toThrow("First caller left");
- release.resolve();
- await second;
- expect(clientUnsubscribeMock).not.toHaveBeenCalled();
- expect(clientSubscribeMock).toHaveBeenCalledTimes(1);
- expect(await adapter.unsubscribeResourcesBySubscriber!("session:")).toBe(1);
- } finally {
- release.resolve();
- await adapter.close();
- }
- });
- it.each(["explicit", "scope"] as const)(
- "does not revive a subscription removed by %s cleanup during connection wait",
- async (mode) => {
- const entered = Promise.withResolvers<void>(),
- release = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(async () => {
- entered.resolve();
- await release.promise;
- });
- const adapter = createMcpAdapter();
- const request = { serverName: "demo", uri: "test://late", subscriberKey: "session:scope:1" };
- const connecting = adapter.connectServer("demo", {
- type: "http",
- url: "https://example.com/mcp",
- });
- try {
- await entered.promise;
- const subscribing = adapter.subscribeResource!(request);
- if (mode === "scope") await adapter.unsubscribeResourcesBySubscriber!("session:");
- else await adapter.unsubscribeResource!(request);
- release.resolve();
- await Promise.all([connecting, subscribing]);
- expect(await adapter.unsubscribeResourcesBySubscriber!("session:")).toBe(0);
- expect(clientSubscribeMock).not.toHaveBeenCalled();
- } finally {
- release.resolve();
- await adapter.close();
- }
- },
- );
- it.each(["lease", "revalidate", "configured"] as const)(
- "keeps a later %s caller independent of the initial connection wait",
- async (mode) => {
- const entered = Promise.withResolvers<void>();
- const release = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(async () => {
- entered.resolve();
- await release.promise;
- });
- const adapter = createMcpAdapter();
- const pool = createMcpConnectionPool({ createAdapter: () => adapter, idleGraceMs: 0 });
- const first = pool.acquireLease(),
- second = pool.acquireLease();
- const config = { type: "stdio" as const, command: "node", isolation: "workspace" as const };
- const scope = { workspaceIdentity: "shared" };
- const starting = first.connectServer("demo", config, scope);
- try {
- await entered.promise;
- const controller = new AbortController();
- const waiting =
- mode === "configured"
- ? adapter.connectConfiguredServers({ demo: config }, { signal: controller.signal })
- : second.connectServer("demo", config, {
- ...scope,
- signal: controller.signal,
- revalidate: mode === "revalidate",
- });
- let finished = false;
- // 经 pool 租约的取消以 reject 结束本次等待;直接调用 adapter 时返回当前状态。
- const settled = waiting.then(
- () => "resolved",
- () => "rejected",
- );
- void settled.then(() => {
- finished = true;
- });
- controller.abort();
- await new Promise<void>((resolve) => setImmediate(resolve));
- expect(finished).toBe(true);
- expect(await settled).toBe(mode === "configured" ? "resolved" : "rejected");
- expect(adapter.getToolSnapshot().demo.status.status).toBe("connecting");
- release.resolve();
- await starting;
- expect(clientConnectMock).toHaveBeenCalledOnce();
- expect((await first.callTool({ serverName: "demo", toolName: "ping" })).content).toEqual([
- { type: "text", text: "ok" },
- ]);
- } finally {
- release.resolve();
- await starting;
- await pool.close();
- }
- },
- );
- it.each(["initial", "recovery"] as const)(
- "continues a list_changed received during %s discovery without another read or notification",
- async (phase) => {
- const adapter = createMcpAdapter();
- const config = { type: "http" as const, url: "https://mcp.example.test" };
- const tool = (name: string) => ({ name, inputSchema: { type: "object", properties: {} } });
- const refreshed = Promise.withResolvers<void>();
- const off = adapter.onSnapshotChanged!(() => {
- if (adapter.getToolSnapshot().demo?.tools?.[0]?.toolName === "new") refreshed.resolve();
- });
- try {
- if (phase === "recovery") await adapter.connectServer("demo", config);
- clientListToolsMock.mockImplementationOnce(async () => {
- capturedClients.at(-1)!.notificationHandlers.get("notifications/tools/list_changed")!({});
- await new Promise<void>((resolve) => setImmediate(resolve));
- return { tools: [tool("old")] };
- });
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("new")] });
- if (phase === "recovery") {
- capturedClients.at(-1)!.onclose?.();
- await adapter.waitForServer!("demo", { timeoutMs: 1000 });
- } else await adapter.connectServer("demo", config);
- await withTimeout(refreshed.promise, 300, "dirty discovery was not resumed");
- expect(adapter.getToolSnapshot().demo.tools?.[0]?.toolName).toBe("new");
- } finally {
- off();
- await adapter.close();
- }
- },
- );
- it("does not inherit a settled caller wait and keeps independent timeout budgets", async () => {
- const entered = Promise.withResolvers<void>(),
- release = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(async () => {
- entered.resolve();
- await release.promise;
- });
- const adapter = createMcpAdapter();
- const pool = createMcpConnectionPool({ createAdapter: () => adapter, idleGraceMs: 0 });
- const a = pool.acquireLease(),
- b = pool.acquireLease();
- const config = { type: "stdio" as const, command: "node", isolation: "workspace" as const };
- const options = { workspaceIdentity: "shared" };
- const abort = new AbortController();
- const initial = a.connectServer("demo", config, { ...options, signal: abort.signal });
- try {
- await entered.promise;
- abort.abort();
- // 租约侧取消以 reject 结束首个调用方的等待;连接本身仍在进行。
- await expect(initial).rejects.toThrow();
- expect(adapter.getToolSnapshot().demo.status.status).toBe("connecting");
- expect(
- (await b.connectServer("demo", config, { ...options, oauthAuthorizationTimeoutMs: 5 }))
- .status,
- ).toBe("connecting");
- let complete = false;
- const survivor = b.connectServer("demo", config, options).then((status) => {
- complete = true;
- return status;
- });
- await new Promise<void>((resolve) => setImmediate(resolve));
- expect(complete).toBe(false);
- release.resolve();
- expect((await survivor).status).toBe("connected");
- expect(clientConnectMock).toHaveBeenCalledOnce();
- } finally {
- release.resolve();
- await initial.catch(() => undefined);
- await pool.close();
- }
- });
- it("hands a notification at refresh completion to the next refresh", async () => {
- const adapter = createMcpAdapter();
- const tool = (name: string) => ({ name, inputSchema: { type: "object", properties: {} } });
- await adapter.connectServer("demo", { type: "stdio", command: "node" });
- const notify = () =>
- capturedClients.at(-1)!.notificationHandlers.get("notifications/tools/list_changed")!({});
- clientListToolsMock
- .mockResolvedValueOnce({ tools: [tool("first")] })
- .mockResolvedValueOnce({ tools: [tool("last")] });
- const finished = Promise.withResolvers<void>();
- const off = adapter.onSnapshotChanged!(() => {
- const name = adapter.getToolSnapshot().demo.tools?.[0]?.toolName;
- if (name === "first") notify();
- if (name === "last") finished.resolve();
- });
- try {
- notify();
- await withTimeout(finished.promise, 500, "notification lost during task handoff");
- expect(clientListToolsMock).toHaveBeenCalledTimes(3);
- } finally {
- off();
- await adapter.close();
- }
- });
- it.each(["subscribe", "unsubscribe"] as const)(
- "serializes a changed resource intent while %s is in flight",
- async (operation) => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", { type: "stdio", command: "node" });
- const entered = Promise.withResolvers<void>(),
- release = Promise.withResolvers<void>();
- const first = { serverName: "demo", uri: "test://resource", subscriberKey: "first" };
- const second = { ...first, subscriberKey: "second" };
- const events: string[] = [];
- clientSubscribeMock.mockImplementation(async () => {
- events.push("subscribe");
- return {};
- });
- clientUnsubscribeMock.mockImplementation(async () => {
- events.push("unsubscribe");
- return {};
- });
- if (operation === "unsubscribe") await adapter.subscribeResource!(first);
- const blocked = operation === "subscribe" ? clientSubscribeMock : clientUnsubscribeMock;
- blocked.mockImplementationOnce(async () => {
- entered.resolve();
- await release.promise;
- events.push(operation);
- return {};
- });
- const pending =
- operation === "subscribe"
- ? adapter.subscribeResource!(first)
- : adapter.unsubscribeResource!(first);
- try {
- await entered.promise;
- const latest =
- operation === "subscribe"
- ? adapter.unsubscribeResource!(first)
- : adapter.subscribeResource!(second);
- await new Promise<void>((resolve) => setImmediate(resolve));
- expect(events).toEqual(operation === "subscribe" ? [] : ["subscribe"]);
- release.resolve();
- await Promise.all([pending, latest]);
- expect(events).toEqual(
- operation === "subscribe"
- ? ["subscribe", "unsubscribe"]
- : ["subscribe", "unsubscribe", "subscribe"],
- );
- } finally {
- release.resolve();
- await pending;
- await adapter.close();
- }
- },
- );
- it("does not publish a discovery result after its client closed", async () => {
- clientListToolsMock.mockImplementationOnce(async () => {
- capturedClients.at(-1)!.onclose?.();
- return { tools: [] };
- });
- const adapter = createMcpAdapter();
- try {
- const status = await adapter.connectServer("demo", { type: "stdio", command: "node" });
- expect(status.status).toBe("failed");
- expect(adapter.appConnectionSnapshot!("demo")).toBeNull();
- expect(adapter.getToolSnapshot().demo.tools).toBeUndefined();
- } finally {
- await adapter.close();
- }
- });
- it("cancels only the initial caller wait while the shared connection keeps starting", async () => {
- const started = Promise.withResolvers<void>();
- const release = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(async () => {
- started.resolve();
- await release.promise;
- });
- const adapter = createMcpAdapter();
- const controller = new AbortController();
- const connecting = adapter.connectServer(
- "demo",
- { type: "stdio", command: "node" },
- { signal: controller.signal },
- );
- try {
- await started.promise;
- controller.abort();
- expect((await connecting).status).toBe("connecting");
- release.resolve();
- expect(await adapter.waitForServer!("demo", { timeoutMs: 1000 })).toEqual({
- timedOut: false,
- });
- expect(adapter.getToolSnapshot().demo.status.status).toBe("connected");
- expect(clientConnectMock).toHaveBeenCalledTimes(1);
- } finally {
- release.resolve();
- await adapter.close();
- }
- });
- it("retains pending tools through recovery attempts and clears them only on exhaustion", async () => {
- vi.useFakeTimers();
- const adapter = createMcpAdapter();
- const observed: Array<{ status: string; tools: number | undefined }> = [];
- try {
- clientListToolsMock.mockResolvedValueOnce({
- tools: [{ name: "ping", inputSchema: { type: "object" } }],
- } as never);
- await adapter.connectServer("demo", { type: "http", url: "https://mcp.example.test" });
- clientListToolsMock.mockRejectedValue(new Error("discovery unavailable"));
- adapter.onSnapshotChanged!(() => {
- const row = adapter.getToolSnapshot().demo;
- observed.push({ status: row.status.status, tools: row.tools?.length });
- });
- capturedClients.at(-1)!.onclose!();
- await vi.advanceTimersByTimeAsync(15_000);
- expect(clientConnectMock).toHaveBeenCalledTimes(6);
- expect(observed.at(-1)).toEqual({ status: "failed", tools: undefined });
- expect(observed.slice(0, -1).length).toBeGreaterThan(0);
- expect(
- observed.slice(0, -1).every((row) => row.status === "connecting" && row.tools === 1),
- ).toBe(true);
- expect(adapter.getToolSnapshot().demo.recovering).toBeUndefined();
- } finally {
- await adapter.close();
- vi.useRealTimers();
- }
- });
- it("notifies terminal failure after delayed connection cleanup, without emitting on reads", async () => {
- const closing = Promise.withResolvers<void>();
- clientListToolsMock.mockRejectedValueOnce(new Error("discovery failed"));
- clientCloseMock.mockImplementationOnce(() => closing.promise);
- const adapter = createMcpAdapter();
- const states: string[] = [];
- adapter.onSnapshotChanged!(() => {
- states.push(adapter.getToolSnapshot().demo?.status.status ?? "missing");
- });
- const connecting = adapter.connectServer("demo", { type: "stdio", command: "node" });
- await vi.waitFor(() => expect(clientCloseMock).toHaveBeenCalled());
- closing.resolve();
- await connecting;
- await Promise.resolve();
- expect(states.at(-1)).toBe("failed");
- const count = states.length;
- adapter.getToolSnapshot();
- await adapter.status();
- adapter.getToolSnapshot();
- await Promise.resolve();
- expect(states).toHaveLength(count);
- await adapter.close();
- });
- it("commits replacement, removal and disablement before awaiting old connection close", async () => {
- const adapter = createMcpAdapter();
- const config = { type: "stdio" as const, command: "server-a" };
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "old", inputSchema: { type: "object" } }],
- } as never);
- await adapter.connectConfiguredServers({ replaced: config, removed: config, disabled: config });
- const closing = Promise.withResolvers<void>();
- clientCloseMock.mockImplementationOnce(() => closing.promise);
- const pending = adapter.connectConfiguredServers({
- replaced: { ...config, command: "server-b" },
- disabled: { ...config, enabled: false },
- });
- try {
- const snapshot = adapter.getToolSnapshot!();
- expect(snapshot.replaced?.status.status).toBe("connecting");
- expect(snapshot.replaced?.tools).toBeUndefined();
- expect(snapshot.removed?.status.status).toBe("disconnected");
- expect(snapshot.disabled?.status.status).toBe("disabled");
- } finally {
- closing.resolve();
- await pending;
- await adapter.close();
- }
- });
- it.each(["disconnect", "disable"] as const)(
- "late %s completion cannot overwrite a newer connection",
- async (action) => {
- const adapter = createMcpAdapter();
- const config = { type: "stdio" as const, command: "server-a" };
- await adapter.connectServer("demo", config);
- const closing = Promise.withResolvers<void>();
- clientCloseMock.mockImplementationOnce(() => closing.promise);
- const old =
- action === "disconnect"
- ? adapter.disconnectServer("demo")
- : adapter.connectServer("demo", { ...config, enabled: false });
- try {
- await adapter.connectServer("demo", { ...config, command: "server-b" });
- closing.resolve();
- await old;
- expect(adapter.getToolSnapshot!().demo?.status.status).toBe("connected");
- } finally {
- closing.resolve();
- await old;
- await adapter.close();
- }
- },
- );
- it("authorizes once and retries a tool call that hit an OAuth error", async () => {
- const descriptor = {
- name: "oauth_ping",
- inputSchema: { type: "object" },
- annotations: { readOnlyHint: true },
- };
- clientListToolsMock.mockResolvedValue({ tools: [descriptor] } as never);
- const adapter = createMcpAdapter();
- await adapter.connectServer("runtime", {
- type: "http",
- url: "https://mcp.example.test/mcp",
- oauth: { type: "authorization_code" },
- });
- // 模型调用不固定定义:授权后的新连接只要仍提供该工具即可重试。
- clientListToolsMock.mockResolvedValue({
- tools: [{ ...descriptor, annotations: { readOnlyHint: false } }],
- } as never);
- clientCallToolMock
- .mockRejectedValueOnce(new UnauthorizedError())
- .mockResolvedValue({ content: [{ type: "text", text: "ok" }] });
- try {
- await expect(
- adapter.callTool({ serverName: "runtime", toolName: "oauth_ping" }),
- ).resolves.toMatchObject({ content: [{ text: "ok" }] });
- expect(authMock).toHaveBeenCalledTimes(1);
- expect(clientCallToolMock).toHaveBeenCalledTimes(2);
- } finally {
- await adapter.close();
- }
- });
- it("accepts an OAuth recovery whose status object changed after unrelated list refresh", async () => {
- const target = { name: "ping", inputSchema: { type: "object" } };
- clientListToolsMock.mockResolvedValue({ tools: [target] } as never);
- const adapter = createMcpAdapter();
- await adapter.connectServer("runtime", {
- type: "http",
- url: "https://mcp.example.test/mcp",
- oauth: { type: "authorization_code" },
- });
- let refreshed = false;
- const unsubscribe = adapter.onSnapshotChanged!(() => {
- if (
- capturedClients.length === 2 &&
- adapter.getToolSnapshot().runtime?.status.status === "connected" &&
- !refreshed
- ) {
- refreshed = true;
- clientListToolsMock.mockResolvedValue({
- tools: [target, { name: "other", inputSchema: { type: "object" } }],
- } as never);
- capturedClients.at(-1)!.notificationHandlers.get("notifications/tools/list_changed")!({});
- }
- });
- clientCallToolMock.mockRejectedValueOnce(new UnauthorizedError());
- try {
- await expect(
- adapter.callTool({ serverName: "runtime", toolName: "ping" }),
- ).resolves.toMatchObject({ content: [{ text: "ok" }] });
- expect(refreshed).toBe(true);
- expect(clientCallToolMock).toHaveBeenCalledTimes(2);
- } finally {
- unsubscribe();
- await adapter.close();
- }
- });
- it.each([
- ["a local not-connected error", "http", SdkErrorCode.NotConnected, "Not connected", 1],
- ["a connection-closed error", "http", SdkErrorCode.ConnectionClosed, "Connection closed", 2],
- [
- "a connection-closed code with other text",
- "http",
- SdkErrorCode.ConnectionClosed,
- "Not connected",
- 2,
- ],
- [
- "a stdio connection-closed error",
- "stdio",
- SdkErrorCode.ConnectionClosed,
- "Connection closed",
- 1,
- ],
- ] as const)(
- "retries a call once only for HTTP connection-closed errors: %s",
- async (_label, type, code, message, calls) => {
- const target = { name: "ping", inputSchema: { type: "object" } };
- clientListToolsMock.mockResolvedValue({ tools: [target] } as never);
- const adapter = createMcpAdapter();
- await adapter.connectServer(
- "demo",
- type === "http" ? { type, url: "https://mcp.example.test/mcp" } : { type, command: "node" },
- );
- clientCallToolMock.mockRejectedValueOnce(new SdkError(code, message));
- try {
- const call = adapter.callTool({ serverName: "demo", toolName: "ping" });
- if (calls === 2) await expect(call).resolves.toMatchObject({ content: [{ text: "ok" }] });
- else await expect(call).rejects.toThrow();
- expect(clientCallToolMock).toHaveBeenCalledTimes(calls);
- } finally {
- await adapter.close();
- }
- },
- );
- it("does not retry on a replaced configuration after the connection closes", async () => {
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "ping", inputSchema: { type: "object" } }],
- } as never);
- const adapter = createMcpAdapter();
- const entered = Promise.withResolvers<void>();
- const release = Promise.withResolvers<void>();
- clientCallToolMock.mockImplementationOnce(async () => {
- entered.resolve();
- await release.promise;
- throw new SdkError(SdkErrorCode.ConnectionClosed, "Connection closed");
- });
- await adapter.connectServer("demo", { type: "http", url: "https://old.example.test/mcp" });
- const call = adapter.callTool({ serverName: "demo", toolName: "ping" });
- const observed = call.then(
- () => "called",
- () => "rejected",
- );
- try {
- await entered.promise;
- await adapter.connectServer("demo", { type: "http", url: "https://new.example.test/mcp" });
- const replacement = adapter.appConnectionSnapshot!("demo");
- release.resolve();
- expect(await observed).toBe("rejected");
- expect(adapter.appConnectionSnapshot!("demo")).toEqual(replacement);
- expect(clientConnectMock).toHaveBeenCalledTimes(2);
- expect(clientCallToolMock).toHaveBeenCalledTimes(1);
- } finally {
- release.resolve();
- await observed;
- await adapter.close();
- }
- });
- it("does not replay an authorized call after its lease is revoked during OAuth recovery", async () => {
- const pool = createMcpConnectionPool({
- createAdapter: () => createMcpAdapter(),
- idleGraceMs: 0,
- });
- const caller = pool.acquireLease({ sessionId: "caller" });
- const other = pool.acquireLease({ sessionId: "other" });
- const config = {
- type: "http" as const,
- url: "https://mcp.example.test/mcp",
- oauth: { type: "authorization_code" as const },
- isolation: "workspace" as const,
- };
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "oauth_ping", inputSchema: { type: "object" } }],
- } as never);
- await caller.connectServer("demo", config);
- await other.connectServer("demo", config);
- const entered = Promise.withResolvers<void>();
- const authorized = Promise.withResolvers<void>();
- authMock.mockImplementationOnce(async () => {
- entered.resolve();
- await authorized.promise;
- return "AUTHORIZED";
- });
- clientCallToolMock
- .mockRejectedValueOnce(new UnauthorizedError())
- .mockResolvedValue({ content: [] });
- const call = caller.callTool({ serverName: "demo", toolName: "oauth_ping" });
- const observed = call.then(
- () => "called",
- (error) => String(error),
- );
- try {
- await entered.promise;
- await caller.connectConfiguredServers({});
- authorized.resolve();
- // 授权完成后本租约已释放:不替它重放调用,返回原授权错误。
- expect(await observed).toMatch(/Unauthorized/);
- expect(clientCallToolMock).toHaveBeenCalledTimes(1);
- expect(other.getToolSnapshot!()?.demo?.status.status).toBe("connected");
- } finally {
- authorized.resolve();
- await observed;
- await caller.close();
- await other.close();
- await pool.close();
- }
- });
- it("forwards runtime scope, session, turn, and trace context through MCP request metadata", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("node_repl", {
- command: "node",
- type: "stdio",
- });
- await adapter.callTool({
- arguments: { code: "1 + 1" },
- serverName: "node_repl",
- toolName: "js",
- runtimeScope: "subagent",
- trace: {
- parentSpanId: "parent-1",
- sessionId: "session-1" as never,
- spanId: "span-1",
- traceId: "trace-1" as never,
- turnId: "turn-1" as never,
- },
- });
- expect(clientCallToolMock).toHaveBeenCalledWith(
- {
- arguments: { code: "1 + 1" },
- name: "js",
- _meta: {
- "com.zcode/request-context": {
- parent_span_id: "parent-1",
- runtime_scope: "subagent",
- session_id: "session-1",
- span_id: "span-1",
- trace_id: "trace-1",
- turn_id: "turn-1",
- },
- parent_span_id: "parent-1",
- runtime_scope: "subagent",
- session_id: "session-1",
- span_id: "span-1",
- trace_id: "trace-1",
- turn_id: "turn-1",
- },
- },
- expect.objectContaining({ resetTimeoutOnProgress: true }),
- );
- });
- it("forwards the root session of a subagent call through MCP request metadata", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("node_repl", { command: "node", type: "stdio" });
- await adapter.callTool({
- arguments: { code: "1 + 1" },
- serverName: "node_repl",
- toolName: "js",
- runtimeScope: "subagent",
- trace: {
- rootSessionId: "session-1" as never,
- sessionId: "subagent-1" as never,
- traceId: "trace-1" as never,
- },
- });
- const meta = clientCallToolMock.mock.calls.at(-1)?.[0]._meta;
- expect(meta).toMatchObject({ root_session_id: "session-1", session_id: "subagent-1" });
- expect(meta["com.zcode/request-context"]).toMatchObject({
- root_session_id: "session-1",
- session_id: "subagent-1",
- });
- });
- it("uses the injected client version for MCP initialize", async () => {
- const adapter = createMcpAdapter({ clientVersion: "9.8.7" });
- await adapter.connectServer("demo", {
- command: "node",
- type: "stdio",
- });
- expect(capturedClientInfo).toEqual([
- {
- name: "zcode",
- version: "9.8.7",
- },
- ]);
- });
- it("logs the configured negotiation policy and actual MCP protocol version", async () => {
- const infoMock = vi.fn();
- const logger = {
- child: vi.fn(),
- debug: vi.fn(),
- error: vi.fn(),
- info: infoMock,
- warn: vi.fn(),
- };
- logger.child.mockReturnValue(logger);
- const adapter = createMcpAdapter({
- clientName: "zcode-test",
- clientVersion: "9.8.7",
- connectionContext: {
- mcpConnectionId: "connection-session-1",
- mcpIsolation: "session",
- sessionId: "session-1",
- workspaceKey: "workspace-1",
- },
- logger,
- });
- await adapter.connectServer("auto-modern", {
- command: "node",
- type: "stdio",
- });
- await adapter.connectServer("explicit-legacy", {
- command: "node",
- protocolVersion: "legacy",
- type: "stdio",
- });
- expect(infoMock).toHaveBeenCalledWith(
- "MCP server connected",
- expect.objectContaining({
- event: "mcp.server.connected",
- mcpClientName: "zcode-test",
- mcpClientVersion: "9.8.7",
- mcpConnectionId: "connection-session-1",
- mcpIsolation: "session",
- mcpProtocolEra: "modern",
- mcpProtocolVersion: "2026-07-28",
- mcpServerName: "auto-modern",
- mcpTransportPid: 42_424,
- mcpVersionNegotiationMode: "auto",
- sessionId: "session-1",
- workspaceKey: "workspace-1",
- }),
- );
- expect(infoMock).toHaveBeenCalledWith(
- "MCP server connected",
- expect.objectContaining({
- event: "mcp.server.connected",
- mcpClientName: "zcode-test",
- mcpClientVersion: "9.8.7",
- mcpProtocolEra: "legacy",
- mcpProtocolVersion: "2025-11-25",
- mcpServerName: "explicit-legacy",
- mcpVersionNegotiationMode: "legacy",
- }),
- );
- });
- it("defaults ordinary MCP servers to dual-era auto while keeping explicit era pins", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("browser_use", {
- command: "node",
- protocolVersion: "2026-07-28",
- timeoutMs: 600_000,
- type: "stdio",
- });
- await adapter.connectServer("default-stdio", {
- command: "node",
- type: "stdio",
- });
- await adapter.connectServer("default-http", {
- type: "http",
- url: "https://mcp.example.test",
- });
- await adapter.connectServer("auto-http", {
- protocolVersion: "auto",
- timeoutMs: 2_000,
- type: "http",
- url: "https://mcp.example.test/auto",
- });
- await adapter.connectServer("legacy-http", {
- protocolVersion: "legacy",
- type: "http",
- url: "https://mcp.example.test/legacy",
- });
- await adapter.connectServer("legacy-sse", {
- type: "sse",
- url: "https://mcp.example.test/sse",
- });
- expect(
- capturedClientOptions.map((options) => ({
- versionNegotiation: (options as { versionNegotiation?: unknown }).versionNegotiation,
- })),
- ).toEqual([
- {
- versionNegotiation: {
- mode: { pin: "2026-07-28" },
- probe: { timeoutMs: 600_000 },
- },
- },
- { versionNegotiation: { mode: "auto", probe: { timeoutMs: 5_000 } } },
- { versionNegotiation: { mode: "auto", probe: { timeoutMs: 5_000 } } },
- { versionNegotiation: { mode: "auto", probe: { timeoutMs: 1_000 } } },
- { versionNegotiation: { mode: "legacy" } },
- { versionNegotiation: { mode: "legacy" } },
- ]);
- // MCP Apps:每个 client 都宣告可渲染的 UI mimeType,server 据此决定是否下发 ui 资源。
- for (const options of capturedClientOptions) {
- expect(options).toMatchObject({
- capabilities: {
- extensions: {
- "io.modelcontextprotocol/ui": { mimeTypes: ["text/html;profile=mcp-app"] },
- },
- },
- });
- }
- });
- it("preserves non-object structuredContent from MCP 2026-07-28", async () => {
- clientCallToolMock.mockResolvedValue({
- content: [{ type: "text", text: "ok" }],
- structuredContent: ["first", 2, true],
- });
- const adapter = createMcpAdapter();
- await adapter.connectServer("modern", {
- command: "node",
- protocolVersion: "2026-07-28",
- type: "stdio",
- });
- await expect(
- adapter.callTool({
- serverName: "modern",
- toolName: "array-result",
- }),
- ).resolves.toMatchObject({
- structuredContent: ["first", 2, true],
- });
- });
- it("projects server timeout onto listed MCP tool descriptors", async () => {
- clientListToolsMock.mockResolvedValue({
- tools: [
- {
- name: "long-task",
- inputSchema: {
- type: "object",
- properties: {},
- },
- },
- ],
- });
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", {
- command: "node",
- timeoutMs: 900_000,
- type: "stdio",
- });
- await expect(adapter.listTools()).resolves.toMatchObject([
- {
- serverName: "demo",
- toolName: "long-task",
- name: "mcp__demo__long-task",
- timeoutMs: 900_000,
- },
- ]);
- });
- it("reports connecting status while an MCP server is still starting", async () => {
- let resolveConnect!: () => void;
- clientConnectMock.mockImplementation(
- () =>
- new Promise<void>((resolve) => {
- resolveConnect = resolve;
- }),
- );
- const adapter = createMcpAdapter();
- const connect = adapter.connectServer("demo", {
- command: "node",
- type: "stdio",
- });
- await Promise.resolve();
- await expect(adapter.status()).resolves.toMatchObject({
- demo: {
- status: "connecting",
- transport: "stdio",
- toolCount: 0,
- },
- });
- resolveConnect();
- await expect(connect).resolves.toMatchObject({
- status: "connected",
- toolCount: 0,
- });
- });
- it("bounds callTool waiting for a shared startup connection without cancelling it", async () => {
- let resolveConnect!: () => void;
- clientConnectMock.mockImplementation(
- () =>
- new Promise<void>((resolve) => {
- resolveConnect = resolve;
- }),
- );
- const adapter = createMcpAdapter();
- const connect = adapter.connectServer("demo", {
- command: "node",
- type: "stdio",
- });
- await Promise.resolve();
- const call = adapter.callTool(
- {
- serverName: "demo",
- toolName: "slow-start",
- },
- { timeoutMs: 20 },
- );
- try {
- await expect(observePromiseWithin(call, 200)).resolves.toMatchObject({
- reason: {
- name: "McpTimeoutError",
- },
- status: "rejected",
- });
- await expect(adapter.status()).resolves.toMatchObject({
- demo: { status: "connecting" },
- });
- expect(clientCallToolMock).not.toHaveBeenCalled();
- } finally {
- resolveConnect();
- await Promise.allSettled([connect, call]);
- await adapter.close();
- }
- await expect(adapter.status()).resolves.toEqual({});
- });
- it("includes sanitized recent stdio stderr when MCP server connection fails", async () => {
- const debugMock = vi.fn();
- const warnMock = vi.fn();
- const logger = {
- child: vi.fn(),
- debug: debugMock,
- error: vi.fn(),
- info: vi.fn(),
- warn: warnMock,
- };
- logger.child.mockReturnValue(logger);
- clientConnectMock.mockImplementation(async () => {
- capturedStdioStderrHandlers[0]?.(
- Buffer.from(
- [
- "MYSQL_PASS=super-secret",
- "Authorization: Bearer live-token",
- "fatal: database refused connection",
- ].join("\n"),
- ),
- );
- throw new Error("MCP error -32000: Connection closed");
- });
- const adapter = createMcpAdapter({ logger });
- await expect(
- adapter.connectServer("mysql-local", {
- command: "node",
- type: "stdio",
- }),
- ).resolves.toMatchObject({
- error: expect.stringContaining("fatal: database refused connection"),
- status: "failed",
- });
- expect(debugMock).toHaveBeenCalledWith(
- "MCP stdio stderr",
- expect.objectContaining({
- stderr: expect.stringContaining("fatal: database refused connection"),
- }),
- );
- expect(warnMock).toHaveBeenCalledWith(
- "MCP server connection failed",
- expect.objectContaining({
- error: "MCP error -32000: Connection closed",
- event: "mcp.server.failed",
- stderr: expect.stringContaining("fatal: database refused connection"),
- }),
- );
- const warnContext = warnMock.mock.calls[0]?.[1] as { stderr?: string };
- expect(warnContext.stderr).toContain("MYSQL_PASS=[Redacted]");
- expect(warnContext.stderr).toContain("Authorization: Bearer [Redacted]");
- expect(warnContext.stderr).not.toContain("super-secret");
- expect(warnContext.stderr).not.toContain("live-token");
- const status = (await adapter.status())["mysql-local"];
- expect(status?.error).toContain("MCP error -32000: Connection closed");
- expect(status?.error).toContain("MYSQL_PASS=[Redacted]");
- expect(status?.error).not.toContain("super-secret");
- expect(status?.error).not.toContain("live-token");
- await adapter.close();
- });
- it.each([
- ["python", "ModuleNotFoundError: No module named 'mcp'"],
- ["node", "Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'example-mcp'"],
- ["path", "ENOENT: no such file or directory"],
- ["permission", "PermissionError: [Errno 13] Permission denied"],
- ])(
- "exposes bounded sanitized %s startup evidence without changing failure kind",
- async (_name, cause) => {
- clientConnectMock.mockImplementation(async () => {
- capturedStdioStderrHandlers[0]?.(
- Buffer.from(
- "noise".repeat(1_000) +
- "\n\u001b[31m" +
- cause +
- "\u001b[0m\nAPI_TOKEN=fixture-secret\n",
- ),
- );
- throw new Error("Connection closed");
- });
- const adapter = createMcpAdapter();
- try {
- const status = await adapter.connectServer("broken", { command: "node", type: "stdio" });
- expect(status).toMatchObject({ status: "failed", failureKind: "process_start_failed" });
- expect(status.error).toContain(cause);
- expect(status.error).toContain("Connection closed");
- expect(status.error).not.toContain("\u001b");
- expect(status.error).not.toContain("fixture-secret");
- expect(status.error!.length).toBeLessThanOrEqual(4_520);
- } finally {
- await adapter.close();
- }
- },
- );
- it("keeps HTTP failures separate from recent stdio stderr", async () => {
- clientConnectMock.mockRejectedValue(new Error("network unavailable"));
- const adapter = createMcpAdapter();
- try {
- const status = await adapter.connectServer("http-failure", {
- type: "http",
- url: "https://example.com/mcp",
- headers: { Authorization: "Bearer fixture" },
- });
- expect(status.error).toBe("network unavailable");
- expect(status.failureKind).toBe("network_unreachable");
- } finally {
- await adapter.close();
- }
- });
- it("redacts quoted and escaped credential values before exposing stderr", async () => {
- clientConnectMock.mockImplementation(async () => {
- capturedStdioStderrHandlers[0]?.(
- Buffer.from(
- [
- 'API_TOKEN="first secret with spaces"',
- String.raw`VE_SPEECH_MCP_TOKEN="second escaped\" secret"`,
- String.raw`{"access_token":"third escaped\" secret"}`,
- "ModuleNotFoundError: No module named 'mcp'",
- ].join("\n"),
- ),
- );
- throw new Error("Connection closed");
- });
- const adapter = createMcpAdapter();
- try {
- const status = await adapter.connectServer("quoted-secret", {
- command: "node",
- type: "stdio",
- });
- expect(status.error).toContain("No module named 'mcp'");
- expect(status.error).not.toContain("secret");
- expect(status.error).not.toContain("escaped");
- expect(status.error).not.toContain("spaces");
- } finally {
- await adapter.close();
- }
- });
- it("does not leak a credential whose key name is cut by stderr tail truncation", async () => {
- clientConnectMock.mockImplementation(async () => {
- // 构造保留的 4000 字符尾部恰好从键名 VE_SPEECH_MCP_TOKEN 中间的 KEN 开始。
- const head = "KEN=fixture-secret\nreal failure\n";
- const tail = `${head}${"y".repeat(4000 - head.length - 1)}\n`;
- capturedStdioStderrHandlers[0]?.(Buffer.from(`first line\nVE_SPEECH_MCP_TO${tail}`));
- throw new Error("Connection closed");
- });
- const adapter = createMcpAdapter();
- try {
- const status = await adapter.connectServer("cut-key", { command: "node", type: "stdio" });
- expect(status.error).toContain("real failure");
- expect(status.error).not.toContain("fixture-secret");
- } finally {
- await adapter.close();
- }
- });
- it("keeps stderr after a CR-only boundary when tail truncation cuts a credential key", async () => {
- clientConnectMock.mockImplementation(async () => {
- const tail = `KEN=fixture-secret\rreal failure\r${"y".repeat(4100)}\rreal failure`;
- capturedStdioStderrHandlers[0]?.(Buffer.from(`VE_SPEECH_MCP_TO${tail}`));
- throw new Error("Connection closed");
- });
- const adapter = createMcpAdapter();
- try {
- const status = await adapter.connectServer("cr-only", { command: "node", type: "stdio" });
- expect(status.error).toContain("real failure");
- expect(status.error).not.toContain("fixture-secret");
- } finally {
- await adapter.close();
- }
- });
- it.each([" ", ",", ";", "&"])(
- "fails closed when tail truncation cuts a quoted credential containing %j and no newline remains",
- async (separator) => {
- const warnMock = vi.fn();
- const logger = {
- child: vi.fn(),
- debug: vi.fn(),
- error: vi.fn(),
- info: vi.fn(),
- warn: warnMock,
- };
- logger.child.mockReturnValue(logger);
- clientConnectMock.mockImplementation(async () => {
- // 保留的 4000 字符尾部从 KEN="..." 开始且没有换行,凭证值内含分隔符。
- const tail = `KEN="fixture${separator}leaked-suffix"`;
- // 前缀 16 字符 + 4000 字符,保留的尾部正好从 KEN 开始。
- const input = `VE_SPEECH_MCP_TO${tail}${"y".repeat(4000 - tail.length)}`;
- capturedStdioStderrHandlers[0]?.(Buffer.from(input));
- throw new Error("Connection closed");
- });
- const adapter = createMcpAdapter({ logger });
- try {
- const status = await adapter.connectServer("long-line", { command: "node", type: "stdio" });
- expect(status.error).toContain("Connection closed");
- for (const text of [status.error, JSON.stringify(warnMock.mock.calls)]) {
- expect(text).not.toContain("fixture");
- expect(text).not.toContain("leaked-suffix");
- }
- } finally {
- await adapter.close();
- }
- },
- );
- it("marks the record disconnected and logs recent stderr when the transport closes unexpectedly", async () => {
- const warnMock = vi.fn();
- const logger = {
- child: vi.fn(),
- debug: vi.fn(),
- error: vi.fn(),
- info: vi.fn(),
- warn: warnMock,
- };
- logger.child.mockReturnValue(logger);
- const telemetryEvents: Array<{ kind: string; mcpInstanceId?: string }> = [];
- const telemetry = createMcpTelemetryTracker({
- idSalt: "device-a",
- onEvent: (event) => telemetryEvents.push(event),
- randomId: () => "mcp-instance-1",
- });
- telemetry.registerConnection({
- connectionId: "connection-1",
- isolation: "session",
- serverName: "node_repl",
- });
- telemetry.acquireOwner({
- connectionId: "connection-1",
- ownerId: "lease-1",
- sessionId: "session-1",
- });
- const adapter = createMcpAdapter({
- connectionContext: {
- mcpConnectionId: "connection-1",
- mcpIsolation: "session",
- sessionId: "session-1",
- },
- logger,
- telemetry,
- });
- await adapter.connectServer("node_repl", {
- command: "node",
- type: "stdio",
- });
- capturedStdioStderrHandlers[0]?.(
- Buffer.from("node_repl uncaughtException: boom from async timer\n"),
- );
- capturedClients.at(-1)?.onclose?.();
- expect(warnMock).toHaveBeenCalledWith(
- "MCP server connection lost",
- expect.objectContaining({
- event: "mcp.server.connection_lost",
- mcpServerName: "node_repl",
- mcpTransportPid: 42_424,
- stderr: expect.stringContaining("boom from async timer"),
- }),
- );
- const statuses = await adapter.status();
- expect(statuses.node_repl?.status).toBe("failed");
- expect(statuses.node_repl?.failureKind).toBe("unexpected_disconnect");
- expect(telemetryEvents.map((event) => [event.kind, event.mcpInstanceId])).toEqual([
- ["process_start", "mcp-instance-1"],
- ["process_crash", "mcp-instance-1"],
- ]);
- });
- it("does not report connection_lost when the server is closed deliberately", async () => {
- const warnMock = vi.fn();
- const logger = {
- child: vi.fn(),
- debug: vi.fn(),
- error: vi.fn(),
- info: vi.fn(),
- warn: warnMock,
- };
- logger.child.mockReturnValue(logger);
- const adapter = createMcpAdapter({ logger });
- await adapter.connectServer("node_repl", {
- command: "node",
- type: "stdio",
- });
- await adapter.disconnectServer("node_repl");
- capturedClients.at(-1)?.onclose?.();
- expect(warnMock).not.toHaveBeenCalledWith("MCP server connection lost", expect.anything());
- });
- // Bugfix 回归:HTTP/SSE MCP 被停掉不会派发 onclose,status() 会一直停在 connected。
- // 设置页刷新依赖 pingServer 把这种无声死亡暴露出来。
- it("keeps the server connected when the ping succeeds", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("official_tools", {
- type: "http",
- url: "https://mcp.example/mcp",
- });
- await expect(adapter.pingServer?.("official_tools")).resolves.toBe(true);
- expect((await adapter.status()).official_tools?.status).toBe("connected");
- });
- it("starts shared remote recovery when the ping hits a dead transport", async () => {
- const warnMock = vi.fn();
- const logger = {
- child: vi.fn(),
- debug: vi.fn(),
- error: vi.fn(),
- info: vi.fn(),
- warn: warnMock,
- };
- logger.child.mockReturnValue(logger);
- const adapter = createMcpAdapter({ logger });
- await adapter.connectServer("official_tools", {
- type: "http",
- url: "https://mcp.example/mcp",
- });
- // SDK 本地错误用字符串 code(REQUEST_TIMEOUT / CONNECTION_CLOSED …)。
- clientPingMock.mockRejectedValue(
- Object.assign(new Error("Request timed out"), { code: "REQUEST_TIMEOUT" }),
- );
- const recovering = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(() => recovering.promise);
- await expect(adapter.pingServer?.("official_tools")).resolves.toBe(false);
- expect((await adapter.status()).official_tools).toMatchObject({
- status: "connecting",
- });
- expect(warnMock).toHaveBeenCalledWith(
- "MCP server ping failed",
- expect.objectContaining({
- event: "mcp.server.ping.failed",
- mcpServerName: "official_tools",
- }),
- );
- recovering.resolve();
- await adapter.waitForServer!("official_tools", { timeoutMs: 2000 });
- await adapter.close();
- });
- it("explicitly retries a failed stdio connection without starting background recovery", async () => {
- const adapter = createMcpAdapter();
- const config = { command: "node", type: "stdio" as const };
- await adapter.connectServer("stdio", config);
- capturedClients[0]!.onclose?.();
- expect(clientConnectMock).toHaveBeenCalledTimes(1);
- expect((await adapter.status()).stdio?.status).toBe("failed");
- await adapter.connectServer("stdio", config, { revalidate: true });
- expect(clientConnectMock).toHaveBeenCalledTimes(2);
- expect((await adapter.status()).stdio?.status).toBe("connected");
- await adapter.close();
- });
- // 回归:内建 node_repl 进程被外部终止后,修复前直接进入 failed,工具在下一步离开会话,
- // 模型不再调用,同 workspace 的新会话也复用这条 failed 记录,没有任何重连入口。
- it("restarts a built-in stdio server after an unexpected exit and keeps its catalog meanwhile", async () => {
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "js", inputSchema: { type: "object" } }],
- } as never);
- const adapter = createMcpAdapter();
- try {
- await adapter.connectServer("node_repl", BUILT_IN_STDIO_CONFIG);
- const restart = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(() => restart.promise);
- capturedClients.at(-1)!.onclose?.();
- const restarting = adapter.getToolSnapshot!().node_repl;
- expect(restarting?.status.status).toBe("connecting");
- expect(restarting?.tools?.map((tool) => tool.toolName)).toEqual(["js"]);
- restart.resolve();
- await adapter.waitForServer!("node_repl", { timeoutMs: 1000 });
- expect(adapter.getToolSnapshot!().node_repl?.status.status).toBe("connected");
- expect(clientConnectMock).toHaveBeenCalledTimes(2);
- } finally {
- await adapter.close();
- }
- });
- it("gives up after five built-in stdio restart attempts and drops the retained catalog", async () => {
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "js", inputSchema: { type: "object" } }],
- } as never);
- const adapter = createMcpAdapter();
- await adapter.connectServer("node_repl", BUILT_IN_STDIO_CONFIG);
- const connects = clientConnectMock.mock.calls.length;
- vi.useFakeTimers();
- try {
- clientConnectMock.mockRejectedValue(new Error("spawn failed"));
- capturedClients.at(-1)!.onclose?.();
- expect(adapter.getToolSnapshot!().node_repl?.status.status).toBe("connecting");
- await vi.runAllTimersAsync();
- expect(clientConnectMock.mock.calls.length - connects).toBe(5);
- expect(adapter.getToolSnapshot!().node_repl?.status.status).toBe("failed");
- expect(await adapter.listTools()).toEqual([]);
- } finally {
- vi.useRealTimers();
- await adapter.close();
- }
- });
- it("limits built-in stdio restarts within the restart window", async () => {
- const adapter = createMcpAdapter();
- vi.useFakeTimers({ toFake: ["Date"] });
- try {
- await adapter.connectServer("node_repl", BUILT_IN_STDIO_CONFIG);
- const exitAndSettle = async () => {
- capturedClients.at(-1)!.onclose?.();
- await adapter.waitForServer!("node_repl", { timeoutMs: 1000 });
- return adapter.getToolSnapshot!().node_repl?.status;
- };
- for (let restart = 0; restart < 5; restart++)
- expect((await exitAndSettle())?.status).toBe("connected");
- // 第 6 次退出仍在 10 分钟窗口内:不再自动重启,进入 failed(目录保留见下一个用例)。
- const connects = clientConnectMock.mock.calls.length;
- expect(await exitAndSettle()).toMatchObject({
- status: "failed",
- failureKind: "unexpected_disconnect",
- });
- expect(clientConnectMock.mock.calls.length).toBe(connects);
- // 显式重连重新开始计数。
- await adapter.connectServer("node_repl", BUILT_IN_STDIO_CONFIG, { revalidate: true });
- expect((await exitAndSettle())?.status).toBe("connected");
- for (let restart = 0; restart < 4; restart++) await exitAndSettle();
- expect((await exitAndSettle())?.status).toBe("failed");
- // 窗口过去后再次退出时允许重启。
- await adapter.connectServer("node_repl", BUILT_IN_STDIO_CONFIG, { revalidate: true });
- for (let restart = 0; restart < 5; restart++) await exitAndSettle();
- vi.setSystemTime(Date.now() + 10 * 60_000 + 1);
- expect((await exitAndSettle())?.status).toBe("connected");
- } finally {
- vi.useRealTimers();
- await adapter.close();
- }
- });
- // 回归:超过重启上限后修复前清空目录,工具离开会话,执行准入重连到不了;同 workspace 的新会话
- // 也复用这条 failed 记录,窗口过去后仍不可用。现在保留目录,下一次执行准入重连。
- it("keeps the built-in catalog after the restart limit and restarts on the next execution", async () => {
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "js", inputSchema: { type: "object" } }],
- } as never);
- const adapter = createMcpAdapter();
- try {
- await adapter.connectServer("node_repl", BUILT_IN_STDIO_CONFIG);
- for (let exit = 0; exit < 6; exit++) {
- capturedClients.at(-1)!.onclose?.();
- await adapter.waitForServer!("node_repl", { timeoutMs: 1000 });
- }
- const limited = adapter.getToolSnapshot!().node_repl;
- expect(limited?.status).toMatchObject({
- status: "failed",
- failureKind: "unexpected_disconnect",
- });
- expect(limited?.tools?.map((tool) => tool.toolName)).toEqual(["js"]);
- const connects = clientConnectMock.mock.calls.length;
- const restart = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(() => restart.promise);
- const waiting = adapter.waitForServer!("node_repl", { reconnect: true, timeoutMs: 1000 });
- // 重连期间目录不离开视图。
- const reconnecting = adapter.getToolSnapshot!().node_repl;
- expect(reconnecting?.status.status).toBe("connecting");
- expect(reconnecting?.tools?.map((tool) => tool.toolName)).toEqual(["js"]);
- restart.resolve();
- await expect(waiting).resolves.toEqual({ timedOut: false });
- expect(adapter.getToolSnapshot!().node_repl?.status.status).toBe("connected");
- expect(clientConnectMock.mock.calls.length - connects).toBe(1);
- } finally {
- await adapter.close();
- }
- });
- it("drops the retained built-in catalog when the execution restart fails", async () => {
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "js", inputSchema: { type: "object" } }],
- } as never);
- const adapter = createMcpAdapter();
- try {
- await adapter.connectServer("node_repl", BUILT_IN_STDIO_CONFIG);
- for (let exit = 0; exit < 6; exit++) {
- capturedClients.at(-1)!.onclose?.();
- await adapter.waitForServer!("node_repl", { timeoutMs: 1000 });
- }
- expect(adapter.getToolSnapshot!().node_repl?.tools?.map((tool) => tool.toolName)).toEqual([
- "js",
- ]);
- clientConnectMock.mockRejectedValueOnce(new Error("spawn failed"));
- await adapter.waitForServer!("node_repl", { reconnect: true, timeoutMs: 1000 });
- const row = adapter.getToolSnapshot!().node_repl;
- expect(row?.status.status).toBe("failed");
- expect(row?.status.failureKind).not.toBe("unexpected_disconnect");
- expect(row?.tools).toBeUndefined();
- } finally {
- await adapter.close();
- }
- });
- it("still drops the catalog of an ordinary stdio server after an unexpected exit", async () => {
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "echo", inputSchema: { type: "object" } }],
- } as never);
- const adapter = createMcpAdapter();
- try {
- await adapter.connectServer("stdio", { command: "node", type: "stdio" });
- capturedClients.at(-1)!.onclose?.();
- const row = adapter.getToolSnapshot!().stdio;
- expect(row?.status).toMatchObject({ status: "failed", failureKind: "unexpected_disconnect" });
- expect(row?.tools).toBeUndefined();
- } finally {
- await adapter.close();
- }
- });
- it("treats a JSON-RPC error answer as a live connection", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("official_tools", {
- type: "http",
- url: "https://mcp.example/mcp",
- });
- // server 回了 method-not-found:请求走通了,连接是活的,不能据此拆连接。
- clientPingMock.mockRejectedValue(
- Object.assign(new Error("Method not found"), { code: -32_601 }),
- );
- await expect(adapter.pingServer?.("official_tools")).resolves.toBe(true);
- expect((await adapter.status()).official_tools?.status).toBe("connected");
- });
- it("reports an unknown server as not alive", async () => {
- const adapter = createMcpAdapter();
- await expect(adapter.pingServer?.("missing")).resolves.toBe(false);
- });
- it("does not reconnect a replaced configuration after its health probe fails", async () => {
- const adapter = createMcpAdapter();
- const config = { type: "http" as const, url: "https://mcp.example/old" };
- await adapter.connectServer("docs", config);
- const probe = Promise.withResolvers<Record<string, never>>();
- const probing = Promise.withResolvers<void>();
- clientPingMock.mockImplementationOnce(() => {
- probing.resolve();
- return probe.promise;
- });
- const refresh = adapter.connectServer("docs", config, { revalidate: true });
- await probing.promise;
- await adapter.connectServer("docs", { ...config, url: "https://mcp.example/new" });
- probe.reject(new SdkError(SdkErrorCode.ConnectionClosed, "Connection closed"));
- await refresh;
- expect(clientConnectMock).toHaveBeenCalledTimes(2);
- expect(capturedHttpOptions).toHaveLength(2);
- expect((await adapter.status()).docs?.status).toBe("connected");
- await adapter.close();
- });
- it("does not retry a stdio call whose transport reports not connected", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("node_repl", {
- command: "node",
- type: "stdio",
- });
- clientCallToolMock.mockRejectedValueOnce(new Error("Not connected"));
- // 进程死亡由 onclose 进入 failed,下一次调用先重连;本次调用不重发。
- await expect(
- adapter.callTool({
- arguments: { code: "1 + 1" },
- serverName: "node_repl",
- toolName: "js",
- }),
- ).rejects.toThrow(/Not connected/);
- expect(clientConnectMock).toHaveBeenCalledTimes(1);
- expect(clientCallToolMock).toHaveBeenCalledTimes(1);
- });
- it("reconnects before calling when the record was already marked disconnected", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("node_repl", {
- command: "node",
- type: "stdio",
- });
- capturedClients.at(-1)?.onclose?.();
- expect((await adapter.status()).node_repl?.status).toBe("failed");
- const result = await adapter.callTool({
- arguments: { code: "1 + 1" },
- serverName: "node_repl",
- toolName: "js",
- });
- expect(result.content).toEqual([{ type: "text", text: "ok" }]);
- expect(clientConnectMock).toHaveBeenCalledTimes(2);
- expect((await adapter.status()).node_repl?.status).toBe("connected");
- });
- it("surfaces the original error when reconnection also fails", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("node_repl", {
- command: "node",
- type: "stdio",
- });
- clientCallToolMock.mockRejectedValue(new Error("Not connected"));
- clientConnectMock.mockRejectedValue(new Error("spawn ENOENT"));
- await expect(
- adapter.callTool({
- arguments: { code: "1 + 1" },
- serverName: "node_repl",
- toolName: "js",
- }),
- ).rejects.toThrow(/Not connected/);
- expect(clientCallToolMock).toHaveBeenCalledTimes(1);
- });
- it("keeps transport error codes separate from the connection failure classification", async () => {
- const adapter = createMcpAdapter();
- clientConnectMock.mockRejectedValue(
- Object.assign(new Error("Connection refused"), { cause: { code: "ECONNREFUSED" } }),
- );
- const status = await adapter.connectServer("diagnostic", { command: "node", type: "stdio" });
- expect(status).toMatchObject({ status: "failed", errorCode: "ECONNREFUSED" });
- expect(status.failureKind).not.toBe("ECONNREFUSED");
- await adapter.close();
- });
- it("injects unified network env into stdio MCP transports", async () => {
- const adapter = createMcpAdapter({
- env: {
- PATH: dirname(process.execPath),
- ZCODE_AGENT_CA_CERT: "/tmp/root-ca.pem",
- },
- network: {
- httpProxy: "127.0.0.1:8888",
- noProxy: "localhost,127.0.0.1",
- },
- });
- await adapter.connectServer("demo", {
- command: "node",
- env: {
- MCP_TOKEN: "secret",
- },
- type: "stdio",
- });
- const options = capturedStdioOptions[0] as { env?: Record<string, string> };
- expect(options.env).toMatchObject({
- ALL_PROXY: "http://127.0.0.1:8888",
- CURL_CA_BUNDLE: "/tmp/root-ca.pem",
- GIT_SSL_CAINFO: "/tmp/root-ca.pem",
- HTTPS_PROXY: "http://127.0.0.1:8888",
- HTTP_PROXY: "http://127.0.0.1:8888",
- MCP_TOKEN: "secret",
- NODE_EXTRA_CA_CERTS: "/tmp/root-ca.pem",
- NO_PROXY: "localhost,127.0.0.1",
- // MCP 会补齐 Node 目录;本测试使用已含 Node 的 PATH,避免依赖测试机的 /bin。
- PATH: dirname(process.execPath),
- REQUESTS_CA_BUNDLE: "/tmp/root-ca.pem",
- SSL_CERT_FILE: "/tmp/root-ca.pem",
- all_proxy: "http://127.0.0.1:8888",
- http_proxy: "http://127.0.0.1:8888",
- https_proxy: "http://127.0.0.1:8888",
- no_proxy: "localhost,127.0.0.1",
- });
- });
- it("appends the injected node fallback after the server env", async () => {
- const resolve = vi.fn(async () => ({
- binDir: "/fallback/bin",
- env: { ZCODE_NODE_FALLBACK_EXEC_PATH: "/electron" },
- }));
- const adapter = createMcpAdapter({
- env: { PATH: dirname(process.execPath) },
- nodeFallback: { resolve },
- });
- await adapter.connectServer("demo", {
- command: "node",
- // 插件自带 PATH 会整体替换宿主 PATH;fallback 仍须追加在它后面。
- env: { PATH: "/plugin/bin" },
- type: "stdio",
- });
- const options = capturedStdioOptions[0] as { env?: Record<string, string> };
- expect(options.env?.PATH).toBe(["/plugin/bin", "/fallback/bin"].join(delimiter));
- expect(options.env?.ZCODE_NODE_FALLBACK_EXEC_PATH).toBe("/electron");
- expect(resolve).toHaveBeenCalledTimes(1);
- });
- it("restores captured user network env into stdio MCP transports", async () => {
- const adapter = createMcpAdapter({
- env: {
- [ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY]: JSON.stringify({
- NODE_EXTRA_CA_CERTS: "/tmp/user-ca.pem",
- http_proxy: "http://user-proxy:8080",
- }),
- PATH: dirname(process.execPath),
- },
- network: {},
- });
- await adapter.connectServer("demo", {
- command: "node",
- type: "stdio",
- });
- const options = capturedStdioOptions[0] as { env?: Record<string, string> };
- expect(options.env).toMatchObject({
- NODE_EXTRA_CA_CERTS: "/tmp/user-ca.pem",
- PATH: dirname(process.execPath),
- http_proxy: "http://user-proxy:8080",
- });
- expect(options.env?.[ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY]).toBeUndefined();
- });
- it("passes proxy-aware fetch into HTTP and SSE MCP transports", async () => {
- const adapter = createMcpAdapter({
- env: {
- ZCODE_HTTP_PROXY: "http://127.0.0.1:8888",
- },
- network: {},
- });
- await adapter.connectServer("http-demo", {
- type: "http",
- url: "https://mcp.example.test",
- });
- await adapter.connectServer("sse-demo", {
- type: "sse",
- url: "https://mcp.example.test/sse",
- });
- expect(capturedHttpOptions[0]).toMatchObject({
- fetch: expect.any(Function),
- });
- expect(capturedSseOptions[0]).toMatchObject({
- fetch: expect.any(Function),
- });
- await adapter.close();
- });
- it("passes a pure token AuthProvider into bare HTTP and SSE MCP transports", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("http-demo", {
- type: "http",
- url: "https://mcp.example.test",
- });
- await adapter.connectServer("sse-demo", {
- type: "sse",
- url: "https://mcp.example.test/sse",
- });
- const providers = [
- (capturedHttpOptions[0] as { authProvider?: Record<string, unknown> }).authProvider,
- (capturedSseOptions[0] as { authProvider?: Record<string, unknown> }).authProvider,
- ];
- for (const provider of providers) {
- expect(provider).toBeDefined();
- // 修复原因(根因 1):运行期只能是纯 AuthProvider。SDK 的 `isOAuthClientProvider()` 判定
- // 依赖 `clientInformation`/`tokens` 等方法是否存在;一旦被识别为 OAuthClientProvider,
- // transport 就会通过 `adaptOAuthProvider` 包裹它,401 走 SDK 的 `auth()`,绕过我们的
- // refresh 单飞锁,rotation 竞态立刻复发。同时它还会带来 discovery / DCR / callback listener。
- expect(typeof provider?.token).toBe("function");
- expect(typeof provider?.onUnauthorized).toBe("function");
- expect(provider?.clientInformation).toBeUndefined();
- expect(provider?.tokens).toBeUndefined();
- expect(provider?.saveTokens).toBeUndefined();
- expect(provider?.clientMetadata).toBeUndefined();
- expect(provider?.redirectToAuthorization).toBeUndefined();
- expect(provider?.saveCodeVerifier).toBeUndefined();
- }
- await adapter.close();
- });
- it("does not attach implicit OAuth provider when HTTP MCP has an Authorization header", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("manual-token", {
- type: "http",
- url: "https://mcp.example.test",
- headers: {
- Authorization: "Bearer user-token",
- },
- });
- expect((capturedHttpOptions[0] as { authProvider?: unknown }).authProvider).toBeUndefined();
- await adapter.close();
- });
- it("passes OAuth client credentials providers into HTTP and SSE MCP transports", async () => {
- const adapter = createMcpAdapter({
- clientName: "zcode-test",
- });
- await adapter.connectServer("http-demo", {
- type: "http",
- url: "https://mcp.example.test",
- headers: {
- "X-ZCode": "1",
- },
- oauth: {
- type: "client_credentials",
- clientId: "http-client",
- clientSecret: "http-secret",
- scope: "mcp:tools",
- },
- });
- await adapter.connectServer("sse-demo", {
- type: "sse",
- url: "https://mcp.example.test/sse",
- oauth: {
- type: "client_credentials",
- clientId: "sse-client",
- clientSecret: "sse-secret",
- },
- });
- const httpOptions = capturedHttpOptions[0] as {
- authProvider?: {
- clientInformation(): { client_id: string; client_secret?: string };
- clientMetadata: {
- client_name?: string;
- grant_types?: string[];
- scope?: string;
- };
- };
- requestInit?: { headers?: Record<string, string> };
- };
- const sseOptions = capturedSseOptions[0] as {
- authProvider?: {
- clientInformation(): { client_id: string; client_secret?: string };
- clientMetadata: { client_name?: string; grant_types?: string[] };
- };
- };
- expect(httpOptions.requestInit?.headers).toEqual({ "X-ZCode": "1" });
- expect(httpOptions.authProvider?.clientInformation()).toEqual({
- client_id: "http-client",
- client_secret: "http-secret",
- });
- expect(httpOptions.authProvider?.clientMetadata).toMatchObject({
- client_name: "zcode-test-http-demo",
- grant_types: ["client_credentials"],
- scope: "mcp:tools",
- });
- expect(sseOptions.authProvider?.clientInformation()).toEqual({
- client_id: "sse-client",
- client_secret: "sse-secret",
- });
- expect(sseOptions.authProvider?.clientMetadata).toMatchObject({
- client_name: "zcode-test-sse-demo",
- grant_types: ["client_credentials"],
- });
- });
- it("exposes pending OAuth authorization URL through server status", async () => {
- const { release: resolveConnect } = stubInteractiveAuthorization(
- "https://auth.example.test/authorize?state=state_test",
- );
- const adapter = createMcpAdapter();
- const connect = adapter.connectServer("notion", {
- type: "http",
- url: "https://mcp.example.test/mcp",
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- },
- });
- let authorizationUrl: string | undefined;
- for (let attempt = 0; attempt < 20; attempt += 1) {
- const status = await adapter.status();
- authorizationUrl = status.notion?.authorization?.authorizationUrl;
- if (authorizationUrl) break;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- expect(authorizationUrl).toBe("https://auth.example.test/authorize?state=state_test");
- resolveConnect();
- await expect(connect).resolves.toMatchObject({
- status: "connected",
- toolCount: 0,
- });
- await adapter.close();
- });
- it("reuses an unchanged configured connection while OAuth authorization is pending", async () => {
- const { release: resolveConnect } = stubInteractiveAuthorization(
- "https://auth.example.test/authorize?state=stable_state",
- );
- const adapter = createMcpAdapter();
- const firstConnect = adapter.connectConfiguredServers({
- notion: {
- type: "http",
- url: "https://mcp.example.test/mcp",
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- },
- },
- });
- let authorizationUrl: string | undefined;
- for (let attempt = 0; attempt < 20; attempt += 1) {
- const status = await adapter.status();
- authorizationUrl = status.notion?.authorization?.authorizationUrl;
- if (authorizationUrl) break;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- const secondConnect = adapter.connectConfiguredServers({
- notion: {
- type: "http",
- url: "https://mcp.example.test/mcp",
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- },
- },
- });
- await Promise.resolve();
- expect(authorizationUrl).toBe("https://auth.example.test/authorize?state=stable_state");
- await expect(
- adapter.connectServer(
- "notion",
- {
- type: "http",
- url: "https://mcp.example.test/mcp",
- oauth: { type: "authorization_code", clientName: "ZCode" },
- },
- { revalidate: true },
- ),
- ).resolves.toMatchObject({
- status: "connecting",
- authorization: { authorizationUrl },
- });
- expect(clientConnectMock).toHaveBeenCalledTimes(1);
- // Phase 1 的探测 transport 在进入 Phase 2 前一定会被关闭(negotiation 失败后不可复用),
- // 所以这里断言的是「第二个 caller 没有另起一条连接」,而不是 transport 从未关闭。
- expect(capturedHttpOptions).toHaveLength(1);
- await expect(adapter.status()).resolves.toMatchObject({
- notion: {
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=stable_state",
- },
- status: "connecting",
- },
- });
- resolveConnect();
- await expect(Promise.all([firstConnect, secondConnect])).resolves.toMatchObject([
- { statuses: { notion: { status: "connected" } } },
- { statuses: { notion: { status: "connected" } } },
- ]);
- await adapter.close();
- });
- it("keeps a reused OAuth connection alive when a bounded caller times out", async () => {
- const { release: resolveConnect } = stubInteractiveAuthorization(
- "https://auth.example.test/authorize?state=shared_timeout_state",
- );
- const adapter = createMcpAdapter();
- const servers = {
- notion: {
- type: "http" as const,
- url: "https://mcp.example.test/mcp",
- oauth: {
- type: "authorization_code" as const,
- clientName: "ZCode",
- },
- },
- };
- const settingsConnect = adapter.connectConfiguredServers(servers);
- for (let attempt = 0; attempt < 20; attempt += 1) {
- const status = await adapter.status();
- if (status.notion?.authorization?.authorizationUrl) break;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- const sessionConnect = await adapter.connectConfiguredServers(servers, {
- oauthAuthorizationTimeoutMs: 20,
- });
- expect(sessionConnect.statuses.notion).toMatchObject({
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=shared_timeout_state",
- },
- status: "connecting",
- });
- expect(clientConnectMock).toHaveBeenCalledTimes(1);
- // Phase 1 的探测 transport 在进入 Phase 2 前一定会被关闭(negotiation 失败后不可复用),
- // 所以这里断言的是「第二个 caller 没有另起一条连接」,而不是 transport 从未关闭。
- expect(capturedHttpOptions).toHaveLength(1);
- resolveConnect();
- await expect(settingsConnect).resolves.toMatchObject({
- statuses: { notion: { status: "connected" } },
- });
- await adapter.close();
- });
- it("only cancels the caller waiter when a reused OAuth connection is aborted", async () => {
- const { release: resolveConnect } = stubInteractiveAuthorization(
- "https://auth.example.test/authorize?state=shared_abort_state",
- );
- const adapter = createMcpAdapter();
- const servers = {
- notion: {
- type: "http" as const,
- url: "https://mcp.example.test/mcp",
- oauth: {
- type: "authorization_code" as const,
- clientName: "ZCode",
- },
- },
- };
- const settingsConnect = adapter.connectConfiguredServers(servers);
- for (let attempt = 0; attempt < 20; attempt += 1) {
- const status = await adapter.status();
- if (status.notion?.authorization?.authorizationUrl) break;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- const abortController = new AbortController();
- const sessionConnect = adapter.connectConfiguredServers(servers, {
- signal: abortController.signal,
- });
- abortController.abort(new Error("session stopped"));
- await expect(sessionConnect).resolves.toMatchObject({
- statuses: {
- notion: {
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=shared_abort_state",
- },
- status: "connecting",
- },
- },
- });
- expect(clientConnectMock).toHaveBeenCalledTimes(1);
- // Phase 1 的探测 transport 在进入 Phase 2 前一定会被关闭(negotiation 失败后不可复用),
- // 所以这里断言的是「第二个 caller 没有另起一条连接」,而不是 transport 从未关闭。
- expect(capturedHttpOptions).toHaveLength(1);
- resolveConnect();
- await expect(settingsConnect).resolves.toMatchObject({
- statuses: { notion: { status: "connected" } },
- });
- await adapter.close();
- });
- it("treats per-connect OAuth authorization timeout as a caller budget, not a transaction kill", async () => {
- let startAuthorization!: () => void;
- const beforeAuthorization = new Promise<void>((resolve) => {
- startAuthorization = resolve;
- });
- const { release, ready } = stubInteractiveAuthorization(
- "https://auth.example.test/authorize?state=state_test",
- beforeAuthorization,
- );
- const adapter = createMcpAdapter();
- const startedAt = Date.now();
- try {
- const status = await adapter.connectServer(
- "notion",
- {
- type: "http",
- url: "https://mcp.example.test/mcp",
- timeoutMs: 2_000,
- oauth: { type: "authorization_code", clientName: "ZCode" },
- },
- { oauthAuthorizationTimeoutMs: 20 },
- );
- // URL 发布含异步存储,不能假定它比 20ms caller 预算更快;事务应在 caller 返回后继续。
- expect(status).toMatchObject({ status: "connecting" });
- expect(status.authorization).toBeUndefined();
- expect(Date.now() - startedAt).toBeLessThan(1_000);
- expect(transportFinishAuthMock).not.toHaveBeenCalled();
- startAuthorization();
- await ready;
- await expect(adapter.status()).resolves.toMatchObject({
- notion: {
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=state_test",
- },
- status: "connecting",
- },
- });
- release();
- await vi.waitFor(async () => {
- expect(await adapter.status()).toMatchObject({ notion: { status: "connected" } });
- });
- } finally {
- startAuthorization();
- release();
- await adapter.close();
- }
- });
- it("keeps runtime OAuth recovery alive after the triggering caller times out", async () => {
- let startAuthorization!: () => void;
- const beforeAuthorization = new Promise<void>((resolve) => {
- startAuthorization = resolve;
- });
- let markReady!: () => void;
- const ready = new Promise<void>((resolve) => {
- markReady = resolve;
- });
- let releaseAuthorization!: () => void;
- const authorizationGate = new Promise<void>((resolve) => {
- releaseAuthorization = resolve;
- });
- authMock.mockImplementation(async (provider: unknown) => {
- const interactiveProvider = provider as {
- redirectToAuthorization(url: URL): Promise<void>;
- state(): Promise<string>;
- };
- const authorizationUrl = new URL("https://auth.example.test/authorize");
- authorizationUrl.searchParams.set("state", await interactiveProvider.state());
- await beforeAuthorization;
- await interactiveProvider.redirectToAuthorization(authorizationUrl);
- markReady();
- await authorizationGate;
- return "AUTHORIZED" as const;
- });
- clientCallToolMock
- .mockRejectedValueOnce(new UnauthorizedError())
- .mockResolvedValue({ content: [{ type: "text", text: "ok" }] });
- const adapter = createMcpAdapter();
- await adapter.connectServer("runtime", {
- oauth: {
- type: "authorization_code",
- },
- type: "http",
- url: "https://mcp.example.test/mcp",
- });
- const call = adapter.callTool(
- {
- serverName: "runtime",
- toolName: "oauth_ping",
- },
- { timeoutMs: 20 },
- );
- try {
- await expect(observePromiseWithin(call, 200)).resolves.toMatchObject({
- reason: {
- name: "McpTimeoutError",
- },
- status: "rejected",
- });
- // 显式让 caller 先超时,再发布 URL,避免靠机器负载决定断言先后。
- startAuthorization();
- await ready;
- await expect(adapter.status()).resolves.toMatchObject({
- runtime: {
- authorization: {
- authorizationUrl: expect.stringContaining("https://auth.example.test/authorize"),
- },
- status: "connecting",
- },
- });
- releaseAuthorization();
- for (let attempt = 0; attempt < 50; attempt += 1) {
- const status = await adapter.status();
- if (status.runtime?.status === "connected") break;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- await expect(adapter.status()).resolves.toMatchObject({
- runtime: { status: "connected" },
- });
- await expect(
- adapter.callTool(
- {
- serverName: "runtime",
- toolName: "oauth_ping",
- },
- { timeoutMs: 200 },
- ),
- ).resolves.toMatchObject({
- content: [{ text: "ok", type: "text" }],
- });
- expect(authMock).toHaveBeenCalledTimes(1);
- // 原 caller 已经超时,后台 recovery 只负责重连;不会替超时 caller 再执行工具。
- expect(clientCallToolMock).toHaveBeenCalledTimes(2);
- } finally {
- startAuthorization();
- releaseAuthorization();
- await Promise.allSettled([call]);
- await adapter.close();
- }
- });
- it("shares one runtime OAuth recovery across concurrent tool callers", async () => {
- let releaseAuthorization!: () => void;
- const authorizationGate = new Promise<void>((resolve) => {
- releaseAuthorization = resolve;
- });
- authMock.mockImplementation(async (provider: unknown) => {
- const interactiveProvider = provider as {
- redirectToAuthorization(url: URL): Promise<void>;
- state(): Promise<string>;
- };
- const authorizationUrl = new URL("https://auth.example.test/authorize");
- authorizationUrl.searchParams.set("state", await interactiveProvider.state());
- await interactiveProvider.redirectToAuthorization(authorizationUrl);
- await authorizationGate;
- return "AUTHORIZED" as const;
- });
- clientCallToolMock
- .mockRejectedValueOnce(new UnauthorizedError())
- .mockResolvedValue({ content: [{ type: "text", text: "ok" }] });
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "oauth_ping", inputSchema: { type: "object" } }],
- } as never);
- const adapter = createMcpAdapter();
- await adapter.connectServer("runtime", {
- oauth: {
- type: "authorization_code",
- },
- type: "http",
- url: "https://mcp.example.test/mcp",
- });
- const firstCall = adapter.callTool(
- {
- serverName: "runtime",
- toolName: "oauth_ping",
- },
- { timeoutMs: 2_000 },
- );
- // 立即挂上处理:触发授权的调用在链接发布时就会 reject,晚挂会被记为未处理 rejection。
- const firstOutcome = firstCall.then(
- () => "resolved",
- (error: unknown) => error,
- );
- try {
- for (let attempt = 0; attempt < 50; attempt += 1) {
- const status = await adapter.status();
- if (status.runtime?.authorization?.authorizationUrl) break;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- await expect(adapter.status()).resolves.toMatchObject({
- runtime: {
- authorization: {
- authorizationUrl: expect.stringContaining("https://auth.example.test/authorize"),
- },
- status: "connecting",
- },
- });
- // 触发授权的调用在链接发布后以原始错误结束,由 core 改写为 needs-auth 文本。
- expect(String(await firstOutcome)).toMatch(/Unauthorized/);
- const secondCall = adapter.callTool(
- {
- serverName: "runtime",
- toolName: "oauth_ping",
- },
- { timeoutMs: 2_000 },
- );
- await Promise.resolve();
- expect(clientCallToolMock).toHaveBeenCalledTimes(1);
- releaseAuthorization();
- await expect(secondCall).resolves.toMatchObject({
- content: [{ text: "ok", type: "text" }],
- });
- expect(authMock).toHaveBeenCalledTimes(1);
- // 第一次认证失败 + second caller 等到同一轮授权完成后的首次实际调用;不另起授权。
- expect(clientCallToolMock).toHaveBeenCalledTimes(2);
- await expect(adapter.status()).resolves.toMatchObject({
- runtime: { status: "connected" },
- });
- } finally {
- releaseAuthorization();
- await Promise.allSettled([firstCall]);
- await adapter.close();
- }
- });
- it("does not let a caller abort close the shared runtime OAuth callback listener", async () => {
- let authorizationContext:
- | {
- authorizationUrl: string;
- redirectUrl: string;
- }
- | undefined;
- // 链接发布后触发调用会立即结束;在发布前中止调用,才能覆盖"调用方中止不关闭共享监听"。
- const entered = Promise.withResolvers<void>();
- const publish = Promise.withResolvers<void>();
- authMock.mockImplementation(
- async (provider: unknown, options?: { authorizationCode?: string }) => {
- if (options?.authorizationCode) return "AUTHORIZED" as const;
- entered.resolve();
- await publish.promise;
- const interactiveProvider = provider as {
- redirectToAuthorization(url: URL): Promise<void>;
- state(): Promise<string>;
- };
- const authorizationUrl = new URL("https://auth.example.test/authorize");
- authorizationUrl.searchParams.set("state", await interactiveProvider.state());
- await interactiveProvider.redirectToAuthorization(authorizationUrl);
- return "REDIRECT" as never;
- },
- );
- clientCallToolMock
- .mockRejectedValueOnce(new UnauthorizedError())
- .mockResolvedValue({ content: [{ type: "text", text: "ok" }] });
- const adapter = createMcpAdapter({
- mcpOAuth: {
- onAuthorizationRequired: async (context) => {
- authorizationContext = context;
- },
- },
- });
- await adapter.connectServer("runtime", {
- oauth: {
- type: "authorization_code",
- },
- type: "http",
- url: "https://mcp.example.test/mcp",
- });
- const abortController = new AbortController();
- const call = adapter.callTool(
- {
- serverName: "runtime",
- toolName: "oauth_ping",
- },
- {
- signal: abortController.signal,
- timeoutMs: 2_000,
- },
- );
- try {
- await entered.promise;
- abortController.abort(new Error("caller stopped"));
- await expect(observePromiseWithin(call, 200)).resolves.toMatchObject({
- reason: {
- message: "caller stopped",
- },
- status: "rejected",
- });
- publish.resolve();
- for (let attempt = 0; attempt < 50; attempt += 1) {
- if (authorizationContext) break;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- expect(authorizationContext).toBeDefined();
- const callbackUrl = new URL(authorizationContext!.redirectUrl);
- callbackUrl.searchParams.set("code", "authorized-after-caller-abort");
- callbackUrl.searchParams.set(
- "state",
- new URL(authorizationContext!.authorizationUrl).searchParams.get("state") ?? "",
- );
- await expect(fetch(callbackUrl)).resolves.toMatchObject({ ok: true });
- for (let attempt = 0; attempt < 50; attempt += 1) {
- const status = await adapter.status();
- if (status.runtime?.status === "connected") break;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- await expect(adapter.status()).resolves.toMatchObject({
- runtime: { status: "connected" },
- });
- expect(authMock).toHaveBeenCalledTimes(2);
- expect(clientConnectMock).toHaveBeenCalledTimes(2);
- } finally {
- publish.resolve();
- await Promise.allSettled([call]);
- await adapter.close();
- }
- });
- describe("tool-call authorization", () => {
- const OAUTH_SERVER = {
- type: "http" as const,
- url: "https://mcp.example.test/mcp",
- oauth: { type: "authorization_code" as const },
- };
- const OAUTH_TOOLS = { tools: [{ name: "oauth_ping", inputSchema: { type: "object" } }] };
- const AUTHORIZATION_URL = "https://auth.example.test/authorize";
- const RECONNECT_WAIT_CAP_MS = 30_000;
- const STATUS_POLL_ATTEMPTS = 50;
- const STATUS_POLL_INTERVAL_MS = 10;
- /** 发布授权链接后停在 gate 上;gate 以 error 结束时模拟授权到期或失败。 */
- function publishAuthorizationThen(gate: Promise<"AUTHORIZED" | Error>) {
- authMock.mockImplementation(async (provider: unknown) => {
- const interactive = provider as {
- redirectToAuthorization(url: URL): Promise<void>;
- state(): Promise<string>;
- };
- const url = new URL(AUTHORIZATION_URL);
- url.searchParams.set("state", await interactive.state());
- await interactive.redirectToAuthorization(url);
- const outcome = await gate;
- if (outcome instanceof Error) throw outcome;
- return outcome;
- });
- }
- async function waitForSnapshot(
- adapter: ReturnType<typeof createMcpAdapter>,
- predicate: (row: ReturnType<typeof adapter.getToolSnapshot>[string] | undefined) => boolean,
- ) {
- for (let attempt = 0; attempt < STATUS_POLL_ATTEMPTS; attempt += 1) {
- if (predicate(adapter.getToolSnapshot().runtime)) return;
- await new Promise((resolve) => setTimeout(resolve, STATUS_POLL_INTERVAL_MS));
- }
- throw new Error(`snapshot did not converge: ${JSON.stringify(adapter.getToolSnapshot())}`);
- }
- /** 调用期 401 触发授权,授权以 error 结束;返回保留了目录的 adapter。 */
- async function failToolCallAuthorization(failure: Error = new Error("authorization expired")) {
- const gate = Promise.withResolvers<"AUTHORIZED" | Error>();
- publishAuthorizationThen(gate.promise);
- clientListToolsMock.mockResolvedValue(OAUTH_TOOLS as never);
- clientCallToolMock.mockRejectedValueOnce(new UnauthorizedError());
- const adapter = createMcpAdapter();
- await adapter.connectServer("runtime", OAUTH_SERVER);
- // 立即挂上处理:调用在链接发布时就会 reject,晚挂会被记为未处理 rejection。
- const call = Promise.allSettled([
- adapter.callTool({ serverName: "runtime", toolName: "oauth_ping" }),
- ]);
- await waitForSnapshot(adapter, (row) => Boolean(row?.status.authorization));
- gate.resolve(failure);
- await call;
- await waitForSnapshot(
- adapter,
- (row) => row?.status.failureKind === "oauth_authorization_failed",
- );
- return adapter;
- }
- it("ends the triggering call once the authorization link is published", async () => {
- const gate = Promise.withResolvers<"AUTHORIZED" | Error>();
- publishAuthorizationThen(gate.promise);
- clientListToolsMock.mockResolvedValue(OAUTH_TOOLS as never);
- clientCallToolMock.mockRejectedValueOnce(new UnauthorizedError());
- const adapter = createMcpAdapter();
- await adapter.connectServer("runtime", OAUTH_SERVER);
- const call = adapter.callTool(
- { serverName: "runtime", toolName: "oauth_ping" },
- { timeoutMs: RECONNECT_WAIT_CAP_MS },
- );
- try {
- // 修复前要等满 30 s 才以工具超时结束,模型不知道需要用户授权。
- const observed = await observePromiseWithin(call, 1_000);
- expect(observed.status).toBe("rejected");
- expect(String((observed as { reason: unknown }).reason)).toMatch(/Unauthorized/);
- expect(adapter.getToolSnapshot().runtime?.status.authorization?.authorizationUrl).toContain(
- AUTHORIZATION_URL,
- );
- // 授权仍在后台进行;完成后连接恢复,已结束的调用不被重放。
- gate.resolve("AUTHORIZED");
- await waitForSnapshot(adapter, (row) => row?.status.status === "connected");
- expect(clientCallToolMock).toHaveBeenCalledTimes(1);
- } finally {
- gate.resolve("AUTHORIZED");
- await Promise.allSettled([call]);
- await adapter.close();
- }
- });
- it("returns the original error when the reconnect wait reaches its cap", async () => {
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "ping", inputSchema: { type: "object" } }],
- } as never);
- const adapter = createMcpAdapter();
- // 连接超时长于重连等待上限,等待一定先被上限截断。
- await adapter.connectServer("demo", {
- type: "http",
- url: "https://mcp.example.test/mcp",
- timeoutMs: RECONNECT_WAIT_CAP_MS * 10,
- });
- const reconnect = Promise.withResolvers<void>();
- clientConnectMock.mockImplementation(() => reconnect.promise);
- clientCallToolMock.mockRejectedValueOnce(
- new SdkError(SdkErrorCode.ConnectionClosed, "Connection closed"),
- );
- vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
- try {
- const observed = adapter
- .callTool(
- { serverName: "demo", toolName: "ping" },
- { timeoutMs: RECONNECT_WAIT_CAP_MS * 4 },
- )
- .then(
- () => undefined,
- (error: unknown) => error,
- );
- await vi.advanceTimersByTimeAsync(RECONNECT_WAIT_CAP_MS);
- const error = await observed;
- // 修复前报 McpTimeoutError,模型以为工具超时,实际是连接关闭且仍在恢复。
- expect(error).toBeInstanceOf(SdkError);
- expect(String(error)).toContain("Connection closed");
- } finally {
- vi.useRealTimers();
- reconnect.resolve();
- await adapter.close();
- }
- });
- it("keeps the tool directory when tool-call authorization ends without completing", async () => {
- const adapter = await failToolCallAuthorization();
- try {
- // 修复前授权失败会清空目录,且之后没有任何重连入口,server 永久停在 needs-auth。
- expect(adapter.getToolSnapshot().runtime?.tools?.map((item) => item.toolName)).toEqual([
- "oauth_ping",
- ]);
- } finally {
- await adapter.close();
- }
- });
- it("keeps the tool directory when the authorization transaction times out", async () => {
- // 授权事务到期由 withTimeout 抛出 McpTimeoutError。修复前 failConnection 先按超时归类为
- // connection_timeout,覆盖调用方传入的 oauth_authorization_failed:目录被清空,也没有重连入口。
- const adapter = await failToolCallAuthorization(
- new McpTimeoutError("MCP server runtime OAuth authorization timed out"),
- );
- try {
- expect(adapter.getToolSnapshot().runtime?.tools?.map((item) => item.toolName)).toEqual([
- "oauth_ping",
- ]);
- } finally {
- await adapter.close();
- }
- });
- it("does not start an authorization when an MCP App reconnects", async () => {
- const adapter = await failToolCallAuthorization();
- try {
- authMock.mockClear();
- clientConnectMock.mockClear();
- // 打开 MCP App 只传 reconnect:修复前会走授权失败重连,发起交互授权并发布链接。
- await expect(
- adapter.waitForServer("runtime", { reconnect: true, timeoutMs: 5_000 }),
- ).resolves.toEqual({ timedOut: false });
- expect(clientConnectMock).not.toHaveBeenCalled();
- expect(authMock).not.toHaveBeenCalled();
- const row = adapter.getToolSnapshot().runtime;
- expect(row?.status.failureKind).toBe("oauth_authorization_failed");
- expect(row?.status.authorization).toBeUndefined();
- } finally {
- await adapter.close();
- }
- });
- it("reconnects once on the next execution with credentials authorized elsewhere", async () => {
- const adapter = await failToolCallAuthorization();
- try {
- // 另一进程(设置页)已完成授权:Phase 1 直接连上,不再发起交互授权。
- authMock.mockClear();
- clientConnectMock.mockClear();
- clientConnectMock.mockResolvedValue(undefined);
- const waits = [
- adapter.waitForServer("runtime", {
- reconnect: true,
- reauthorize: true,
- timeoutMs: 5_000,
- }),
- adapter.waitForServer("runtime", {
- reconnect: true,
- reauthorize: true,
- timeoutMs: 5_000,
- }),
- ];
- // 重连期间保留目录,工具不会离开模型视图。
- expect(adapter.getToolSnapshot().runtime?.tools?.map((item) => item.toolName)).toEqual([
- "oauth_ping",
- ]);
- await expect(Promise.all(waits)).resolves.toEqual([
- { timedOut: false },
- { timedOut: false },
- ]);
- expect(adapter.getToolSnapshot().runtime?.status.status).toBe("connected");
- expect(clientConnectMock).toHaveBeenCalledTimes(1);
- expect(authMock).not.toHaveBeenCalled();
- await expect(
- adapter.callTool({ serverName: "runtime", toolName: "oauth_ping" }),
- ).resolves.toMatchObject({ content: [{ text: "ok" }] });
- } finally {
- await adapter.close();
- }
- });
- it("starts a new authorization on the next execution when credentials are still missing", async () => {
- const adapter = await failToolCallAuthorization();
- const gate = Promise.withResolvers<"AUTHORIZED" | Error>();
- try {
- publishAuthorizationThen(gate.promise);
- clientConnectMock.mockRejectedValueOnce(new UnauthorizedError());
- // 授权链接发布即结束等待,调用方据此立即返回 needs-auth。
- await expect(
- observePromiseWithin(
- adapter.waitForServer("runtime", {
- reconnect: true,
- reauthorize: true,
- timeoutMs: 5_000,
- }),
- 1_000,
- ),
- ).resolves.toEqual({ status: "fulfilled", value: { timedOut: false } });
- const row = adapter.getToolSnapshot().runtime;
- expect(row?.status.authorization?.authorizationUrl).toContain(AUTHORIZATION_URL);
- expect(row?.tools?.map((item) => item.toolName)).toEqual(["oauth_ping"]);
- } finally {
- gate.resolve(new Error("closed"));
- await adapter.close();
- }
- });
- });
- it("converges Phase 2 orchestration errors to a retryable failed server status", async () => {
- const credentialError = new Error("Unable to read shared ZCode credentials");
- const credentialStore = {
- filePath: "/tmp/zcode-unreadable-credentials.json",
- load: vi.fn(async () => {
- throw credentialError;
- }),
- loadMany: vi.fn(async () => {
- throw credentialError;
- }),
- } as unknown as SharedZCodeCredentialStore;
- clientConnectMock.mockRejectedValue(new UnauthorizedError());
- const adapter = createMcpAdapter({
- mcpOAuth: {
- credentialStore,
- },
- });
- const config = {
- oauth: {
- type: "authorization_code" as const,
- },
- type: "http" as const,
- url: "https://mcp.example.test/mcp",
- };
- try {
- await expect(adapter.connectConfiguredServers({ broken: config })).resolves.toMatchObject({
- statuses: {
- broken: {
- error: "Unable to read shared ZCode credentials",
- failureKind: "oauth_authorization_failed",
- status: "failed",
- },
- },
- });
- await expect(adapter.status()).resolves.toMatchObject({
- broken: {
- failureKind: "oauth_authorization_failed",
- status: "failed",
- },
- });
- await expect(adapter.connectConfiguredServers({ broken: config })).resolves.toMatchObject({
- statuses: {
- broken: {
- failureKind: "oauth_authorization_failed",
- status: "failed",
- },
- },
- });
- // 相同配置 reconcile 不重试失败连接;显式连接操作(revalidate)仍可重试。
- expect(clientConnectMock).toHaveBeenCalledTimes(1);
- await adapter.connectServer("broken", config, { revalidate: true });
- expect(clientConnectMock).toHaveBeenCalledTimes(2);
- } finally {
- await adapter.close();
- }
- });
- it("only opens OAuth authorization URL when an opener is injected", async () => {
- const openAuthorizationUrl = vi.fn(async () => {});
- const { release: resolveConnect } = stubInteractiveAuthorization(
- "https://auth.example.test/authorize?state=state_test",
- );
- const adapter = createMcpAdapter({
- mcpOAuth: {
- openAuthorizationUrl,
- },
- });
- const connect = adapter.connectServer("notion", {
- type: "http",
- url: "https://mcp.example.test/mcp",
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- },
- });
- for (let attempt = 0; attempt < 20; attempt += 1) {
- const status = await adapter.status();
- if (status.notion?.authorization?.authorizationUrl) break;
- await new Promise((resolve) => setTimeout(resolve, 10));
- }
- expect(openAuthorizationUrl).toHaveBeenCalledWith(
- expect.objectContaining({
- authorizationUrl: "https://auth.example.test/authorize?state=state_test",
- serverName: "notion",
- }),
- );
- resolveConnect();
- await expect(connect).resolves.toMatchObject({
- status: "connected",
- toolCount: 0,
- });
- await adapter.close();
- });
- describe("async startup snapshot and wait", () => {
- const LIST_CHANGED = "notifications/tools/list_changed";
- const stdio = { type: "stdio" as const, command: "node" };
- const tool = (name: string) => ({ name, inputSchema: { type: "object", properties: {} } });
- it("reads memory only: no tools while connecting, an empty list is distinct after connect", async () => {
- const gate = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(() => gate.promise);
- const adapter = createMcpAdapter();
- const connecting = adapter.connectServer("demo", stdio);
- await vi.waitFor(() => expect(clientConnectMock).toHaveBeenCalledOnce());
- const before = adapter.getToolSnapshot!();
- expect(before.demo?.status.status).toBe("connecting");
- expect(before.demo?.tools).toBeUndefined();
- expect(before.demo?.toolsVersion).toBeUndefined();
- gate.resolve();
- await connecting;
- const after = adapter.getToolSnapshot!();
- expect(after.demo?.status.status).toBe("connected");
- expect(after.demo?.tools).toEqual([]);
- expect(after.demo?.toolsVersion).toEqual(expect.any(String));
- await adapter.close();
- });
- it("refreshes list_changed in the background and keeps alwaysLoad", async () => {
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("a")] });
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", { ...stdio, alwaysLoad: true });
- const first = adapter.getToolSnapshot!().demo!;
- const refresh = Promise.withResolvers<{ tools: unknown[] }>();
- clientListToolsMock.mockImplementationOnce(() => refresh.promise);
- capturedClients.at(-1)!.notificationHandlers.get(LIST_CHANGED)!({});
- await vi.waitFor(() => expect(clientListToolsMock).toHaveBeenCalledTimes(2));
- // 刷新进行中:快照立即返回旧列表,不等待网络。
- const during = adapter.getToolSnapshot!().demo!;
- expect(during.toolsVersion).toBe(first.toolsVersion);
- expect(during.tools?.map((item) => item.toolName)).toEqual(["a"]);
- refresh.resolve({ tools: [tool("b")] });
- await vi.waitFor(() =>
- expect(adapter.getToolSnapshot!().demo!.tools?.map((item) => item.toolName)).toEqual(["b"]),
- );
- const after = adapter.getToolSnapshot!().demo!;
- expect(after.toolsVersion).not.toBe(first.toolsVersion);
- expect(after.tools?.[0]?.alwaysLoad).toBe(true);
- expect(after.status.toolCount).toBe(1);
- await adapter.close();
- });
- it("retries a failed list refresh in the producer while snapshot reads stay pure", async () => {
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("a")] });
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", stdio);
- const first = adapter.getToolSnapshot!().demo!;
- const handler = capturedClients.at(-1)!.notificationHandlers.get(LIST_CHANGED)!;
- const failed = Promise.withResolvers<{ tools: unknown[] }>();
- clientListToolsMock.mockImplementationOnce(() => failed.promise);
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("c")] });
- handler({});
- failed.reject(new Error("list failed"));
- await vi.waitFor(() => expect(clientListToolsMock).toHaveBeenCalledTimes(2));
- // 清空一轮宏任务,确保失败分支已执行完(脏标记已重新置位),而不是靠 sleep。
- await new Promise((resolve) => setImmediate(resolve));
- expect(adapter.getToolSnapshot!().demo!.toolsVersion).toBe(first.toolsVersion);
- // 失败后保留脏标记,但读快照只读内存,反复读取也不会发起 tools/list。
- for (let index = 0; index < 3; index += 1) adapter.getToolSnapshot!();
- expect(clientListToolsMock).toHaveBeenCalledTimes(2);
- // 无第二次通知,也无 listTools 调用;由连接层推进失败后的重试。
- await vi.waitFor(
- () =>
- expect(adapter.getToolSnapshot!().demo!.tools?.map((item) => item.toolName)).toEqual([
- "c",
- ]),
- { timeout: 2000 },
- );
- expect(clientListToolsMock).toHaveBeenCalledTimes(3);
- await adapter.close();
- });
- it("cancels a list refresh backoff when its connection closes", async () => {
- vi.useFakeTimers();
- const adapter = createMcpAdapter();
- try {
- await adapter.connectServer("demo", stdio);
- clientListToolsMock.mockRejectedValueOnce(new Error("temporary list failure"));
- capturedClients.at(-1)!.notificationHandlers.get(LIST_CHANGED)!({});
- await vi.advanceTimersByTimeAsync(0);
- expect(vi.getTimerCount()).toBeGreaterThan(0);
- await adapter.disconnectServer("demo");
- await vi.advanceTimersByTimeAsync(0);
- expect(vi.getTimerCount()).toBe(0);
- expect(clientListToolsMock).toHaveBeenCalledTimes(2);
- } finally {
- await adapter.close();
- vi.useRealTimers();
- }
- });
- it("ignores a late list_changed from a replaced client", async () => {
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("old")] });
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", stdio);
- const oldHandler = capturedClients.at(-1)!.notificationHandlers.get(LIST_CHANGED)!;
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("fresh")] });
- await adapter.connectServer("demo", { ...stdio, args: ["replacement"] });
- const replaced = adapter.getToolSnapshot!().demo!;
- const calls = clientListToolsMock.mock.calls.length;
- oldHandler({});
- await Promise.resolve();
- expect(clientListToolsMock).toHaveBeenCalledTimes(calls);
- expect(adapter.getToolSnapshot!().demo!).toEqual(replaced);
- await adapter.close();
- });
- it("runs one more pass when list_changed arrives during a refresh", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", stdio);
- const handler = capturedClients.at(-1)!.notificationHandlers.get(LIST_CHANGED)!;
- const firstRefresh = Promise.withResolvers<{ tools: unknown[] }>();
- clientListToolsMock.mockImplementationOnce(() => firstRefresh.promise);
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("second")] });
- handler({});
- await vi.waitFor(() => expect(clientListToolsMock).toHaveBeenCalledTimes(2));
- handler({});
- // single-flight:进行中的刷新不会并发第二次 tools/list。
- expect(clientListToolsMock).toHaveBeenCalledTimes(2);
- firstRefresh.resolve({ tools: [tool("first")] });
- await vi.waitFor(() =>
- expect(adapter.getToolSnapshot!().demo!.tools?.map((item) => item.toolName)).toEqual([
- "second",
- ]),
- );
- expect(clientListToolsMock).toHaveBeenCalledTimes(3);
- await adapter.close();
- });
- it("drops a late refresh result after the connection is replaced", async () => {
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("old")] });
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", stdio);
- const late = Promise.withResolvers<{ tools: unknown[] }>();
- clientListToolsMock.mockImplementationOnce(() => late.promise);
- capturedClients.at(-1)!.notificationHandlers.get(LIST_CHANGED)!({});
- await vi.waitFor(() => expect(clientListToolsMock).toHaveBeenCalledTimes(2));
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("fresh")] });
- await adapter.connectServer("demo", { ...stdio, args: ["replacement"] });
- const replaced = adapter.getToolSnapshot!().demo!;
- late.resolve({ tools: [tool("late")] });
- await Promise.resolve();
- await Promise.resolve();
- expect(adapter.getToolSnapshot!().demo!).toEqual(replaced);
- expect(replaced.tools?.map((item) => item.toolName)).toEqual(["fresh"]);
- await adapter.close();
- });
- it("removes the stdio directory after an unexpected disconnect", async () => {
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("a")] });
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", stdio);
- const first = adapter.getToolSnapshot!().demo!;
- capturedClients.at(-1)!.onclose?.();
- const after = adapter.getToolSnapshot!().demo!;
- expect(first.tools).toHaveLength(1);
- expect(after.status.status).toBe("failed");
- expect(after.toolsVersion).toBeUndefined();
- expect(after.tools).toBeUndefined();
- await adapter.close();
- });
- it("waits for the shared startup with a bound and cancel without closing the connection", async () => {
- const gate = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(() => gate.promise);
- const adapter = createMcpAdapter();
- const connecting = adapter.connectServer("demo", stdio);
- await vi.waitFor(() => expect(clientConnectMock).toHaveBeenCalledOnce());
- await expect(adapter.waitForServer!("demo", { timeoutMs: 1 })).resolves.toEqual({
- timedOut: true,
- });
- const abort = new AbortController();
- const aborted = adapter.waitForServer!("demo", { signal: abort.signal, timeoutMs: 60_000 });
- abort.abort(new Error("caller cancelled"));
- await expect(aborted).rejects.toThrow("caller cancelled");
- expect(adapter.getToolSnapshot!().demo!.status.status).toBe("connecting");
- const waiting = adapter.waitForServer!("demo", { timeoutMs: 60_000 });
- gate.resolve();
- await expect(waiting).resolves.toEqual({ timedOut: false });
- await connecting;
- expect(clientConnectMock).toHaveBeenCalledOnce();
- await expect(adapter.waitForServer!("demo", { timeoutMs: 1 })).resolves.toEqual({
- timedOut: false,
- });
- await expect(adapter.waitForServer!("missing", { timeoutMs: 1 })).resolves.toEqual({
- timedOut: false,
- });
- await adapter.close();
- });
- it("reconnects a disconnected server only when the execution check asks for it", async () => {
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("a")] });
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", stdio);
- capturedClients.at(-1)!.onclose?.();
- // WaitForMcpServers 不带 reconnect:断线 server 不自动重连。
- await expect(adapter.waitForServer!("demo", { timeoutMs: 60_000 })).resolves.toEqual({
- timedOut: false,
- });
- expect(clientConnectMock).toHaveBeenCalledOnce();
- expect(adapter.getToolSnapshot!().demo!.status.status).toBe("failed");
- clientListToolsMock.mockResolvedValueOnce({ tools: [tool("b")] });
- await expect(
- adapter.waitForServer!("demo", { timeoutMs: 60_000, reconnect: true }),
- ).resolves.toEqual({ timedOut: false });
- expect(clientConnectMock).toHaveBeenCalledTimes(2);
- const recovered = adapter.getToolSnapshot!().demo!;
- expect(recovered.status.status).toBe("connected");
- expect(recovered.tools?.map((item) => item.toolName)).toEqual(["b"]);
- await adapter.close();
- });
- it("keeps a waiter pending while a reconnect is still closing the previous connection", async () => {
- const adapter = createMcpAdapter();
- await adapter.connectServer("demo", stdio);
- const closing = Promise.withResolvers<void>();
- clientCloseMock.mockImplementationOnce(() => closing.promise);
- const reconnecting = adapter.connectServer("demo", { ...stdio, args: ["replacement"] });
- // 旧连接还在关闭、新握手尚未开始:新的 connecting 记录必须已登记,等待不能凭旧的 connected 记录立即成功。
- expect(adapter.getToolSnapshot!().demo!.status.status).toBe("connecting");
- await expect(adapter.waitForServer!("demo", { timeoutMs: 1 })).resolves.toEqual({
- timedOut: true,
- });
- const waiting = adapter.waitForServer!("demo", { timeoutMs: 60_000 });
- closing.resolve();
- await expect(waiting).resolves.toEqual({ timedOut: false });
- await expect(reconnecting).resolves.toMatchObject({ status: "connected" });
- await adapter.close();
- });
- it("returns a waiter as soon as startup turns into a pending OAuth authorization", async () => {
- const beforeAuthorization = Promise.withResolvers<void>();
- const { release } = stubInteractiveAuthorization(
- "https://auth.example.test/authorize?state=state_wait",
- beforeAuthorization.promise,
- );
- const adapter = createMcpAdapter();
- const connecting = adapter.connectServer("notion", {
- type: "http",
- url: "https://mcp.example.test/mcp",
- oauth: { type: "authorization_code", clientName: "ZCode" },
- });
- await vi.waitFor(() => expect(authMock).toHaveBeenCalled());
- expect(adapter.getToolSnapshot!().notion?.status.authorization).toBeUndefined();
- // 调用开始时只是普通 connecting;授权需要用户处理时立即结束本次等待,不等满预算。
- const waiting = adapter.waitForServer!("notion", { timeoutMs: 60_000 });
- beforeAuthorization.resolve();
- await expect(waiting).resolves.toEqual({ timedOut: false });
- expect(adapter.getToolSnapshot!().notion?.status.authorization?.authorizationUrl).toBe(
- "https://auth.example.test/authorize?state=state_wait",
- );
- // 已处于授权等待时再调用,也立即返回;底层授权不被取消。
- await expect(adapter.waitForServer!("notion", { timeoutMs: 60_000 })).resolves.toEqual({
- timedOut: false,
- });
- release();
- await expect(connecting).resolves.toMatchObject({ status: "connected" });
- await adapter.close();
- });
- });
- it("retries list refresh on the recovered connection and cancels it when that connection closes", async () => {
- const adapter = createMcpAdapter();
- const tool = (name: string) => ({ name, inputSchema: { type: "object" } });
- clientListToolsMock.mockResolvedValue({ tools: [tool("old")] } as never);
- await adapter.connectServer("remote", { type: "sse", url: "https://example.test/mcp" });
- capturedClients.at(-1)!.onclose?.();
- await adapter.waitForServer!("remote", { timeoutMs: 1000 });
- expect(adapter.getToolSnapshot!().remote?.status.status).toBe("connected");
- vi.useFakeTimers();
- try {
- clientListToolsMock
- .mockRejectedValueOnce(new Error("transient list failure"))
- .mockResolvedValueOnce({ tools: [tool("new")] } as never);
- capturedClients.at(-1)!.notificationHandlers.get("notifications/tools/list_changed")!({});
- await vi.advanceTimersByTimeAsync(1000);
- expect(adapter.getToolSnapshot!().remote?.tools?.map((t) => t.toolName)).toEqual(["new"]);
- clientListToolsMock.mockRejectedValue(new Error("another list failure"));
- capturedClients.at(-1)!.notificationHandlers.get("notifications/tools/list_changed")!({});
- await vi.advanceTimersByTimeAsync(0);
- const calls = clientListToolsMock.mock.calls.length;
- await adapter.close();
- await vi.runAllTimersAsync();
- expect(clientListToolsMock).toHaveBeenCalledTimes(calls);
- } finally {
- vi.useRealTimers();
- await adapter.close();
- }
- });
- });
- // 回归:远程意外断连在后台恢复,等待期间不丢目录。
- it("keeps remote tools pending until shared background recovery settles", async () => {
- const adapter = createMcpAdapter();
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "ping", inputSchema: { type: "object" } }],
- } as never);
- await adapter.connectServer("remote", { type: "http", url: "https://example.test/mcp" });
- const release = Promise.withResolvers<void>();
- clientConnectMock.mockImplementationOnce(() => release.promise);
- capturedClients.at(-1)!.onclose?.();
- expect(adapter.getToolSnapshot?.()?.remote?.status.status).toBe("connecting");
- expect(adapter.getToolSnapshot?.()?.remote?.tools?.[0]?.toolName).toBe("ping");
- const waiting = adapter.waitForServer!("remote", { timeoutMs: 1000 });
- release.resolve();
- await waiting;
- expect(adapter.getToolSnapshot?.()?.remote?.status.status).toBe("connected");
- await adapter.close();
- });
- it("exhausts five remote recovery attempts and drops the retained directory", async () => {
- const adapter = createMcpAdapter();
- clientListToolsMock.mockResolvedValue({
- tools: [{ name: "ping", inputSchema: { type: "object" } }],
- } as never);
- await adapter.connectServer("remote", { type: "http", url: "https://example.test/mcp" });
- const calls = clientConnectMock.mock.calls.length;
- vi.useFakeTimers();
- try {
- clientConnectMock.mockRejectedValue(new Error("offline"));
- capturedClients.at(-1)!.onclose?.();
- expect(adapter.getToolSnapshot!().remote?.status.status).toBe("connecting");
- await vi.runAllTimersAsync();
- expect(clientConnectMock.mock.calls.length - calls).toBe(5);
- expect(adapter.getToolSnapshot!().remote?.status.status).toBe("failed");
- expect(await adapter.listTools()).toEqual([]);
- } finally {
- vi.useRealTimers();
- await adapter.close();
- }
- });
|