mcp-format.test.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. PROVIDER_BUILTIN_TOOL_SERVER_NAME,
  4. rewriteProviderBuiltinToolContent,
  5. } from "../../src/model/provider-builtin-tool-compat/contract.js";
  6. import { deltaTexts, run, serverToolUse, textBlock } from "./stream-test-helpers.js";
  7. // 新格式:服务端直接下发 mcp_tool_use / mcp_tool_result,结果摘要在结果之后。
  8. // 旧格式契约见 contract.test.ts;经 Runtime 的端到端见 ../provider-builtin-tool-rounds.test.ts。
  9. describe("provider-builtin-tool-compat upstream MCP format", () => {
  10. /** serverName 为 null 时不带 server_name(线上漏发的形态)。 */
  11. const mcpToolUseBlock = (
  12. id = "call_1",
  13. serverName: string | null = "4_5v_mcp",
  14. name = "analyze_image",
  15. ) => ({
  16. type: "mcp_tool_use",
  17. id,
  18. name,
  19. ...(serverName === null ? {} : { server_name: serverName }),
  20. input: {},
  21. });
  22. const mcpToolResultBlock = (id = "call_1", isError = false) => ({
  23. type: "mcp_tool_result",
  24. tool_use_id: id,
  25. content: [{ type: "text", text: "ok" }],
  26. is_error: isError,
  27. });
  28. const mcpToolUse = (
  29. index: number,
  30. id = "call_1",
  31. serverName: string | null = "4_5v_mcp",
  32. name = "analyze_image",
  33. ) => [
  34. { type: "content_block_start", index, content_block: mcpToolUseBlock(id, serverName, name) },
  35. { type: "content_block_stop", index },
  36. ];
  37. const mcpToolResult = (index: number, id = "call_1", isError = false) => [
  38. { type: "content_block_start", index, content_block: mcpToolResultBlock(id, isError) },
  39. { type: "content_block_stop", index },
  40. ];
  41. /** 服务端渲染的结果摘要:真实流与服务端文档示例都是这个形态。 */
  42. const summary = (toolName: string, body = "ok") =>
  43. `**Output:**\n**${toolName}_result_summary:** ${body}`;
  44. it("passes upstream MCP blocks through and drops their rendered copies", () => {
  45. const output = run([
  46. ...textBlock(0, "**🌐 Built-in Tool: analyze_image**"),
  47. ...mcpToolUse(1),
  48. ...mcpToolResult(2),
  49. ...textBlock(3, summary("analyze_image")),
  50. ...textBlock(4, "答案"),
  51. ]);
  52. expect(output).toEqual([...mcpToolUse(1), ...mcpToolResult(2), ...textBlock(4, "答案")]);
  53. });
  54. it("drops the rendered copies around parallel upstream MCP calls", () => {
  55. // 服务端文档示例:两个调用在前、两个结果在后,按 id 配对,展示文本夹在中间;摘要标签按各自的调用确认。
  56. const output = run([
  57. ...textBlock(0, "**🌐 Built-in Tool: analyze_image**"),
  58. ...mcpToolUse(1, "call_1"),
  59. ...textBlock(2, "**🌐 Built-in Tool: webReader**"),
  60. ...mcpToolUse(3, "call_2", "web_reader", "webReader"),
  61. ...mcpToolResult(4, "call_1"),
  62. ...textBlock(5, summary("analyze_image", "first")),
  63. ...mcpToolResult(6, "call_2", true),
  64. ...textBlock(7, summary("webReader", "MCP error 400")),
  65. ...textBlock(8, "答案"),
  66. ]);
  67. expect(output).toEqual([
  68. ...mcpToolUse(1, "call_1"),
  69. ...mcpToolUse(3, "call_2", "web_reader", "webReader"),
  70. ...mcpToolResult(4, "call_1"),
  71. ...mcpToolResult(6, "call_2", true),
  72. ...textBlock(8, "答案"),
  73. ]);
  74. });
  75. it("drops a summary whose prefix and label arrive split across deltas", () => {
  76. const output = run([
  77. ...mcpToolUse(0),
  78. ...mcpToolResult(1),
  79. { type: "content_block_start", index: 2, content_block: { type: "text", text: "" } },
  80. { type: "content_block_delta", index: 2, delta: { type: "text_delta", text: "**Out" } },
  81. { type: "ping" },
  82. {
  83. type: "content_block_delta",
  84. index: 2,
  85. delta: { type: "text_delta", text: "put:**\n**analyze_image_res" },
  86. },
  87. {
  88. type: "content_block_delta",
  89. index: 2,
  90. delta: { type: "text_delta", text: "ult_summary:** ok" },
  91. },
  92. { type: "content_block_stop", index: 2 },
  93. ]);
  94. expect(deltaTexts(output)).toEqual([]);
  95. });
  96. it("drops the next call header when a result arrives without its summary", () => {
  97. // 与旧格式对齐:结果后直接接下一个调用时,紧跟结果的调用头也是副本。
  98. const output = run([
  99. ...textBlock(0, "**🌐 Built-in Tool: analyze_image**"),
  100. ...mcpToolUse(1, "call_1"),
  101. ...mcpToolResult(2, "call_1"),
  102. ...textBlock(3, "**🌐 Built-in Tool: webReader**"),
  103. ...mcpToolUse(4, "call_2"),
  104. ...mcpToolResult(5, "call_2"),
  105. ...textBlock(6, "答案"),
  106. ]);
  107. expect(output).toEqual([
  108. ...mcpToolUse(1, "call_1"),
  109. ...mcpToolResult(2, "call_1"),
  110. ...mcpToolUse(4, "call_2"),
  111. ...mcpToolResult(5, "call_2"),
  112. ...textBlock(6, "答案"),
  113. ]);
  114. });
  115. it.each([
  116. [
  117. "a bold answer after the paired result",
  118. [...mcpToolUse(0), ...mcpToolResult(1)],
  119. "**结论:** 图片包含一辆红色汽车",
  120. ],
  121. ["summary-like text before any result", mcpToolUse(0), "**Output:** copy"],
  122. // 紧跟结果、像调用头的文字,后面没有调用就不是副本。
  123. [
  124. "header-like text after the result with no call after it",
  125. [...mcpToolUse(0), ...mcpToolResult(1)],
  126. "**🌐 正文",
  127. ],
  128. // 评审 CR-01:服务端没发摘要时,模型以 **Output:** 开头的回答紧跟结果,不能被整块删掉。
  129. [
  130. "a model answer labelled **Output:** after the paired result",
  131. [...mcpToolUse(0), ...mcpToolResult(1)],
  132. "**Output:** 正常最终回答",
  133. ],
  134. [
  135. "a model answer labelled **Output:** after an error result",
  136. [...mcpToolUse(0), ...mcpToolResult(1, "call_1", true)],
  137. "**Output:** 正常最终回答",
  138. ],
  139. [
  140. "a summary labelled for a different tool",
  141. [...mcpToolUse(0), ...mcpToolResult(1)],
  142. summary("webReader"),
  143. ],
  144. ])("keeps %s", (_label, before, answer) => {
  145. // 摘要后面没有结构块可确认:只有紧跟配对结果、带配对调用摘要标签的整块才算副本。
  146. const output = run([...before, ...textBlock(2, answer)]);
  147. expect(deltaTexts(output)).toEqual([answer]);
  148. });
  149. it("keeps a model answer labelled **Output:** that arrives split across deltas", () => {
  150. const deltas = ["**Out", "put:** 正常", "最终回答"];
  151. const output = run([
  152. ...mcpToolUse(0),
  153. ...mcpToolResult(1),
  154. { type: "content_block_start", index: 2, content_block: { type: "text", text: "" } },
  155. ...deltas.map((text) => ({
  156. type: "content_block_delta",
  157. index: 2,
  158. delta: { type: "text_delta", text },
  159. })),
  160. { type: "content_block_stop", index: 2 },
  161. ...textBlock(3, "补充"),
  162. ]);
  163. // 整块收完就放行,不等下一个块,顺序不变。
  164. expect(deltaTexts(output)).toEqual([...deltas, "补充"]);
  165. });
  166. it("keeps a summary the stream cut off before its block stopped", () => {
  167. // 断流时还没确认的缓冲帧由 finish 补发,不吞内容。
  168. const output = run([
  169. ...mcpToolUse(0),
  170. ...mcpToolResult(1),
  171. { type: "content_block_start", index: 2, content_block: { type: "text", text: "" } },
  172. {
  173. type: "content_block_delta",
  174. index: 2,
  175. delta: { type: "text_delta", text: "**Output:** partial" },
  176. },
  177. ]);
  178. expect(deltaTexts(output)).toEqual(["**Output:** partial"]);
  179. });
  180. it("fills in a missing server_name on an upstream MCP call", () => {
  181. // 2026-11-05 线上漏发 server_name:AI SDK schema 要求它是字符串,缺失时整轮报错。
  182. expect(run(mcpToolUse(0, "call_1", null))).toEqual(
  183. mcpToolUse(0, "call_1", PROVIDER_BUILTIN_TOOL_SERVER_NAME),
  184. );
  185. expect(rewriteProviderBuiltinToolContent([mcpToolUseBlock("call_1", null)])).toEqual({
  186. changed: true,
  187. content: [mcpToolUseBlock("call_1", PROVIDER_BUILTIN_TOOL_SERVER_NAME)],
  188. });
  189. });
  190. it("drops an MCP result that no call in the message paired", () => {
  191. // AI SDK 按 tool_use_id 直接取调用记录,取不到会抛 TypeError,整条流失败。
  192. const output = run([
  193. ...mcpToolUse(0),
  194. { type: "content_block_start", index: 1, content_block: mcpToolResultBlock("call_other") },
  195. { type: "content_block_delta", index: 1, delta: { type: "text_delta", text: "stray" } },
  196. { type: "content_block_stop", index: 1 },
  197. ...textBlock(2, "答案"),
  198. ]);
  199. expect(output).toEqual([...mcpToolUse(0), ...textBlock(2, "答案")]);
  200. expect(
  201. rewriteProviderBuiltinToolContent([
  202. mcpToolUseBlock(),
  203. mcpToolResultBlock("call_other"),
  204. { type: "text", text: "答案" },
  205. ]),
  206. ).toEqual({ changed: true, content: [mcpToolUseBlock(), { type: "text", text: "答案" }] });
  207. });
  208. it("pairs calls and results across the two formats within one message", () => {
  209. // 服务端半迁移时可能混出:旧格式调用配新格式结果,新格式调用配旧格式裸结果,都不能当成未配对丢掉。
  210. const output = run([
  211. ...serverToolUse(0),
  212. ...mcpToolResult(1, "call_1"),
  213. ...mcpToolUse(2, "call_2"),
  214. {
  215. type: "content_block_start",
  216. index: 3,
  217. content_block: { type: "tool_result", tool_use_id: "call_2", content: "ok" },
  218. },
  219. { type: "content_block_stop", index: 3 },
  220. ]);
  221. expect(
  222. output
  223. .filter((event) => event.type === "content_block_start")
  224. .map((event) => event.content_block),
  225. ).toEqual([
  226. expect.objectContaining({ type: "mcp_tool_use", id: "call_1" }),
  227. mcpToolResultBlock("call_1"),
  228. mcpToolUseBlock("call_2"),
  229. expect.objectContaining({ type: "mcp_tool_result", tool_use_id: "call_2" }),
  230. ]);
  231. });
  232. it("leaves upstream MCP blocks untouched in non-streaming content", () => {
  233. // 服务端文档:非流式响应没有展示文本,原生块原样透传。
  234. const content = [mcpToolUseBlock(), mcpToolResultBlock(), { type: "text", text: "答案" }];
  235. expect(rewriteProviderBuiltinToolContent(content)).toEqual({ changed: false, content });
  236. });
  237. });