plugin-mcp-skill.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // 插件 MCP skill 生命周期 e2e helper。
  2. // 仅在 conversation-session/manual-review/pending/conversation-session-plugin-mcp-skill-*.test.ts
  3. // 这几个 spec 里被使用;wdio 启动前在 seedE2EStartupState 阶段会把对应的
  4. // plugin 安装记录落到 <E2E_HOME>/.zcode/cli/plugins/,并 patch cli config。
  5. import { readFile } from "node:fs/promises";
  6. import { dirname, join, resolve } from "node:path";
  7. import { fileURLToPath } from "node:url";
  8. // 修复原因:Windows 上 URL.pathname 是 `/C:/...`,resolve 后变成 `C:\C:\...`,
  9. // 与 wdio 写入工作区配置的真实插件路径不一致;必须用 fileURLToPath 转本机路径。
  10. const FIXTURES_ROOT = resolve(
  11. dirname(fileURLToPath(import.meta.url)),
  12. "..",
  13. "fixtures",
  14. "plugin-mcp-skill",
  15. );
  16. const PLUGIN_ROOT = join(FIXTURES_ROOT, "e2e-plugin-mcp-ping");
  17. export const PLUGIN_MCP_FIXTURE_DIR = FIXTURES_ROOT;
  18. export const PLUGIN_MCP_PLUGIN_ROOT = PLUGIN_ROOT;
  19. export const PLUGIN_MCP_PLUGIN_ID = "e2e-plugin-mcp-ping@e2e-fixture";
  20. // Bugfix: agent adapter 注入 MCP 工具时把 namespaced server 名(`plugin:<name>:<server>`)
  21. // 重写成符合模型 API 工具名约束的 `mcp__plugin_<name>_<server>__<tool>`,实际发给模型的就是这个名。
  22. // 见 `apps/zcode-cli/packages/adapters/src/mcp/descriptor.ts` 与
  23. // `apps/zcode-cli/packages/adapters/src/mcp/index.ts:normalizeMcpToolDescriptor`。
  24. export const PLUGIN_MCP_TOOL_NAME = "mcp__plugin_e2e-plugin-mcp-ping_ping__ping";
  25. export const PLUGIN_MCP_MARKER_PREFIX = "E2E_PLUGIN_MCP_PING";
  26. /**
  27. * 读取 upstream replay 捕获的请求记录,遍历 tools 数组,断言指定工具名是否注册。
  28. * 强证据:只有当 plugin MCP server 真正被 listTools 时,agent 才会把
  29. * `mcp__plugin_e2e-plugin-mcp-ping_ping__ping` 加进 `body.tools[]` 一并发送给模型。
  30. */
  31. export function captureArtifactAdvertisesToolName(requestJson: unknown, toolName: string): boolean {
  32. if (!requestJson || typeof requestJson !== "object") return false;
  33. const tools = (requestJson as { tools?: unknown }).tools;
  34. if (!Array.isArray(tools)) return false;
  35. return tools.some(
  36. (tool) =>
  37. tool !== null && typeof tool === "object" && (tool as { name?: unknown }).name === toolName,
  38. );
  39. }
  40. /**
  41. * 找到包含 marker 的最后一条 capture 请求,导出 requestJson 给调用方继续断言。
  42. * 比 `countUpstreamRequests` 更进一步:返回 record,方便读 tools 数组。
  43. */
  44. export async function findUpstreamRequestContaining(
  45. marker: string,
  46. timeoutMs = 30000,
  47. options?: { advertisedToolName?: string },
  48. ): Promise<{ method: string; path: string; requestJson: unknown } | null> {
  49. const capturePath = process.env.E2E_PROVIDER_CAPTURE_PATH?.trim();
  50. if (!capturePath) return null;
  51. const deadline = Date.now() + timeoutMs;
  52. let latest: { method: string; path: string; requestJson: unknown } | null = null;
  53. while (Date.now() < deadline) {
  54. const artifact = await readCaptureArtifact(capturePath);
  55. if (artifact) {
  56. for (const record of artifact.records) {
  57. if (
  58. containsText(record.requestJson, marker) &&
  59. (!options?.advertisedToolName ||
  60. captureArtifactAdvertisesToolName(record.requestJson, options.advertisedToolName))
  61. ) {
  62. latest = {
  63. method: record.method,
  64. path: record.path,
  65. requestJson: record.requestJson,
  66. };
  67. }
  68. }
  69. }
  70. // 修复原因:同一首发 prompt 会并发触发主请求与标题请求,两者都含 marker,
  71. // 但只有主请求携带 MCP tools。指定 advertisedToolName 时必须等到主请求落盘。
  72. if (latest) return latest;
  73. await sleep(250);
  74. }
  75. return latest;
  76. }
  77. async function readCaptureArtifact(capturePath: string): Promise<{
  78. records: Array<{ method: string; path: string; requestJson: unknown }>;
  79. } | null> {
  80. try {
  81. const raw = await readFile(capturePath, "utf-8");
  82. const parsed = JSON.parse(raw) as {
  83. records?: Array<{ method: string; path: string; requestJson?: unknown }>;
  84. };
  85. if (!parsed || !Array.isArray(parsed.records)) return null;
  86. return {
  87. records: parsed.records.map((record) => ({
  88. method: record.method,
  89. path: record.path,
  90. requestJson: record.requestJson ?? null,
  91. })),
  92. };
  93. } catch (error) {
  94. if (
  95. typeof error === "object" &&
  96. error !== null &&
  97. "code" in error &&
  98. (error as { code?: string }).code === "ENOENT"
  99. ) {
  100. return null;
  101. }
  102. throw error;
  103. }
  104. }
  105. function containsText(value: unknown, expected: string): boolean {
  106. if (typeof value === "string") return value.includes(expected);
  107. if (Array.isArray(value)) return value.some((item) => containsText(item, expected));
  108. if (!value || typeof value !== "object") return false;
  109. return Object.values(value).some((child) => containsText(child, expected));
  110. }
  111. function sleep(ms: number): Promise<void> {
  112. return new Promise((resolveSleep) => setTimeout(resolveSleep, ms));
  113. }