mcp-config-protocol-version.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { loadFileConfig } from "../src/config/file-config.adapter.js";
  6. import { parseConfigFileToRuntimePatchWithDiagnostics } from "../src/config/schema.js";
  7. describe("MCP config protocolVersion", () => {
  8. describe.each(["stdio", "http", "sse"] as const)("%s", (type) => {
  9. const transport =
  10. type === "stdio"
  11. ? { type, command: "mcp-test-server" }
  12. : { type, url: "https://example.test/mcp" };
  13. it.each([undefined, "auto", "legacy", "2026-07-28"])(
  14. "preserves protocolVersion=%s through file loading",
  15. async (protocolVersion) => {
  16. const root = await mkdtemp(join(tmpdir(), "zcode-mcp-protocol-config-"));
  17. const path = join(root, "config.json");
  18. const server = {
  19. ...transport,
  20. ...(protocolVersion === undefined ? {} : { protocolVersion }),
  21. };
  22. try {
  23. await writeFile(path, JSON.stringify({ mcp: { servers: { test: server } } }));
  24. const result = loadFileConfig(path);
  25. expect(result.loaded).toBe(true);
  26. expect(result.diagnostics).toEqual([]);
  27. expect(result.config.mcp?.servers).toEqual({ test: server });
  28. } finally {
  29. await rm(root, { recursive: true, force: true });
  30. }
  31. },
  32. );
  33. it.each([{ protocolVersion: "v2" }, { protocolVersion: null }, { unknownOption: true }])(
  34. "rejects invalid fields %j without discarding other config",
  35. (invalid) => {
  36. const valid = { ...transport, protocolVersion: "legacy" };
  37. const plugins = { enabledPlugins: { "example@marketplace": true } };
  38. const result = parseConfigFileToRuntimePatchWithDiagnostics({
  39. mcp: { servers: { valid, broken: { ...transport, ...invalid } } },
  40. plugins,
  41. });
  42. expect(result.config.mcp?.servers).toEqual({ valid });
  43. expect(result.config.plugins).toEqual(plugins);
  44. expect(result.diagnostics).toEqual([
  45. expect.objectContaining({
  46. code: "config_mcp_server_invalid",
  47. path: "mcp.servers.broken",
  48. severity: "warning",
  49. }),
  50. ]);
  51. },
  52. );
  53. });
  54. });