mcp-plain-http-fixture.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { Buffer } from "node:buffer";
  2. import type { IncomingMessage, ServerResponse } from "node:http";
  3. export interface PlainHttpMcpFixtureState {
  4. plainHttpInitializeCount: number;
  5. plainHttpToolCallMarkers: string[];
  6. }
  7. export async function handlePlainHttpMcpRequest(
  8. request: IncomingMessage,
  9. response: ServerResponse,
  10. state: PlainHttpMcpFixtureState,
  11. ): Promise<void> {
  12. if (request.method === "GET") {
  13. response.writeHead(405);
  14. response.end();
  15. return;
  16. }
  17. if (request.method !== "POST") {
  18. response.writeHead(404);
  19. response.end();
  20. return;
  21. }
  22. const message = JSON.parse(await readRequestBody(request)) as {
  23. id?: number | string;
  24. method?: string;
  25. params?: {
  26. arguments?: { marker?: unknown };
  27. name?: string;
  28. protocolVersion?: string;
  29. };
  30. };
  31. if (message.method === "initialize") {
  32. state.plainHttpInitializeCount += 1;
  33. sendJson(response, 200, {
  34. id: message.id,
  35. jsonrpc: "2.0",
  36. result: {
  37. capabilities: { tools: {} },
  38. protocolVersion: message.params?.protocolVersion ?? "2025-11-25",
  39. serverInfo: {
  40. name: "mcp-plain-http-status-isolation-e2e",
  41. version: "0.1.0",
  42. },
  43. },
  44. });
  45. return;
  46. }
  47. if (message.method === "tools/list") {
  48. sendJson(response, 200, {
  49. id: message.id,
  50. jsonrpc: "2.0",
  51. result: {
  52. tools: [
  53. {
  54. annotations: {
  55. destructiveHint: false,
  56. idempotentHint: true,
  57. readOnlyHint: true,
  58. },
  59. description: "Unauthenticated HTTP fixture ping",
  60. inputSchema: {
  61. properties: {
  62. marker: { type: "string" },
  63. },
  64. required: ["marker"],
  65. type: "object",
  66. },
  67. name: "ping",
  68. },
  69. ],
  70. },
  71. });
  72. return;
  73. }
  74. if (message.method === "tools/call" && message.params?.name === "ping") {
  75. const marker = message.params.arguments?.marker;
  76. if (typeof marker !== "string") {
  77. sendJson(response, 200, {
  78. error: {
  79. code: -32602,
  80. message: "plain HTTP ping requires a string marker",
  81. },
  82. id: message.id,
  83. jsonrpc: "2.0",
  84. });
  85. return;
  86. }
  87. state.plainHttpToolCallMarkers.push(marker);
  88. sendJson(response, 200, {
  89. id: message.id,
  90. jsonrpc: "2.0",
  91. result: {
  92. content: [{ text: `E2E_MCP_HTTP_PONG:${marker}`, type: "text" }],
  93. },
  94. });
  95. return;
  96. }
  97. if (message.id === undefined) {
  98. response.writeHead(202);
  99. response.end();
  100. return;
  101. }
  102. sendJson(response, 200, {
  103. error: {
  104. code: -32601,
  105. message: `Unsupported plain HTTP E2E MCP method: ${message.method ?? "unknown"}`,
  106. },
  107. id: message.id,
  108. jsonrpc: "2.0",
  109. });
  110. }
  111. async function readRequestBody(request: IncomingMessage): Promise<string> {
  112. const chunks: Buffer[] = [];
  113. for await (const chunk of request) {
  114. chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
  115. }
  116. return Buffer.concat(chunks).toString("utf8");
  117. }
  118. function sendJson(response: ServerResponse, statusCode: number, body: unknown): void {
  119. response.writeHead(statusCode, {
  120. "Content-Type": "application/json",
  121. });
  122. response.end(JSON.stringify(body));
  123. }