mcp-server.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // 独立 MCP 进程:控制 socket 只用于可重复地安排完成顺序,不替代 MCP 数据链路。
  2. import { createInterface } from "node:readline";
  3. import { connect } from "node:net";
  4. import { readFile } from "node:fs/promises";
  5. const [port, htmlPath, source] = process.argv.slice(2);
  6. const control = connect(Number(port), "127.0.0.1");
  7. const sendEvent = (event) => control.write(JSON.stringify({ source, ...event }) + "\n");
  8. const respond = (id, result) =>
  9. process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id, result }) + "\n");
  10. const pending = new Map();
  11. const result = (key) => ({
  12. ...(key === "raw-error" ? { isError: true } : {}),
  13. content:
  14. key === "raw-error"
  15. ? [
  16. { type: "text", text: "fixture error" },
  17. { type: "image", data: "aGVsbG8=", mimeType: "image/png" },
  18. {
  19. type: "resource",
  20. resource: { uri: "fixture://error", mimeType: "text/plain", text: "details" },
  21. },
  22. ]
  23. : [{ type: "text", text: "x".repeat(40_000) }],
  24. structuredContent: { key, source },
  25. _meta: { fixture: true },
  26. });
  27. createInterface({ input: control }).on("line", (line) => {
  28. const command = JSON.parse(line);
  29. if (command.type === "disconnect") process.exit(0);
  30. if (command.type === "notify") {
  31. process.stdout.write(
  32. JSON.stringify({
  33. jsonrpc: "2.0",
  34. method: "notifications/resources/updated",
  35. params: { uri: command.uri },
  36. }) + "\n",
  37. );
  38. sendEvent({ type: "notified", uri: command.uri });
  39. }
  40. if (command.type === "release") {
  41. const call = pending.get(command.key);
  42. if (call) {
  43. pending.delete(command.key);
  44. respond(call.id, call.result);
  45. sendEvent({ type: "released", key: command.key });
  46. }
  47. }
  48. });
  49. createInterface({ input: process.stdin }).on("line", async (line) => {
  50. const message = JSON.parse(line);
  51. if (message.method === "notifications/cancelled") {
  52. const call = [...pending.entries()].find(([, value]) => value.id === message.params.requestId);
  53. if (call) sendEvent({ type: "cancelled", key: call[0] });
  54. // 故意允许控制端晚回结果,验证 Agent 不接受取消后的写入,也不会重放。
  55. return;
  56. }
  57. if (message.id === undefined) return;
  58. const { id, method, params } = message;
  59. switch (method) {
  60. case "initialize":
  61. respond(id, {
  62. protocolVersion: "2025-11-25",
  63. capabilities: { tools: {}, resources: { subscribe: true } },
  64. serverInfo: { name: "host-e2e", version: "1" },
  65. });
  66. break;
  67. case "tools/list":
  68. respond(id, {
  69. tools: [
  70. {
  71. name: "edit",
  72. description: "Local E2E app-only tool",
  73. inputSchema: {
  74. type: "object",
  75. properties: { key: { type: "string" }, delayed: { type: "boolean" } },
  76. required: ["key"],
  77. },
  78. annotations: { readOnlyHint: false },
  79. _meta: { ui: { visibility: ["app"] } },
  80. },
  81. ],
  82. });
  83. break;
  84. case "resources/read": {
  85. sendEvent({ type: "read", uri: params.uri });
  86. const resources = {
  87. "fixture://document.pdf": {
  88. mimeType: "application/pdf",
  89. blob: Buffer.from("%PDF-1.7\n%%EOF\n").toString("base64"),
  90. },
  91. "fixture://vector.svg": {
  92. mimeType: "image/svg+xml",
  93. text: '<svg xmlns="http://www.w3.org/2000/svg"/>',
  94. },
  95. "fixture://unsupported.bin": { mimeType: "application/x-msdownload", blob: "YQ==" },
  96. };
  97. if (resources[params.uri]) {
  98. respond(id, {
  99. contents: [
  100. { uri: params.uri, ...resources[params.uri], _meta: { private: "server-only" } },
  101. ],
  102. });
  103. break;
  104. }
  105. respond(id, {
  106. contents: [
  107. {
  108. uri: params.uri,
  109. mimeType: "text/html;profile=mcp-app",
  110. text: await readFile(htmlPath, "utf8"),
  111. _meta: {
  112. ui: source === "managed-permissions" ? { permissions: { geolocation: {} } } : {},
  113. },
  114. },
  115. ],
  116. });
  117. break;
  118. }
  119. case "resources/list":
  120. respond(id, { resources: [] });
  121. break;
  122. case "resources/templates/list":
  123. respond(id, { resourceTemplates: [] });
  124. break;
  125. case "resources/subscribe":
  126. case "resources/unsubscribe":
  127. sendEvent({ type: method, uri: params.uri });
  128. respond(id, {});
  129. break;
  130. case "tools/call": {
  131. const key = params.arguments.key;
  132. sendEvent({ type: "executed", key });
  133. if (params.arguments.delayed) pending.set(key, { id, result: result(key) });
  134. else respond(id, result(key));
  135. break;
  136. }
  137. default:
  138. respond(id, {});
  139. }
  140. });
  141. process.stdin.on("end", () => {
  142. control.end();
  143. process.exit(0);
  144. });