Skip to content

Commit d2709e0

Browse files
committed
add mongodbtool test
1 parent 55cb171 commit d2709e0

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

tests/integration/tools/mongodb/mongodbTool.test.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
55
import { MongoDBToolBase } from "../../../../src/tools/mongodb/mongodbTool.js";
66
import { type ToolBase, type ToolConstructorParams, type OperationType } from "../../../../src/tools/tool.js";
77
import { defaultDriverOptions, type UserConfig } from "../../../../src/common/config.js";
8-
import { MCPConnectionManager } from "../../../../src/common/connectionManager.js";
8+
import {
9+
MCPConnectionManager,
10+
ConnectionStateConnected,
11+
type AtlasClusterConnectionInfo,
12+
} from "../../../../src/common/connectionManager.js";
913
import { Session } from "../../../../src/common/session.js";
1014
import { CompositeLogger } from "../../../../src/common/logger.js";
1115
import { DeviceId } from "../../../../src/helpers/deviceId.js";
@@ -14,13 +18,14 @@ import { InMemoryTransport } from "../../inMemoryTransport.js";
1418
import { Telemetry } from "../../../../src/telemetry/telemetry.js";
1519
import { Server } from "../../../../src/server.js";
1620
import { type ConnectionErrorHandler, connectionErrorHandler } from "../../../../src/common/connectionErrorHandler.js";
17-
import { defaultTestConfig } from "../../helpers.js";
21+
import { defaultTestConfig, expectDefined } from "../../helpers.js";
1822
import { setupMongoDBIntegrationTest } from "./mongodbHelpers.js";
1923
import { ErrorCodes } from "../../../../src/common/errors.js";
2024
import { Keychain } from "../../../../src/common/keychain.js";
2125
import { Elicitation } from "../../../../src/elicitation.js";
2226
import { MongoDbTools } from "../../../../src/tools/mongodb/tools.js";
2327
import { VectorSearchEmbeddingsManager } from "../../../../src/common/search/vectorSearchEmbeddingsManager.js";
28+
import { type TestConnectionManager } from "../../../utils/index.js";
2429

2530
const injectedErrorHandler: ConnectionErrorHandler = (error) => {
2631
switch (error.code) {
@@ -327,4 +332,42 @@ describe("MongoDBTool implementations", () => {
327332
expect(tools?.tools.find((tool) => tool.name === "UnusableVoyageTool")).toBeUndefined();
328333
});
329334
});
335+
336+
describe("resolveTelemetryMetadata", () => {
337+
it("should return empty metadata when not connected", async () => {
338+
await cleanupAndStartServer();
339+
const tool = mcpServer?.tools.find((t) => t.name === "Random");
340+
expectDefined(tool);
341+
const randomTool = tool as RandomTool;
342+
343+
const result: CallToolResult = { content: [{ type: "text", text: "test" }] };
344+
const metadata = randomTool["resolveTelemetryMetadata"](result, {} as never);
345+
346+
expect(metadata).toEqual({});
347+
expect(metadata).not.toHaveProperty("project_id");
348+
expect(metadata).not.toHaveProperty("connection_auth_type");
349+
});
350+
351+
it("should return metadata with connection_auth_type when connected via connection string", async () => {
352+
await cleanupAndStartServer({ connectionString: mdbIntegration.connectionString() });
353+
// Connect to MongoDB to set the connection state
354+
await mcpClient?.callTool({
355+
name: "Random",
356+
arguments: {},
357+
});
358+
359+
const tool = mcpServer?.tools.find((t) => t.name === "Random");
360+
expectDefined(tool);
361+
const randomTool = tool as RandomTool;
362+
363+
const result: CallToolResult = { content: [{ type: "text", text: "test" }] };
364+
const metadata = randomTool["resolveTelemetryMetadata"](result, {} as never);
365+
366+
// When connected via connection string, connection_auth_type should be set
367+
// The actual value depends on the connection string, but it should be present
368+
expect(metadata).toHaveProperty("connection_auth_type");
369+
expect(typeof metadata.connection_auth_type).toBe("string");
370+
expect(metadata.connection_auth_type).toBe("scram");
371+
});
372+
});
330373
});

0 commit comments

Comments
 (0)