builtin-restart-limit.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { expect, it } from "vitest";
  2. // 内建 server 超过自动重启上限:adapter 保留的目录仍在视图里,下一次执行准入重连。
  3. import type { McpServerToolSnapshot } from "@zcode/contracts";
  4. import type { AgentRuntimeInternal } from "../../src/runtime/internal.js";
  5. import { createRuntime, mcpStatus, stdioServer, tool } from "../mcp-runtime-fixture.js";
  6. function readTools(snapshot: Record<string, McpServerToolSnapshot>): string[] {
  7. const { runtime } = createRuntime({
  8. label: "builtin-restart-limit",
  9. snapshot,
  10. servers: { node_repl: stdioServer() },
  11. });
  12. try {
  13. return (runtime as unknown as AgentRuntimeInternal)
  14. .mcpSession!.read()
  15. .tools.map((item) => item.toolName);
  16. } finally {
  17. runtime.beginShutdown();
  18. }
  19. }
  20. it("keeps the catalog the adapter retained after an unexpected disconnect", () => {
  21. // 修复前视图只列 connected/connecting 与 needs-auth 的工具:超过重启上限后工具离开视图,
  22. // 模型不再调用,执行准入的重连也就到不了。
  23. expect(
  24. readTools({
  25. node_repl: {
  26. status: mcpStatus("failed", { failureKind: "unexpected_disconnect" }),
  27. tools: [tool("node_repl", "js")],
  28. toolsVersion: "1",
  29. },
  30. }),
  31. ).toEqual(["js"]);
  32. });
  33. it("does not list stale tools of an explicitly disconnected server", () => {
  34. expect(
  35. readTools({
  36. node_repl: { status: mcpStatus("disconnected"), tools: [tool("node_repl", "js")] },
  37. }),
  38. ).toEqual([]);
  39. });