dual-era-version-server.mjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 requireFromBrowserUse = createRequire(
  6. new URL(
  7. "../../../../../../apps/zcode-cli/packages/browser-use-plugin/package.json",
  8. import.meta.url,
  9. ),
  10. );
  11. const { McpServer } = requireFromBrowserUse("@modelcontextprotocol/server");
  12. const { serveStdio, StdioServerTransport } = requireFromBrowserUse(
  13. "@modelcontextprotocol/server/stdio",
  14. );
  15. const mode = process.env.E2E_MCP_VERSION_MODE;
  16. const wireLogPath = process.env.E2E_MCP_VERSION_WIRE_LOG;
  17. if ((mode !== "modern" && mode !== "legacy") || !wireLogPath) {
  18. throw new Error(
  19. "E2E MCP version fixture requires a valid mode and wire log path",
  20. );
  21. }
  22. let writeQueue = Promise.resolve();
  23. function recordEvent(event) {
  24. writeQueue = writeQueue
  25. .then(async () => {
  26. await mkdir(dirname(wireLogPath), { recursive: true });
  27. await appendFile(
  28. wireLogPath,
  29. `${JSON.stringify({ ...event, mode, pid: process.pid, ts: new Date().toISOString() })}\n`,
  30. "utf8",
  31. );
  32. })
  33. .catch(() => undefined);
  34. return writeQueue;
  35. }
  36. await recordEvent({ event: "started" });
  37. let inputBuffer = "";
  38. process.stdin.on("data", (chunk) => {
  39. inputBuffer += chunk.toString("utf8");
  40. let newlineIndex = inputBuffer.indexOf("\n");
  41. while (newlineIndex >= 0) {
  42. const line = inputBuffer.slice(0, newlineIndex).trim();
  43. inputBuffer = inputBuffer.slice(newlineIndex + 1);
  44. if (line) {
  45. try {
  46. const message = JSON.parse(line);
  47. if (typeof message?.method === "string") {
  48. void recordEvent({ event: "request", method: message.method });
  49. }
  50. } catch {
  51. void recordEvent({ event: "invalid-request-line" });
  52. }
  53. }
  54. newlineIndex = inputBuffer.indexOf("\n");
  55. }
  56. });
  57. function createServer() {
  58. const server = new McpServer({
  59. name: `desktop-e2e-${mode}`,
  60. version: "1.0.0",
  61. });
  62. server.registerTool(
  63. "ping",
  64. {
  65. description: "Return a real MCP E2E pong marker",
  66. inputSchema: {},
  67. },
  68. async () => ({ content: [{ text: `pong-${mode}`, type: "text" }] }),
  69. );
  70. return server;
  71. }
  72. if (mode === "modern") {
  73. serveStdio(createServer, { legacy: "reject" });
  74. } else {
  75. await createServer().connect(new StdioServerTransport());
  76. }