mcp-structured-content-projection.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // @vitest-environment node
  2. /**
  3. * MCP 结果投影:空 structuredContent 不再追加 "Structured content:" 文本块。
  4. *
  5. * 背景(2026-08-19 产品反馈):CUA 等成功结果携带 structuredContent: undefined
  6. * 的空键时,通用 bridge 仍追加 "Structured content:\n"(stringify(undefined)
  7. * 为空串),模型每次都看到一段无信息量的尾巴。
  8. *
  9. * 同时锁住「有值必须投影」:上游 CUA toolError 的结构化 details(action_sent /
  10. * 恢复提示)只有 structuredContent 一个通道。Renderer 不解析这段文本触发副作用,
  11. * 但模型仍需要 delivery evidence 避免盲试或重放动作。
  12. */
  13. import { describe, expect, it } from "vitest";
  14. import { registerMcpTools } from "../src/mcp/index.js";
  15. import { createToolRegistry } from "../src/tool/registry.js";
  16. import type { McpPort } from "@zcode/shared";
  17. describe("formatMcpToolResult structuredContent projection", () => {
  18. // 最小 mock port(与 mcp-tool-bridge.test.ts 的 createMockMcpPort 同形);
  19. // 本用例组只消费 registerMcpTools 的 descriptor→entry 注册,port 不被调用。
  20. const createMockMcpPort = (): McpPort =>
  21. ({
  22. connectConfiguredServers: async () => ({ statuses: {}, tools: [] }),
  23. connectServer: async () => ({
  24. status: "connected",
  25. transport: "stdio",
  26. toolCount: 1,
  27. updatedAt: "now",
  28. }),
  29. disconnectServer: async () => undefined,
  30. status: async () => ({}),
  31. listTools: async () => [],
  32. callTool: async () => ({ content: [] }),
  33. close: async () => {},
  34. }) as McpPort;
  35. function structuredToolEntry() {
  36. const registry = createToolRegistry();
  37. registerMcpTools(registry, createMockMcpPort(), [
  38. {
  39. serverName: "modern",
  40. toolName: "structured",
  41. inputSchema: { type: "object" },
  42. },
  43. ]);
  44. return registry.get("mcp__modern__structured");
  45. }
  46. it("undefined 值不投影(CUA 成功结果空尾巴修复)", () => {
  47. const entry = structuredToolEntry();
  48. expect(
  49. entry?.formatModelContent?.({
  50. content: [{ type: "text", text: '{"pid":18067}' }],
  51. structuredContent: undefined,
  52. }),
  53. ).toBe('{"pid":18067}');
  54. });
  55. it("null / 空对象 / 空数组值同样不投影", () => {
  56. const entry = structuredToolEntry();
  57. for (const empty of [null, {}, []]) {
  58. expect(
  59. entry?.formatModelContent?.({
  60. content: [{ type: "text", text: "ok" }],
  61. structuredContent: empty,
  62. }),
  63. ).toBe("ok");
  64. }
  65. });
  66. it("有值的 structuredContent 仍完整投影(错误 delivery details 通道)", () => {
  67. const entry = structuredToolEntry();
  68. expect(
  69. entry?.formatModelContent?.({
  70. content: [{ type: "text", text: "blocked" }],
  71. isError: true,
  72. structuredContent: { action_sent: true, request_delivery_state: "possibly_sent" },
  73. }),
  74. ).toContain(
  75. 'Structured content:\n{\n "action_sent": true,\n "request_delivery_state": "possibly_sent"\n}',
  76. );
  77. });
  78. });