mcp.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. convertToZCodeAgentMcpServer,
  4. isZCodeCuaMcpCommand,
  5. isZCodeCuaMcpPackageArg,
  6. } from "../src/mcp.js";
  7. describe("zcode-cua MCP matcher (single source of truth for services + CLI bootstrap)", () => {
  8. it("matches the zcode-cua command by leaf name", () => {
  9. expect(isZCodeCuaMcpCommand("zcode-cua")).toBe(true);
  10. expect(isZCodeCuaMcpCommand("/opt/homebrew/bin/zcode-cua")).toBe(true);
  11. expect(isZCodeCuaMcpCommand("uvx")).toBe(false);
  12. expect(isZCodeCuaMcpCommand("zcode-cua-server")).toBe(false);
  13. });
  14. it("matches uvx/pip package spec forms", () => {
  15. for (const arg of ["zcode-cua", "zcode-cua[macos]", "zcode-cua@v0.1.3", "zcode-cua[macos]@v0.1.3"]) {
  16. expect(isZCodeCuaMcpPackageArg(arg)).toBe(true);
  17. }
  18. });
  19. it("matches git and local-path forms (the drift that broke desktop/services parity)", () => {
  20. expect(isZCodeCuaMcpPackageArg("zcode-cua.git")).toBe(true);
  21. expect(isZCodeCuaMcpPackageArg("git+https://github.com/x/zcode-cua.git@v0.1.3")).toBe(true);
  22. expect(isZCodeCuaMcpPackageArg("/Users/me/src/zcode-cua")).toBe(true);
  23. expect(isZCodeCuaMcpPackageArg("C:\\src\\zcode-cua")).toBe(true);
  24. });
  25. it("does not match unrelated args", () => {
  26. for (const arg of ["--from", "run", "zcode-cua-server", "not-zcode-cua", "server.js"]) {
  27. expect(isZCodeCuaMcpPackageArg(arg)).toBe(false);
  28. }
  29. });
  30. it("recognizes a package spec placed in COMMAND position (command/arg matchers stay consistent)", () => {
  31. // 之前 command matcher 只认精确叶子 zcode-cua,会漏判把包规格直接当 command 的配置 → 漏注入 broker。
  32. for (const command of ["zcode-cua@1.2.3", "zcode-cua[macos]", "zcode-cua.git", "/opt/tools/zcode-cua@1.2.3"]) {
  33. expect(isZCodeCuaMcpCommand(command), command).toBe(true);
  34. }
  35. });
  36. it("recognizes pip == pins and the zcode_cua underscore distribution name (PyPI treats _ == -)", () => {
  37. for (const arg of ["zcode-cua==1.2.3", "zcode_cua", "zcode_cua==1.2.3", "zcode_cua@v0.1.3"]) {
  38. expect(isZCodeCuaMcpPackageArg(arg), arg).toBe(true);
  39. }
  40. expect(isZCodeCuaMcpCommand("zcode_cua")).toBe(true);
  41. });
  42. it("does not false-positive on sibling packages like zcode-cua-proxy", () => {
  43. for (const value of ["zcode-cua-proxy", "zcode-cua-proxy@1.0.0", "zcode-cua-helper"]) {
  44. expect(isZCodeCuaMcpCommand(value), value).toBe(false);
  45. expect(isZCodeCuaMcpPackageArg(value), value).toBe(false);
  46. }
  47. });
  48. it("recognizes `python -m zcode_cua.server` submodule form (M1 fail-open fix)", () => {
  49. // `python -m zcode_cua.server` → arg 是 `zcode_cua.server`;旧 matcher 漏判 → 不注入 broker → fail-open。
  50. for (const arg of ["zcode_cua.server", "zcode-cua.server", "zcode_cua.main", "zcode-cua.__main__"]) {
  51. expect(isZCodeCuaMcpPackageArg(arg), arg).toBe(true);
  52. }
  53. });
  54. it("recognizes local paths with a trailing separator (M1 fail-open fix)", () => {
  55. // `.../zcode-cua/` 直接取叶子会得到空串 → 漏判 → fail-open。
  56. expect(isZCodeCuaMcpPackageArg("/Users/me/src/zcode-cua/")).toBe(true);
  57. expect(isZCodeCuaMcpPackageArg("C:\\src\\zcode-cua\\")).toBe(true);
  58. expect(isZCodeCuaMcpCommand("/opt/homebrew/bin/zcode-cua/")).toBe(true);
  59. // 但带尾斜杠的 sibling 仍不误判。
  60. expect(isZCodeCuaMcpPackageArg("/Users/me/src/zcode-cua-proxy/")).toBe(false);
  61. });
  62. });
  63. describe("MCP config helpers", () => {
  64. it("passes legacy http_headers through to ZCode Agent MCP servers", () => {
  65. expect(
  66. convertToZCodeAgentMcpServer("web-search-prime", {
  67. type: "http",
  68. url: "https://bigmodel.example.test/mcp",
  69. timeoutMs: 5000,
  70. http_headers: {
  71. Authorization: "Bearer token",
  72. },
  73. }),
  74. ).toEqual({
  75. name: "web-search-prime",
  76. type: "http",
  77. url: "https://bigmodel.example.test/mcp",
  78. headers: [{ name: "Authorization", value: "Bearer token" }],
  79. timeoutMs: 5000,
  80. });
  81. });
  82. it("preserves timeoutMs when converting stdio MCP servers for ZCode Agent", () => {
  83. expect(
  84. convertToZCodeAgentMcpServer("context7", {
  85. type: "stdio",
  86. command: "npx",
  87. args: ["-y", "@upstash/context7-mcp"],
  88. timeoutMs: 3,
  89. }),
  90. ).toEqual({
  91. name: "context7",
  92. command: "npx",
  93. args: ["-y", "@upstash/context7-mcp"],
  94. env: [],
  95. timeoutMs: 3,
  96. });
  97. });
  98. it("preserves timeoutMs when converting sse MCP servers for ZCode Agent", () => {
  99. expect(
  100. convertToZCodeAgentMcpServer("stream", {
  101. type: "sse",
  102. url: "https://bigmodel.example.test/sse",
  103. timeoutMs: 8000,
  104. }),
  105. ).toEqual({
  106. name: "stream",
  107. type: "sse",
  108. url: "https://bigmodel.example.test/sse",
  109. headers: [],
  110. timeoutMs: 8000,
  111. });
  112. });
  113. it("preserves HTTP OAuth client credentials when converting MCP servers for ZCode Agent", () => {
  114. expect(
  115. convertToZCodeAgentMcpServer("protected", {
  116. type: "http",
  117. url: "https://mcp.example.test/mcp",
  118. oauth: {
  119. type: "client_credentials",
  120. clientId: "zcode-client",
  121. clientSecret: "secret",
  122. scope: "mcp:tools",
  123. },
  124. }),
  125. ).toMatchObject({
  126. name: "protected",
  127. type: "http",
  128. oauth: {
  129. type: "client_credentials",
  130. clientId: "zcode-client",
  131. clientSecret: "secret",
  132. scope: "mcp:tools",
  133. },
  134. });
  135. });
  136. it("preserves HTTP OAuth authorization code config when converting MCP servers for ZCode Agent", () => {
  137. expect(
  138. convertToZCodeAgentMcpServer("figma", {
  139. type: "http",
  140. url: "https://mcp.figma.com/mcp",
  141. oauth: {
  142. type: "authorization_code",
  143. clientName: "ZCode",
  144. redirectPath: "/oauth/callback/mcp/figma",
  145. scope: "mcp:connect",
  146. },
  147. }),
  148. ).toMatchObject({
  149. name: "figma",
  150. type: "http",
  151. oauth: {
  152. type: "authorization_code",
  153. clientName: "ZCode",
  154. redirectPath: "/oauth/callback/mcp/figma",
  155. scope: "mcp:connect",
  156. },
  157. });
  158. });
  159. it("does not persist implicit OAuth defaults when converting bare HTTP MCP servers", () => {
  160. expect(
  161. convertToZCodeAgentMcpServer("notion", {
  162. type: "http",
  163. url: "https://mcp.notion.com/mcp",
  164. timeoutMs: 30000,
  165. }),
  166. ).toEqual({
  167. name: "notion",
  168. type: "http",
  169. url: "https://mcp.notion.com/mcp",
  170. headers: [],
  171. timeoutMs: 30000,
  172. });
  173. });
  174. it("drops invalid timeoutMs when converting MCP servers for ZCode Agent", () => {
  175. expect(
  176. convertToZCodeAgentMcpServer("zero", {
  177. type: "stdio",
  178. command: "npx",
  179. timeoutMs: 0,
  180. }),
  181. ).not.toHaveProperty("timeoutMs");
  182. expect(
  183. convertToZCodeAgentMcpServer("fractional", {
  184. type: "http",
  185. url: "https://bigmodel.example.test/mcp",
  186. timeoutMs: 1.5,
  187. }),
  188. ).not.toHaveProperty("timeoutMs");
  189. });
  190. it("keeps headers ahead of legacy http_headers when both are present", () => {
  191. expect(
  192. convertToZCodeAgentMcpServer("web-reader", {
  193. type: "sse",
  194. url: "https://bigmodel.example.test/sse",
  195. headers: {
  196. Authorization: "Bearer fresh",
  197. },
  198. http_headers: {
  199. Authorization: "Bearer stale",
  200. },
  201. }),
  202. ).toMatchObject({
  203. headers: [{ name: "Authorization", value: "Bearer fresh" }],
  204. });
  205. });
  206. });