|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { validateJumpServerCredential } from "./runtime.ts"; |
| 3 | + |
| 4 | +afterEach(() => { |
| 5 | + vi.unstubAllGlobals(); |
| 6 | +}); |
| 7 | + |
| 8 | +describe("JumpServer MCP runtime", () => { |
| 9 | + it("uses Streamable HTTP with Worker-safe tool schema validation", async () => { |
| 10 | + const credential = { |
| 11 | + mcpEndpoint: "https://jumpserver.example.com/mcp", |
| 12 | + token: "jumpserver-token", |
| 13 | + }; |
| 14 | + |
| 15 | + // Warm the SDK's Node-only Zod fast path so this test isolates tool output validation. |
| 16 | + await validateJumpServerCredential(credential, createStreamableMcpFetch()); |
| 17 | + vi.stubGlobal("Function", function disabledFunctionConstructor() { |
| 18 | + throw new EvalError("Code generation from strings disallowed for this context"); |
| 19 | + }); |
| 20 | + |
| 21 | + const result = await validateJumpServerCredential(credential, createStreamableMcpFetch()); |
| 22 | + |
| 23 | + expect(result.metadata).toMatchObject({ |
| 24 | + mcpEndpoint: "https://jumpserver.example.com/mcp", |
| 25 | + availableActions: ["assets_assets_list"], |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + it("falls back to legacy SSE when Streamable HTTP is unavailable", async () => { |
| 30 | + const requests: Array<{ method: string; pathname: string }> = []; |
| 31 | + |
| 32 | + const result = await validateJumpServerCredential( |
| 33 | + { |
| 34 | + mcpEndpoint: "https://jumpserver.example.com/sse", |
| 35 | + token: "jumpserver-token", |
| 36 | + }, |
| 37 | + createLegacySseMcpFetch(requests), |
| 38 | + ); |
| 39 | + |
| 40 | + expect(result.metadata).toMatchObject({ |
| 41 | + mcpEndpoint: "https://jumpserver.example.com/sse", |
| 42 | + availableActions: ["assets_assets_list"], |
| 43 | + }); |
| 44 | + expect(requests).toContainEqual({ method: "POST", pathname: "/sse" }); |
| 45 | + expect(requests).toContainEqual({ method: "GET", pathname: "/sse" }); |
| 46 | + expect(requests).toContainEqual({ method: "POST", pathname: "/messages" }); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +function createStreamableMcpFetch(): typeof fetch { |
| 51 | + return vi.fn(async (_input: RequestInfo | URL, init?: RequestInit): Promise<Response> => { |
| 52 | + const method = init?.method ?? "GET"; |
| 53 | + if (method === "GET") { |
| 54 | + return new Response(null, { status: 405 }); |
| 55 | + } |
| 56 | + |
| 57 | + const request = readRequest(init); |
| 58 | + if (!("id" in request)) { |
| 59 | + return new Response(null, { status: 202 }); |
| 60 | + } |
| 61 | + |
| 62 | + const result = |
| 63 | + request.method === "initialize" |
| 64 | + ? { |
| 65 | + protocolVersion: "2025-03-26", |
| 66 | + capabilities: {}, |
| 67 | + serverInfo: { name: "jumpserver", version: "1.0.0" }, |
| 68 | + } |
| 69 | + : { |
| 70 | + tools: [ |
| 71 | + { |
| 72 | + name: "assets_assets_list", |
| 73 | + inputSchema: { type: "object" }, |
| 74 | + outputSchema: { type: "object" }, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }; |
| 78 | + |
| 79 | + return new Response(JSON.stringify({ jsonrpc: "2.0", id: request.id, result }), { |
| 80 | + headers: { |
| 81 | + "content-type": "application/json", |
| 82 | + "mcp-session-id": "test-session", |
| 83 | + }, |
| 84 | + }); |
| 85 | + }) as typeof fetch; |
| 86 | +} |
| 87 | + |
| 88 | +function createLegacySseMcpFetch(requests: Array<{ method: string; pathname: string }>): typeof fetch { |
| 89 | + const encoder = new TextEncoder(); |
| 90 | + let streamController: ReadableStreamDefaultController<Uint8Array> | undefined; |
| 91 | + |
| 92 | + return vi.fn(async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => { |
| 93 | + const url = new URL(input instanceof Request ? input.url : input.toString()); |
| 94 | + const method = init?.method ?? "GET"; |
| 95 | + requests.push({ method, pathname: url.pathname }); |
| 96 | + |
| 97 | + if (method === "POST" && url.pathname === "/sse") { |
| 98 | + return new Response(null, { status: 405 }); |
| 99 | + } |
| 100 | + if (method === "GET") { |
| 101 | + return new Response( |
| 102 | + new ReadableStream<Uint8Array>({ |
| 103 | + start(controller) { |
| 104 | + streamController = controller; |
| 105 | + controller.enqueue(encoder.encode("event: endpoint\ndata: /messages?session_id=test-session\n\n")); |
| 106 | + }, |
| 107 | + }), |
| 108 | + { headers: { "content-type": "text/event-stream" } }, |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + const request = readRequest(init); |
| 113 | + if ("id" in request) { |
| 114 | + const result = |
| 115 | + request.method === "initialize" |
| 116 | + ? { |
| 117 | + protocolVersion: "2024-11-05", |
| 118 | + capabilities: {}, |
| 119 | + serverInfo: { name: "jumpserver", version: "1.0.0" }, |
| 120 | + } |
| 121 | + : { |
| 122 | + tools: [ |
| 123 | + { |
| 124 | + name: "assets_assets_list", |
| 125 | + inputSchema: { type: "object" }, |
| 126 | + }, |
| 127 | + ], |
| 128 | + }; |
| 129 | + streamController!.enqueue( |
| 130 | + encoder.encode(`event: message\ndata: ${JSON.stringify({ jsonrpc: "2.0", id: request.id, result })}\n\n`), |
| 131 | + ); |
| 132 | + } |
| 133 | + return new Response(null, { status: 202 }); |
| 134 | + }) as typeof fetch; |
| 135 | +} |
| 136 | + |
| 137 | +function readRequest(init?: RequestInit): Record<string, unknown> { |
| 138 | + return typeof init?.body === "string" ? (JSON.parse(init.body) as Record<string, unknown>) : {}; |
| 139 | +} |
0 commit comments