server-class.test.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { expect, it, vi } from "vitest";
  2. import type { McpServerToolSnapshot } from "@zcode/contracts";
  3. import { classifyServer } from "../../src/mcp/runtime/session.js";
  4. import type { AgentRuntimeInternal } from "../../src/runtime/internal.js";
  5. import { createRuntime, mcpStatus, stdioServer, tool } from "../mcp-runtime-fixture.js";
  6. it.each([
  7. ["config disabled", { enabled: false, servers: { local: {} } }, "connected", "disabled"],
  8. ["server disabled", { servers: { local: { enabled: false } } }, "connected", "disabled"],
  9. ["status disabled", { servers: { local: {} } }, "disabled", "disabled"],
  10. ["removed from config", { servers: {} }, "connected", "not_configured"],
  11. ["no record", { servers: { local: {} } }, undefined, "absent"],
  12. ["connecting", { servers: { local: {} } }, "connecting", "pending"],
  13. ["connected", { servers: { local: {} } }, "connected", "connected"],
  14. ["failed", { servers: { local: {} } }, "failed", "failed"],
  15. ["no server map", {}, "connected", "connected"],
  16. ] as const)("classifies a server: %s", (_label, config, status, expected) => {
  17. expect(
  18. classifyServer(
  19. config as Parameters<typeof classifyServer>[0],
  20. "local",
  21. status ? mcpStatus(status) : undefined,
  22. ),
  23. ).toBe(expected);
  24. });
  25. it.each([
  26. ["authorization prompt", { authorization: { type: "oauth_authorization_code" } }],
  27. ["authorization failure", { failureKind: "oauth_authorization_failed" }],
  28. ["origin denied", { failureKind: "official_origin_untrusted" }],
  29. ] as const)("classifies a failed server with %s", (label, extra) => {
  30. expect(
  31. classifyServer(
  32. { servers: { local: {} } } as Parameters<typeof classifyServer>[0],
  33. "local",
  34. mcpStatus("failed", extra as Parameters<typeof mcpStatus>[1]),
  35. ),
  36. ).toBe(label === "origin denied" ? "failed" : "needs_auth");
  37. });
  38. it("a child waits only for its scoped servers that are still connecting", async () => {
  39. const waitForServer = vi.fn(async () => ({ timedOut: false }));
  40. const snapshot: Record<string, McpServerToolSnapshot> = {
  41. fast: { status: mcpStatus("connected"), tools: [tool("fast", "ping")], toolsVersion: "1" },
  42. slow: { status: mcpStatus("connecting") },
  43. };
  44. const { runtime } = createRuntime({
  45. label: "child-wait",
  46. snapshot,
  47. servers: { fast: stdioServer(), slow: stdioServer() },
  48. mcpPort: { waitForServer },
  49. });
  50. try {
  51. void (runtime as unknown as AgentRuntimeInternal).mcpSession!.start();
  52. // 启动始终未完成:修复前 child 等整个父启动,与自己无关的慢 server 会卡住 child 创建。
  53. const unscoped = await runtime.createChildMcpAccess();
  54. expect(unscoped.snapshot?.tools.map((item) => item.toolName)).toEqual(["ping"]);
  55. expect(waitForServer).not.toHaveBeenCalled();
  56. await runtime.createChildMcpAccess(["fast", "slow"]);
  57. expect(waitForServer).toHaveBeenCalledExactlyOnceWith("slow", { timeoutMs: 30_000 });
  58. waitForServer.mockClear();
  59. // 等待集合可比借用范围大:tools 引用的 server 也要等,借用范围仍只认 scoped。
  60. const widened = await runtime.createChildMcpAccess(["fast"], undefined, {
  61. waitFor: ["fast", "slow"],
  62. });
  63. expect(waitForServer).toHaveBeenCalledExactlyOnceWith("slow", { timeoutMs: 30_000 });
  64. expect(Object.keys(widened.snapshot?.statuses ?? {})).toEqual(["fast"]);
  65. } finally {
  66. runtime.beginShutdown();
  67. }
  68. });