app-tool-authorization.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // MCP App 宿主授权(ToolExecutor.authorizeAppTool)的失败类型。
  2. // 规格:docs/design/v2/tool/00-tool-change-chain.md「MCP App 授权失败的错误类型」。
  3. // mcp/uiCallTool 与 plugins/mentionItems 共用这条授权链;调用方按 CoreError.type 区分「被拒绝」
  4. // 与其他失败,不读错误文本。
  5. import { describe, expect, it, vi } from "vitest";
  6. import { CoreErrorType, type McpPort, type PermissionBrokerResult } from "@zcode/contracts";
  7. import { createMcpToolEntry } from "../src/mcp/index.js";
  8. import { PermissionService, defaultPermissionConfig } from "../src/permission/service.js";
  9. import { ToolExecutorImpl } from "../src/tool/executor/impl.js";
  10. import { ToolRegistryImpl } from "../src/tool/registry.js";
  11. const descriptor = {
  12. name: "search",
  13. serverName: "plugin:gitlab:gitlab",
  14. toolName: "search",
  15. inputSchema: {
  16. type: "object",
  17. properties: { query: { type: "string" } },
  18. required: ["query"],
  19. additionalProperties: false,
  20. },
  21. };
  22. const toolEntry = () =>
  23. createMcpToolEntry("mcp__plugin_gitlab_gitlab__search", descriptor, {} as McpPort, false);
  24. function createExecutor(input: {
  25. disallowed?: string[];
  26. broker?: () => Promise<PermissionBrokerResult>;
  27. }) {
  28. return new ToolExecutorImpl({
  29. registry: new ToolRegistryImpl(),
  30. permissionService: new PermissionService({
  31. ...defaultPermissionConfig,
  32. disallowedTools: new Set(input.disallowed ?? []),
  33. }),
  34. permissionBroker: { requestPermission: input.broker ?? vi.fn() },
  35. sessionId: "fixture" as never,
  36. emitEvent: async () => {},
  37. });
  38. }
  39. describe("authorizeAppTool failure types", () => {
  40. it("returns the approved input when the user allows", async () => {
  41. const tool = toolEntry();
  42. const executor = createExecutor({ broker: async () => ({ decision: "allow" }) });
  43. await expect(
  44. executor.authorizeAppTool(
  45. { id: "call", name: tool.metadata.name, input: { query: "fix" } },
  46. tool,
  47. ),
  48. ).resolves.toEqual({ query: "fix" });
  49. });
  50. it("throws permission_denied when the user denies", async () => {
  51. const tool = toolEntry();
  52. const executor = createExecutor({ broker: async () => ({ decision: "deny" }) });
  53. await expect(
  54. executor.authorizeAppTool(
  55. { id: "call", name: tool.metadata.name, input: { query: "fix" } },
  56. tool,
  57. ),
  58. ).rejects.toMatchObject({ type: CoreErrorType.PermissionDenied });
  59. });
  60. it("throws permission_denied for a deny rule without asking", async () => {
  61. const tool = toolEntry();
  62. const broker = vi.fn();
  63. const executor = createExecutor({ disallowed: [tool.metadata.name], broker });
  64. await expect(
  65. executor.authorizeAppTool(
  66. { id: "call", name: tool.metadata.name, input: { query: "fix" } },
  67. tool,
  68. ),
  69. ).rejects.toMatchObject({ type: CoreErrorType.PermissionDenied });
  70. expect(broker).not.toHaveBeenCalled();
  71. });
  72. it("throws invalid_input, not a denial, when the input does not match the tool schema", async () => {
  73. const tool = toolEntry();
  74. const broker = vi.fn();
  75. const executor = createExecutor({ broker });
  76. await expect(
  77. executor.authorizeAppTool({ id: "call", name: tool.metadata.name, input: {} }, tool),
  78. ).rejects.toMatchObject({ type: CoreErrorType.InvalidInput });
  79. expect(broker).not.toHaveBeenCalled();
  80. });
  81. });