@@ -5,7 +5,11 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
55import { MongoDBToolBase } from "../../../../src/tools/mongodb/mongodbTool.js" ;
66import { type ToolBase , type ToolConstructorParams , type OperationType } from "../../../../src/tools/tool.js" ;
77import { 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" ;
913import { Session } from "../../../../src/common/session.js" ;
1014import { CompositeLogger } from "../../../../src/common/logger.js" ;
1115import { DeviceId } from "../../../../src/helpers/deviceId.js" ;
@@ -14,13 +18,14 @@ import { InMemoryTransport } from "../../inMemoryTransport.js";
1418import { Telemetry } from "../../../../src/telemetry/telemetry.js" ;
1519import { Server } from "../../../../src/server.js" ;
1620import { type ConnectionErrorHandler , connectionErrorHandler } from "../../../../src/common/connectionErrorHandler.js" ;
17- import { defaultTestConfig } from "../../helpers.js" ;
21+ import { defaultTestConfig , expectDefined } from "../../helpers.js" ;
1822import { setupMongoDBIntegrationTest } from "./mongodbHelpers.js" ;
1923import { ErrorCodes } from "../../../../src/common/errors.js" ;
2024import { Keychain } from "../../../../src/common/keychain.js" ;
2125import { Elicitation } from "../../../../src/elicitation.js" ;
2226import { MongoDbTools } from "../../../../src/tools/mongodb/tools.js" ;
2327import { VectorSearchEmbeddingsManager } from "../../../../src/common/search/vectorSearchEmbeddingsManager.js" ;
28+ import { type TestConnectionManager } from "../../../utils/index.js" ;
2429
2530const 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