stdio-transport.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
  3. const terminateMcpStdioProcessTree = vi.hoisted(() => vi.fn(async () => undefined));
  4. vi.mock("../src/mcp/process-tree.js", () => ({
  5. terminateMcpStdioProcessTree,
  6. }));
  7. import { ProcessTreeStdioClientTransport } from "../src/mcp/stdio-transport.js";
  8. describe("ProcessTreeStdioClientTransport Windows fallback", () => {
  9. afterEach(() => {
  10. vi.restoreAllMocks();
  11. });
  12. it("uses the existing taskkill tree cleanup when Job Object attach fails", async () => {
  13. const originalPlatform = process.platform;
  14. Object.defineProperty(process, "platform", { configurable: true, value: "win32" });
  15. try {
  16. vi.spyOn(StdioClientTransport.prototype, "start").mockResolvedValue();
  17. vi.spyOn(StdioClientTransport.prototype, "pid", "get").mockReturnValue(4321);
  18. const windowsJobObjectFactory = vi.fn(async () => undefined);
  19. const transport = new ProcessTreeStdioClientTransport(
  20. { command: process.execPath },
  21. { windowsJobObjectFactory },
  22. );
  23. Object.assign(transport as unknown as { _process: object }, {
  24. _process: {
  25. exitCode: null,
  26. once: vi.fn(),
  27. pid: 4321,
  28. signalCode: null,
  29. },
  30. });
  31. await transport.start();
  32. delete (transport as unknown as { _process?: object })._process;
  33. const dispose = Object.getOwnPropertyDescriptor(
  34. ProcessTreeStdioClientTransport.prototype,
  35. "_dispose",
  36. )?.value as ((this: ProcessTreeStdioClientTransport) => Promise<void>) | undefined;
  37. expect(dispose).toBeTypeOf("function");
  38. await dispose?.call(transport);
  39. expect(windowsJobObjectFactory).toHaveBeenCalledWith(4321);
  40. expect(terminateMcpStdioProcessTree).toHaveBeenCalledWith(4321);
  41. } finally {
  42. Object.defineProperty(process, "platform", {
  43. configurable: true,
  44. value: originalPlatform,
  45. });
  46. }
  47. });
  48. });