|
| 1 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 2 | +import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; |
| 3 | +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; |
| 4 | +import { it, expect, vi } from "vitest"; |
| 5 | +import { startHTTPStreamServer } from "./startHTTPStreamServer.js"; |
| 6 | +import { getRandomPort } from "get-port-please"; |
| 7 | +import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; |
| 8 | +import { EventSource } from "eventsource"; |
| 9 | +import { setTimeout as delay } from "node:timers/promises"; |
| 10 | +import { proxyServer } from "./proxyServer.js"; |
| 11 | + |
| 12 | +if (!("EventSource" in global)) { |
| 13 | + // @ts-expect-error - figure out how to use --experimental-eventsource with vitest |
| 14 | + global.EventSource = EventSource; |
| 15 | +} |
| 16 | + |
| 17 | +it("proxies messages between HTTP stream and stdio servers", async () => { |
| 18 | + const stdioTransport = new StdioClientTransport({ |
| 19 | + command: "tsx", |
| 20 | + args: ["src/simple-stdio-server.ts"], |
| 21 | + }); |
| 22 | + |
| 23 | + const stdioClient = new Client( |
| 24 | + { |
| 25 | + name: "mcp-proxy", |
| 26 | + version: "1.0.0", |
| 27 | + }, |
| 28 | + { |
| 29 | + capabilities: {}, |
| 30 | + } |
| 31 | + ); |
| 32 | + |
| 33 | + await stdioClient.connect(stdioTransport); |
| 34 | + |
| 35 | + const serverVersion = stdioClient.getServerVersion() as { |
| 36 | + name: string; |
| 37 | + version: string; |
| 38 | + }; |
| 39 | + |
| 40 | + const serverCapabilities = stdioClient.getServerCapabilities() as {}; |
| 41 | + |
| 42 | + const port = await getRandomPort(); |
| 43 | + |
| 44 | + const onConnect = vi.fn(); |
| 45 | + const onClose = vi.fn(); |
| 46 | + |
| 47 | + await startHTTPStreamServer({ |
| 48 | + createServer: async () => { |
| 49 | + const mcpServer = new Server(serverVersion, { |
| 50 | + capabilities: serverCapabilities, |
| 51 | + }); |
| 52 | + |
| 53 | + await proxyServer({ |
| 54 | + server: mcpServer, |
| 55 | + client: stdioClient, |
| 56 | + serverCapabilities, |
| 57 | + }); |
| 58 | + |
| 59 | + return mcpServer; |
| 60 | + }, |
| 61 | + port, |
| 62 | + endpoint: "/stream", |
| 63 | + onConnect, |
| 64 | + onClose, |
| 65 | + }); |
| 66 | + |
| 67 | + const streamClient = new Client( |
| 68 | + { |
| 69 | + name: "stream-client", |
| 70 | + version: "1.0.0", |
| 71 | + }, |
| 72 | + { |
| 73 | + capabilities: {}, |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + const transport = new StreamableHTTPClientTransport( |
| 78 | + new URL(`http://localhost:${port}/stream`) |
| 79 | + ); |
| 80 | + |
| 81 | + await streamClient.connect(transport); |
| 82 | + |
| 83 | + const result = await streamClient.listResources(); |
| 84 | + expect(result).toEqual({ |
| 85 | + resources: [ |
| 86 | + { |
| 87 | + uri: "file:///example.txt", |
| 88 | + name: "Example Resource", |
| 89 | + }, |
| 90 | + ], |
| 91 | + }); |
| 92 | + |
| 93 | + expect( |
| 94 | + await streamClient.readResource({ uri: result.resources[0].uri }, {}) |
| 95 | + ).toEqual({ |
| 96 | + contents: [ |
| 97 | + { |
| 98 | + uri: "file:///example.txt", |
| 99 | + mimeType: "text/plain", |
| 100 | + text: "This is the content of the example resource.", |
| 101 | + }, |
| 102 | + ], |
| 103 | + }); |
| 104 | + expect(await streamClient.subscribeResource({ uri: "xyz" })).toEqual({}); |
| 105 | + expect(await streamClient.unsubscribeResource({ uri: "xyz" })).toEqual({}); |
| 106 | + expect(await streamClient.listResourceTemplates()).toEqual({ |
| 107 | + resourceTemplates: [ |
| 108 | + { |
| 109 | + uriTemplate: `file://{filename}`, |
| 110 | + name: "Example resource template", |
| 111 | + description: "Specify the filename to retrieve", |
| 112 | + }, |
| 113 | + ], |
| 114 | + }); |
| 115 | + |
| 116 | + expect(onConnect).toHaveBeenCalled(); |
| 117 | + expect(onClose).not.toHaveBeenCalled(); |
| 118 | + |
| 119 | + // the transport no requires the function terminateSession to be called but the client does not implement it |
| 120 | + // so we need to call it manually |
| 121 | + await transport.terminateSession(); |
| 122 | + await streamClient.close(); |
| 123 | + |
| 124 | + await delay(1000); |
| 125 | + |
| 126 | + expect(onClose).toHaveBeenCalled(); |
| 127 | +}); |
0 commit comments