startup-diagnostic-server.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { access } from "node:fs/promises";
  2. import { createInterface } from "node:readline";
  3. const [mode, recoveryMarker] = process.argv.slice(2);
  4. const recovered =
  5. recoveryMarker &&
  6. (await access(recoveryMarker).then(
  7. () => true,
  8. () => false,
  9. ));
  10. if (!recovered) {
  11. // 真实 stderr/退出路径;只模拟外部进程的失败内容,不写 App 状态。
  12. const causes = {
  13. python: "ModuleNotFoundError: No module named 'mcp'",
  14. node: "Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'example-mcp'",
  15. };
  16. process.stderr.write(
  17. `\u001b[31m${causes[mode]}\u001b[0m\nAPI_TOKEN=fixture-sensitive\nAUTH_TOKEN="fixture quoted secret"\n{"access_token":"fixture escaped\\" secret"}\nAuthorization: Bearer fixture-bearer\nhttps://user:fixture-pass@example.com/?api_key=fixture-key\n`,
  18. () => process.exit(1),
  19. );
  20. } else {
  21. createInterface({ input: process.stdin }).on("line", (line) => {
  22. const request = JSON.parse(line);
  23. if (request.id === undefined) return;
  24. const result =
  25. request.method === "initialize"
  26. ? {
  27. protocolVersion: request.params.protocolVersion,
  28. capabilities: { tools: {} },
  29. serverInfo: { name: "recovered-mcp", version: "1.0.0" },
  30. }
  31. : request.method === "tools/list"
  32. ? { tools: [] }
  33. : {};
  34. process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id: request.id, result })}\n`);
  35. });
  36. }