@@ -9,6 +9,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
99import { MongoClient , ObjectId } from "mongodb" ;
1010import { toIncludeAllMembers } from "jest-extended" ;
1111import config from "../../src/config.js" ;
12+ import { McpError } from "@modelcontextprotocol/sdk/types.js" ;
1213
1314interface ParameterInfo {
1415 name : string ;
@@ -227,10 +228,93 @@ export const dbOperationParameters: ParameterInfo[] = [
227228 { name : "collection" , type : "string" , description : "Collection name" , required : true } ,
228229] ;
229230
230- export function validateParameters ( tool : ToolInfo , parameters : ParameterInfo [ ] ) : void {
231- const toolParameters = getParameters ( tool ) ;
232- expect ( toolParameters ) . toHaveLength ( parameters . length ) ;
233- expect ( toolParameters ) . toIncludeAllMembers ( parameters ) ;
231+ export const dbOperationInvalidArgTests = [ { } , { database : 123 } , { foo : "bar" , database : "test" } , { database : [ ] } ] ;
232+
233+ export function validateToolMetadata (
234+ integration : IntegrationTest ,
235+ name : string ,
236+ description : string ,
237+ parameters : ParameterInfo [ ]
238+ ) : void {
239+ it ( "should have correct metadata" , async ( ) => {
240+ const { tools } = await integration . mcpClient ( ) . listTools ( ) ;
241+ const tool = tools . find ( ( tool ) => tool . name === name ) ! ;
242+ expect ( tool ) . toBeDefined ( ) ;
243+ expect ( tool . description ) . toBe ( description ) ;
244+
245+ const toolParameters = getParameters ( tool ) ;
246+ expect ( toolParameters ) . toHaveLength ( parameters . length ) ;
247+ expect ( toolParameters ) . toIncludeAllMembers ( parameters ) ;
248+ } ) ;
249+ }
250+
251+ export function validateAutoConnectBehavior (
252+ integration : IntegrationTest ,
253+ name : string ,
254+ validation : ( ) => {
255+ args : { [ x : string ] : unknown } ;
256+ expectedResponse ?: string ;
257+ validate ?: ( content : unknown ) => void ;
258+ } ,
259+ beforeEachImpl ?: ( ) => Promise < void >
260+ ) : void {
261+ describe ( "when not connected" , ( ) => {
262+ if ( beforeEachImpl ) {
263+ beforeEach ( ( ) => beforeEachImpl ( ) ) ;
264+ }
265+
266+ it ( "connects automatically if connection string is configured" , async ( ) => {
267+ config . connectionString = integration . connectionString ( ) ;
268+
269+ const validationInfo = validation ( ) ;
270+
271+ const response = await integration . mcpClient ( ) . callTool ( {
272+ name,
273+ arguments : validationInfo . args ,
274+ } ) ;
275+
276+ if ( validationInfo . expectedResponse ) {
277+ const content = getResponseContent ( response . content ) ;
278+ expect ( content ) . toContain ( validationInfo . expectedResponse ) ;
279+ }
280+
281+ if ( validationInfo . validate ) {
282+ validationInfo . validate ( response . content ) ;
283+ }
284+ } ) ;
285+
286+ it ( "throws an error if connection string is not configured" , async ( ) => {
287+ const response = await integration . mcpClient ( ) . callTool ( {
288+ name,
289+ arguments : validation ( ) . args ,
290+ } ) ;
291+ const content = getResponseContent ( response . content ) ;
292+ expect ( content ) . toContain ( "You need to connect to a MongoDB instance before you can access its data." ) ;
293+ } ) ;
294+ } ) ;
295+ }
296+
297+ export function validateThrowsForInvalidArguments (
298+ integration : IntegrationTest ,
299+ name : string ,
300+ args : { [ x : string ] : unknown } [ ]
301+ ) : void {
302+ describe ( "with invalid arguments" , ( ) => {
303+ for ( const arg of args ) {
304+ it ( `throws a schema error for: ${ JSON . stringify ( arg ) } ` , async ( ) => {
305+ await integration . connectMcpClient ( ) ;
306+ try {
307+ await integration . mcpClient ( ) . callTool ( { name, arguments : arg } ) ;
308+ expect . fail ( "Expected an error to be thrown" ) ;
309+ } catch ( error ) {
310+ expect ( error ) . toBeInstanceOf ( McpError ) ;
311+ const mcpError = error as McpError ;
312+ expect ( mcpError . code ) . toEqual ( - 32602 ) ;
313+ expect ( mcpError . message ) . toContain ( `Invalid arguments for tool ${ name } ` ) ;
314+ }
315+ } ) ;
316+ }
317+ } ) ;
234318}
235319
236320export function describeAtlas ( name : number | string | Function | jest . FunctionLike , fn : jest . EmptyFunction ) {
0 commit comments