officialMcpAuthProtocol.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* OMCP-011 相关 —— 官方 MCP 身份头协议 schema(spec §7.2)。
  2. 请求侧不得携带任何秘密;响应侧失败原因必须可枚举且无自由文本。 */
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. zcodeOfficialMcpAuthHeadersRequestParamsSchema,
  6. zcodeOfficialMcpAuthHeadersResponseSchema,
  7. zcodeProtocolMethods,
  8. } from "../src/zcode-protocol/index.js";
  9. const VALID_REQUEST = {
  10. mcpKey: "image-search",
  11. pluginId: "zcode-tools@zcode-plugins-official",
  12. requestId: "mcp-auth-1",
  13. targetOrigin: "https://mcp.zcode.example",
  14. workspace: { workspaceKey: "local:/repo", workspacePath: "/repo" },
  15. };
  16. describe("official mcp auth headers protocol", () => {
  17. it("registers the reverse request under the interaction namespace", () => {
  18. expect(zcodeProtocolMethods.interactionRequestOfficialMcpAuthHeaders).toBe(
  19. "interaction/requestOfficialMcpAuthHeaders",
  20. );
  21. });
  22. it("accepts a minimal request and a remote workspace identity", () => {
  23. expect(zcodeOfficialMcpAuthHeadersRequestParamsSchema.parse(VALID_REQUEST)).toMatchObject({
  24. mcpKey: "image-search",
  25. targetOrigin: "https://mcp.zcode.example",
  26. });
  27. expect(
  28. zcodeOfficialMcpAuthHeadersRequestParamsSchema.safeParse({
  29. ...VALID_REQUEST,
  30. workspace: {
  31. workspaceIdentity: "ssh:host/repo",
  32. workspaceKey: "ssh:host/repo",
  33. workspacePath: "/repo",
  34. },
  35. }).success,
  36. ).toBe(true);
  37. });
  38. it("is strict, so a smuggled credential field is rejected", () => {
  39. for (const extra of [
  40. { jwt: "leaked" },
  41. { headers: { Authorization: "Bearer leaked" } },
  42. { codingPlanApiKey: "leaked" },
  43. ]) {
  44. expect(
  45. zcodeOfficialMcpAuthHeadersRequestParamsSchema.safeParse({ ...VALID_REQUEST, ...extra })
  46. .success,
  47. ).toBe(false);
  48. }
  49. });
  50. it("requires every identifying field to be a non-empty string", () => {
  51. for (const field of ["mcpKey", "pluginId", "requestId", "targetOrigin"]) {
  52. expect(
  53. zcodeOfficialMcpAuthHeadersRequestParamsSchema.safeParse({
  54. ...VALID_REQUEST,
  55. [field]: "",
  56. }).success,
  57. ).toBe(false);
  58. }
  59. });
  60. it("accepts both response branches", () => {
  61. expect(
  62. zcodeOfficialMcpAuthHeadersResponseSchema.parse({
  63. headers: { Authorization: "Bearer jwt", "Bigmodel-Target-Type": "TEAM" },
  64. ok: true,
  65. }),
  66. ).toMatchObject({ ok: true });
  67. for (const reason of ["official_auth_unavailable", "official_auth_plan_required"]) {
  68. expect(
  69. zcodeOfficialMcpAuthHeadersResponseSchema.parse({ ok: false, reason }),
  70. ).toMatchObject({ ok: false, reason });
  71. }
  72. });
  73. it("rejects unknown reasons, free-text errors and cross-branch fields", () => {
  74. const invalid = [
  75. { ok: false, reason: "start_plan_not_supported" },
  76. { ok: false, reason: "official_auth_rejected" },
  77. { errorMessage: "something went wrong", ok: false, reason: "official_auth_unavailable" },
  78. { headers: {}, ok: false, reason: "official_auth_unavailable" },
  79. { ok: true, reason: "official_auth_unavailable" },
  80. { ok: true },
  81. ];
  82. for (const value of invalid) {
  83. expect(zcodeOfficialMcpAuthHeadersResponseSchema.safeParse(value).success).toBe(false);
  84. }
  85. });
  86. });