server.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env node
  2. // E2E MCP App fixture(docs/plugins/extension-host.md §16.5):不依赖 MCP SDK 的 stdio server。
  3. // - 页面 ui://e2e-mcp-viewer/panel.html:显示最近一次 open_document 的结果(tool-result 的 structuredContent)。
  4. // - 工具 open_document({ path }):MCP App 查看器的约定,只给页面用(visibility app)。
  5. // 每次调用追加一行到插件目录旁的 e2e-mcp-viewer-calls.jsonl,E2E 读它确认收到的 path。
  6. import { appendFile } from "node:fs/promises";
  7. import { dirname, join } from "node:path";
  8. import readline from "node:readline";
  9. import { fileURLToPath } from "node:url";
  10. const root = dirname(fileURLToPath(import.meta.url));
  11. const callsFile = join(root, "..", "e2e-mcp-viewer-calls.jsonl");
  12. const PAGE_URI = "ui://e2e-mcp-viewer/panel.html";
  13. const PAGE = `<!doctype html>
  14. <html>
  15. <head>
  16. <meta charset="utf-8" />
  17. <style>
  18. :root { color-scheme: light dark; }
  19. body {
  20. margin: 0;
  21. padding: 16px;
  22. font: 13px/1.5 system-ui, sans-serif;
  23. color: var(--color-text-primary, #1f2328);
  24. background: var(--color-background-primary, transparent);
  25. }
  26. h1 { margin: 0 0 8px; font-size: 15px; }
  27. #opened { font-family: ui-monospace, monospace; color: var(--color-text-secondary, #59636e); }
  28. </style>
  29. </head>
  30. <body data-ready="0" data-opened-path="">
  31. <h1>E2E MCP panel</h1>
  32. <div id="opened">No document</div>
  33. <script>
  34. const host = window.zcode;
  35. const render = () => {
  36. const variables = host.hostContext?.styles?.variables ?? {};
  37. for (const [name, value] of Object.entries(variables)) {
  38. if (typeof value === "string") document.documentElement.style.setProperty(name, value);
  39. }
  40. const opened = host.toolOutput?.opened ?? "";
  41. document.body.dataset.openedPath = opened;
  42. document.getElementById("opened").textContent = opened ? "Opened " + opened : "No document";
  43. };
  44. host.subscribe(render);
  45. host.ready().then(() => {
  46. document.body.dataset.ready = "1";
  47. render();
  48. });
  49. </script>
  50. </body>
  51. </html>`;
  52. const input = readline.createInterface({ input: process.stdin });
  53. const send = (message) => process.stdout.write(JSON.stringify(message) + "\n");
  54. input.on("line", async (line) => {
  55. let request;
  56. try {
  57. request = JSON.parse(line);
  58. } catch {
  59. return;
  60. }
  61. const { id, method, params } = request;
  62. if (method === "initialize") {
  63. send({
  64. jsonrpc: "2.0",
  65. id,
  66. result: {
  67. protocolVersion: "2024-11-05",
  68. capabilities: { tools: {}, resources: {} },
  69. serverInfo: { name: "e2e-mcp-viewer", version: "1.0.0" },
  70. },
  71. });
  72. return;
  73. }
  74. if (method === "tools/list") {
  75. send({
  76. jsonrpc: "2.0",
  77. id,
  78. result: {
  79. tools: [
  80. {
  81. name: "open_document",
  82. description: "Open a workspace file in the E2E viewer page",
  83. inputSchema: {
  84. type: "object",
  85. properties: { path: { type: "string" } },
  86. required: ["path"],
  87. },
  88. annotations: { readOnlyHint: true },
  89. _meta: { ui: { visibility: ["app"] } },
  90. },
  91. ],
  92. },
  93. });
  94. return;
  95. }
  96. if (method === "tools/call" && params?.name === "open_document") {
  97. const path = String(params.arguments?.path ?? "");
  98. await appendFile(
  99. callsFile,
  100. JSON.stringify({ tool: "open_document", path, at: Date.now() }) + "\n",
  101. );
  102. send({
  103. jsonrpc: "2.0",
  104. id,
  105. result: {
  106. content: [{ type: "text", text: `E2E_OPEN_DOCUMENT ${path}` }],
  107. structuredContent: { opened: path },
  108. },
  109. });
  110. return;
  111. }
  112. if (method === "resources/list") {
  113. send({
  114. jsonrpc: "2.0",
  115. id,
  116. result: {
  117. resources: [
  118. { uri: PAGE_URI, name: "E2E MCP panel", mimeType: "text/html;profile=mcp-app" },
  119. ],
  120. },
  121. });
  122. return;
  123. }
  124. if (method === "resources/read") {
  125. send({
  126. jsonrpc: "2.0",
  127. id,
  128. result: {
  129. contents: [{ uri: params.uri, mimeType: "text/html;profile=mcp-app", text: PAGE }],
  130. },
  131. });
  132. return;
  133. }
  134. if (id !== undefined) send({ jsonrpc: "2.0", id, result: {} });
  135. });