mcp-network.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { chmod, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { dirname, delimiter, join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import {
  6. buildMcpStdioEnv,
  7. prependRunningNodeDirectory,
  8. } from "../src/mcp/network.js";
  9. describe("MCP stdio environment", () => {
  10. it("prepends the running Node directory so portable node commands work remotely", () => {
  11. const originalPath = ["/definitely/missing/bin", "/also/missing/bin"].join(delimiter);
  12. const env = buildMcpStdioEnv({
  13. env: {
  14. PATH: originalPath,
  15. },
  16. });
  17. expect(env.PATH).toBe([dirname(process.execPath), originalPath].join(delimiter));
  18. });
  19. it("preserves an existing PATH when another executable node is already available", async () => {
  20. const root = await mkdtemp(join(tmpdir(), "zcode-mcp-node-path-"));
  21. const nodePath = join(root, "node");
  22. try {
  23. await writeFile(nodePath, "#!/bin/sh\nexit 0\n");
  24. await chmod(nodePath, 0o755);
  25. const env = buildMcpStdioEnv({ env: { PATH: root } });
  26. expect(env.PATH).toBe(root);
  27. } finally {
  28. await rm(root, { force: true, recursive: true });
  29. }
  30. });
  31. it("normalizes Windows separators and case before adding the running node directory", () => {
  32. const env = prependRunningNodeDirectory(
  33. { Path: "C:\\ZCode\\Node;C:\\Windows\\System32" },
  34. {
  35. execPath: "c:/zcode/node/node.exe",
  36. isExecutable: () => false,
  37. platform: "win32",
  38. },
  39. );
  40. expect(env).toEqual({ Path: "C:\\ZCode\\Node;C:\\Windows\\System32" });
  41. });
  42. });