plan-mode-mcp-permission-server.mjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env node
  2. import { appendFile, mkdir } from "node:fs/promises";
  3. import { createRequire } from "node:module";
  4. import { dirname } from "node:path";
  5. const requireFromMcpAdapter = createRequire(
  6. new URL(
  7. "../../../../../../apps/zcode-cli/packages/adapters/package.json",
  8. import.meta.url,
  9. ),
  10. );
  11. const { McpServer } = requireFromMcpAdapter(
  12. "@modelcontextprotocol/sdk/server/mcp.js",
  13. );
  14. const { StdioServerTransport } = requireFromMcpAdapter(
  15. "@modelcontextprotocol/sdk/server/stdio.js",
  16. );
  17. const { z } = requireFromMcpAdapter("zod");
  18. const LOOKUP_RESULT_PREFIX = "E2E_PLAN_MCP_LOOKUP_RESULT";
  19. const RESET_RESULT = "E2E_PLAN_MCP_RESET_SHOULD_NOT_RUN";
  20. const callLogPath = process.env.E2E_PLAN_MCP_CALL_LOG;
  21. const server = new McpServer({
  22. name: "plan-mode-mcp-permission-e2e",
  23. version: "0.1.0",
  24. });
  25. server.registerTool(
  26. "lookup",
  27. {
  28. title: "Plan Mode MCP Lookup",
  29. description:
  30. "Return an E2E marker for Plan mode MCP permission coverage. This intentionally omits readOnlyHint.",
  31. inputSchema: {
  32. query: z.string().optional(),
  33. },
  34. annotations: {
  35. destructiveHint: false,
  36. idempotentHint: true,
  37. },
  38. },
  39. async ({ query }) => {
  40. await recordCall("lookup", { query });
  41. return {
  42. content: [
  43. {
  44. type: "text",
  45. text: `${LOOKUP_RESULT_PREFIX}:${query ?? ""}`,
  46. },
  47. ],
  48. };
  49. },
  50. );
  51. server.registerTool(
  52. "reset",
  53. {
  54. title: "Plan Mode MCP Reset",
  55. description:
  56. "Destructive E2E marker tool. Plan mode must reject this before the server handler runs.",
  57. inputSchema: {
  58. marker: z.string().optional(),
  59. },
  60. annotations: {
  61. destructiveHint: true,
  62. idempotentHint: false,
  63. },
  64. },
  65. async ({ marker }) => {
  66. await recordCall("reset", { marker });
  67. return {
  68. content: [
  69. {
  70. type: "text",
  71. text: `${RESET_RESULT}:${marker ?? ""}`,
  72. },
  73. ],
  74. };
  75. },
  76. );
  77. await server.connect(new StdioServerTransport());
  78. async function recordCall(tool, input) {
  79. if (!callLogPath) return;
  80. await mkdir(dirname(callLogPath), { recursive: true });
  81. await appendFile(
  82. callLogPath,
  83. `${JSON.stringify({ input, tool, ts: new Date().toISOString() })}\n`,
  84. "utf-8",
  85. );
  86. }