-
Notifications
You must be signed in to change notification settings - Fork 165
chore: add createSessionConfig hook MCP-294 #740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
tests/integration/transports/createSessionConfig.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import { StreamableHttpRunner } from "../../../src/transports/streamableHttp.js"; | ||
| import { Client } from "@modelcontextprotocol/sdk/client/index.js"; | ||
| import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import type { TransportRunnerConfig } from "../../../src/lib.js"; | ||
| import { defaultTestConfig } from "../helpers.js"; | ||
|
|
||
| describe("createSessionConfig", () => { | ||
| const userConfig = defaultTestConfig; | ||
| let runner: StreamableHttpRunner; | ||
|
|
||
| describe("basic functionality", () => { | ||
| it("should use the modified config from createSessionConfig", async () => { | ||
| const createSessionConfig: TransportRunnerConfig["createSessionConfig"] = async (userConfig) => { | ||
| return Promise.resolve({ | ||
| ...userConfig, | ||
| apiBaseUrl: "https://test-api.mongodb.com/", | ||
| }); | ||
| }; | ||
| userConfig.httpPort = 0; // Use a random port | ||
| runner = new StreamableHttpRunner({ | ||
| userConfig, | ||
| createSessionConfig, | ||
| }); | ||
| await runner.start(); | ||
|
|
||
| const server = await runner["setupServer"](); | ||
| expect(server.userConfig.apiBaseUrl).toBe("https://test-api.mongodb.com/"); | ||
|
|
||
| await runner.close(); | ||
| }); | ||
|
|
||
| it("should work without a createSessionConfig", async () => { | ||
| userConfig.httpPort = 0; // Use a random port | ||
| runner = new StreamableHttpRunner({ | ||
| userConfig, | ||
| }); | ||
| await runner.start(); | ||
|
|
||
| const server = await runner["setupServer"](); | ||
| expect(server.userConfig.apiBaseUrl).toBe(userConfig.apiBaseUrl); | ||
|
|
||
| await runner.close(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("connection string modification", () => { | ||
| it("should allow modifying connection string via createSessionConfig", async () => { | ||
| const createSessionConfig: TransportRunnerConfig["createSessionConfig"] = async (userConfig) => { | ||
| // Simulate fetching connection string from environment or secrets | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| return { | ||
| ...userConfig, | ||
| connectionString: "mongodb://test-server:27017/test-db", | ||
| }; | ||
| }; | ||
|
|
||
| userConfig.httpPort = 0; // Use a random port | ||
| runner = new StreamableHttpRunner({ | ||
| userConfig: { ...userConfig, connectionString: undefined }, | ||
| createSessionConfig, | ||
| }); | ||
| await runner.start(); | ||
|
|
||
| const server = await runner["setupServer"](); | ||
| expect(server.userConfig.connectionString).toBe("mongodb://test-server:27017/test-db"); | ||
|
|
||
| await runner.close(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("server integration", () => { | ||
| let client: Client; | ||
| let transport: StreamableHTTPClientTransport; | ||
|
|
||
| it("should successfully initialize server with createSessionConfig and serve requests", async () => { | ||
| const createSessionConfig: TransportRunnerConfig["createSessionConfig"] = async (userConfig) => { | ||
| // Simulate async config modification | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
| return { | ||
| ...userConfig, | ||
| readOnly: true, // Enable read-only mode | ||
| }; | ||
| }; | ||
|
|
||
| userConfig.httpPort = 0; // Use a random port | ||
| runner = new StreamableHttpRunner({ | ||
| userConfig, | ||
| createSessionConfig, | ||
| }); | ||
| await runner.start(); | ||
|
|
||
| client = new Client({ | ||
| name: "test-client", | ||
| version: "1.0.0", | ||
| }); | ||
| transport = new StreamableHTTPClientTransport(new URL(`${runner.serverAddress}/mcp`)); | ||
|
|
||
| await client.connect(transport); | ||
| const response = await client.listTools(); | ||
|
|
||
| expect(response).toBeDefined(); | ||
| expect(response.tools).toBeDefined(); | ||
| expect(response.tools.length).toBeGreaterThan(0); | ||
|
|
||
| // Verify read-only mode is applied - insert-one should not be available | ||
| const writeTools = response.tools.filter((tool) => tool.name === "insert-one"); | ||
| expect(writeTools.length).toBe(0); | ||
|
|
||
| // Verify read tools are available | ||
| const readTools = response.tools.filter((tool) => tool.name === "find"); | ||
| expect(readTools.length).toBe(1); | ||
|
|
||
| await client.close(); | ||
| await transport.close(); | ||
| await runner.close(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("error handling", () => { | ||
| it("should propagate errors from configProvider on client connection", async () => { | ||
| const createSessionConfig: TransportRunnerConfig["createSessionConfig"] = async () => { | ||
| return Promise.reject(new Error("Failed to fetch config")); | ||
| }; | ||
|
|
||
| userConfig.httpPort = 0; // Use a random port | ||
| runner = new StreamableHttpRunner({ | ||
| userConfig, | ||
| createSessionConfig, | ||
| }); | ||
|
|
||
| // Start succeeds because setupServer is only called when a client connects | ||
| await runner.start(); | ||
|
|
||
| // Error should occur when a client tries to connect | ||
| const testClient = new Client({ | ||
| name: "test-client", | ||
| version: "1.0.0", | ||
| }); | ||
| const testTransport = new StreamableHTTPClientTransport(new URL(`${runner.serverAddress}/mcp`)); | ||
|
|
||
| await expect(testClient.connect(testTransport)).rejects.toThrow(); | ||
|
|
||
| await testClient.close(); | ||
| await testTransport.close(); | ||
|
|
||
| await runner.close(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not too sure if I understood the usecase for this correctly so let me know if this should be used differently.