plugin-mcp-skill.ts 4.7 KB

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