server.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env node
  2. // E2E MCP fixture(docs/plugins/extension-host.md §16.6):不依赖 MCP SDK 的 stdio server。
  3. // 工具 mention_search({ query }):只给宿主调(visibility app),composer.mention 的 tool target。
  4. // 返回 structuredContent.items(CLI 按 plugins/mentionItems 的严格 schema 校验)。
  5. // 启动与每次调用各追加一行到插件目录旁的 e2e-mcp-mention-calls.jsonl,E2E 读它确认收到的 query。
  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-mention-calls.jsonl");
  12. const input = readline.createInterface({ input: process.stdin });
  13. const send = (message) => process.stdout.write(JSON.stringify(message) + "\n");
  14. input.on("line", async (line) => {
  15. let request;
  16. try {
  17. request = JSON.parse(line);
  18. } catch {
  19. return;
  20. }
  21. const { id, method, params } = request;
  22. if (method === "initialize") {
  23. send({
  24. jsonrpc: "2.0",
  25. id,
  26. result: {
  27. protocolVersion: "2024-11-05",
  28. capabilities: { tools: {} },
  29. serverInfo: { name: "e2e-mcp-mention", version: "1.0.0" },
  30. },
  31. });
  32. return;
  33. }
  34. if (method === "tools/list") {
  35. await appendFile(callsFile, JSON.stringify({ event: "listed", at: Date.now() }) + "\n");
  36. send({
  37. jsonrpc: "2.0",
  38. id,
  39. result: {
  40. tools: [
  41. {
  42. name: "mention_search",
  43. description: "Search E2E documents for the composer @ panel",
  44. inputSchema: {
  45. type: "object",
  46. properties: { query: { type: "string" } },
  47. required: ["query"],
  48. },
  49. annotations: { readOnlyHint: true },
  50. _meta: { ui: { visibility: ["app"] } },
  51. },
  52. ],
  53. },
  54. });
  55. return;
  56. }
  57. if (method === "tools/call" && params?.name === "mention_search") {
  58. const query = String(params.arguments?.query ?? "");
  59. await appendFile(
  60. callsFile,
  61. JSON.stringify({ event: "call", tool: "mention_search", query, at: Date.now() }) + "\n",
  62. );
  63. const tag = query || "all";
  64. send({
  65. jsonrpc: "2.0",
  66. id,
  67. result: {
  68. content: [{ type: "text", text: `E2E_MCP_MENTION ${tag}` }],
  69. structuredContent: {
  70. items: [1, 2].map((n) => ({
  71. id: `${tag}-${n}`,
  72. label: `E2E doc ${tag} #${n}`,
  73. description: `MCP candidate ${n}`,
  74. icon: "database",
  75. insertText: `E2E_MCP_INSERT[${tag}#${n}]`,
  76. })),
  77. },
  78. },
  79. });
  80. return;
  81. }
  82. if (id !== undefined) send({ jsonrpc: "2.0", id, result: {} });
  83. });
  84. // 先登记 line 监听再写启动记录:readline 创建后就开始读,监听前到达的 initialize 会丢掉,
  85. // 连接要等 30 秒超时才失败(第一次运行就是这样)。
  86. void appendFile(callsFile, JSON.stringify({ event: "started", at: Date.now() }) + "\n");