|
| 1 | +import { StreamableHttpRunner } from "../../../src/transports/streamableHttp.js"; |
| 2 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 3 | +import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; |
| 4 | +import { describe, expect, it, beforeAll, afterAll } from "vitest"; |
| 5 | +import type { UserConfig } from "../../../src/common/config.js"; |
| 6 | +import { TransportRunnerConfig } from "../../../src/lib.js"; |
| 7 | +import { defaultTestConfig } from "../helpers.js"; |
| 8 | + |
| 9 | +describe("createSessionConfig", () => { |
| 10 | + const userConfig = defaultTestConfig; |
| 11 | + let runner: StreamableHttpRunner; |
| 12 | + |
| 13 | + describe("basic functionality", () => { |
| 14 | + it("should use the modified config from createSessionConfig", async () => { |
| 15 | + const createSessionConfig: TransportRunnerConfig["createSessionConfig"] = async (userConfig) => { |
| 16 | + return { |
| 17 | + ...userConfig, |
| 18 | + apiBaseUrl: "https://test-api.mongodb.com/", |
| 19 | + }; |
| 20 | + }; |
| 21 | + |
| 22 | + userConfig.httpPort = 0; // Use a random port |
| 23 | + runner = new StreamableHttpRunner({ |
| 24 | + userConfig, |
| 25 | + createSessionConfig, |
| 26 | + }); |
| 27 | + await runner.start(); |
| 28 | + |
| 29 | + const server = await runner["setupServer"](); |
| 30 | + expect(server.userConfig.apiBaseUrl).toBe("https://test-api.mongodb.com/"); |
| 31 | + |
| 32 | + await runner.close(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should work without a createSessionConfig", async () => { |
| 36 | + userConfig.httpPort = 0; // Use a random port |
| 37 | + runner = new StreamableHttpRunner({ |
| 38 | + userConfig, |
| 39 | + }); |
| 40 | + await runner.start(); |
| 41 | + |
| 42 | + const server = await runner["setupServer"](); |
| 43 | + expect(server.userConfig.apiBaseUrl).toBe(userConfig.apiBaseUrl); |
| 44 | + |
| 45 | + await runner.close(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe("connection string modification", () => { |
| 50 | + it("should allow modifying connection string via createSessionConfig", async () => { |
| 51 | + const createSessionConfig: TransportRunnerConfig["createSessionConfig"] = async (userConfig) => { |
| 52 | + // Simulate fetching connection string from environment or secrets |
| 53 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 54 | + |
| 55 | + return { |
| 56 | + ...userConfig, |
| 57 | + connectionString: "mongodb://test-server:27017/test-db", |
| 58 | + }; |
| 59 | + }; |
| 60 | + |
| 61 | + userConfig.httpPort = 0; // Use a random port |
| 62 | + runner = new StreamableHttpRunner({ |
| 63 | + userConfig: { ...userConfig, connectionString: undefined }, |
| 64 | + createSessionConfig, |
| 65 | + }); |
| 66 | + await runner.start(); |
| 67 | + |
| 68 | + const server = await runner["setupServer"](); |
| 69 | + expect(server.userConfig.connectionString).toBe("mongodb://test-server:27017/test-db"); |
| 70 | + |
| 71 | + await runner.close(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe("server integration", () => { |
| 76 | + let client: Client; |
| 77 | + let transport: StreamableHTTPClientTransport; |
| 78 | + |
| 79 | + it("should successfully initialize server with createSessionConfig and serve requests", async () => { |
| 80 | + const createSessionConfig: TransportRunnerConfig["createSessionConfig"] = async (userConfig) => { |
| 81 | + // Simulate async config modification |
| 82 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 83 | + return { |
| 84 | + ...userConfig, |
| 85 | + readOnly: true, // Enable read-only mode |
| 86 | + }; |
| 87 | + }; |
| 88 | + |
| 89 | + userConfig.httpPort = 0; // Use a random port |
| 90 | + runner = new StreamableHttpRunner({ |
| 91 | + userConfig, |
| 92 | + createSessionConfig, |
| 93 | + }); |
| 94 | + await runner.start(); |
| 95 | + |
| 96 | + client = new Client({ |
| 97 | + name: "test-client", |
| 98 | + version: "1.0.0", |
| 99 | + }); |
| 100 | + transport = new StreamableHTTPClientTransport(new URL(`${runner.serverAddress}/mcp`)); |
| 101 | + |
| 102 | + await client.connect(transport); |
| 103 | + const response = await client.listTools(); |
| 104 | + |
| 105 | + expect(response).toBeDefined(); |
| 106 | + expect(response.tools).toBeDefined(); |
| 107 | + expect(response.tools.length).toBeGreaterThan(0); |
| 108 | + |
| 109 | + // Verify read-only mode is applied - insert-one should not be available |
| 110 | + const writeTools = response.tools.filter((tool) => tool.name === "insert-one"); |
| 111 | + expect(writeTools.length).toBe(0); |
| 112 | + |
| 113 | + // Verify read tools are available |
| 114 | + const readTools = response.tools.filter((tool) => tool.name === "find"); |
| 115 | + expect(readTools.length).toBe(1); |
| 116 | + |
| 117 | + await client.close(); |
| 118 | + await transport.close(); |
| 119 | + await runner.close(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe("error handling", () => { |
| 124 | + it("should propagate errors from configProvider on client connection", async () => { |
| 125 | + const createSessionConfig: TransportRunnerConfig["createSessionConfig"] = async () => { |
| 126 | + throw new Error("Failed to fetch config"); |
| 127 | + }; |
| 128 | + |
| 129 | + userConfig.httpPort = 0; // Use a random port |
| 130 | + runner = new StreamableHttpRunner({ |
| 131 | + userConfig, |
| 132 | + createSessionConfig, |
| 133 | + }); |
| 134 | + |
| 135 | + // Start succeeds because setupServer is only called when a client connects |
| 136 | + await runner.start(); |
| 137 | + |
| 138 | + // Error should occur when a client tries to connect |
| 139 | + const testClient = new Client({ |
| 140 | + name: "test-client", |
| 141 | + version: "1.0.0", |
| 142 | + }); |
| 143 | + const testTransport = new StreamableHTTPClientTransport(new URL(`${runner.serverAddress}/mcp`)); |
| 144 | + |
| 145 | + await expect(testClient.connect(testTransport)).rejects.toThrow(); |
| 146 | + |
| 147 | + await testClient.close(); |
| 148 | + await testTransport.close(); |
| 149 | + |
| 150 | + await runner.close(); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments