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