diff --git a/.vscode/launch.json b/.vscode/launch.json index 8eec7d6e5..658926601 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,13 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Attach by Process ID", + "processId": "${command:PickProcess}", + "request": "attach", + "skipFiles": ["/**"], + "type": "node" + }, { "type": "node", "request": "launch", diff --git a/README.md b/README.md index fbf5660f8..1541166d4 100644 --- a/README.md +++ b/README.md @@ -669,7 +669,6 @@ npx -y mongodb-mcp-server@latest --logPath=/path/to/logs --readOnly --indexCheck "args": [ "-y", "mongodb-mcp-server", - "--connectionString", "mongodb+srv://username:password@cluster.mongodb.net/myDatabase", "--readOnly" ] diff --git a/package.json b/package.json index 6fef5e0c4..24f88877f 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "dist" ], "scripts": { - "start": "node dist/index.js --transport http --loggers stderr mcp", + "start": "node dist/index.js --transport http --loggers stderr mcp --previewFeatures vectorSearch", "start:stdio": "node dist/index.js --transport stdio --loggers stderr mcp", "prepare": "husky && pnpm run build", "build:clean": "rm -rf dist", diff --git a/src/tools/args.ts b/src/tools/args.ts index 11b5b8b80..8ad418d31 100644 --- a/src/tools/args.ts +++ b/src/tools/args.ts @@ -18,6 +18,7 @@ export const ALLOWED_CLUSTER_NAME_CHARACTERS_ERROR = const ALLOWED_PROJECT_NAME_CHARACTERS_REGEX = /^[a-zA-Z0-9\s()@&+:._',-]+$/; export const ALLOWED_PROJECT_NAME_CHARACTERS_ERROR = "Project names can't be longer than 64 characters and can only contain letters, numbers, spaces, and the following symbols: ( ) @ & + : . _ - ' ,"; + export const CommonArgs = { string: (): ZodString => z.string().regex(NO_UNICODE_REGEX, NO_UNICODE_ERROR), diff --git a/src/tools/mongodb/create/createIndex.ts b/src/tools/mongodb/create/createIndex.ts index ef5536439..ecf06e484 100644 --- a/src/tools/mongodb/create/createIndex.ts +++ b/src/tools/mongodb/create/createIndex.ts @@ -7,62 +7,129 @@ import { quantizationEnum } from "../../../common/search/vectorSearchEmbeddingsM import { similarityValues } from "../../../common/schemas.js"; export class CreateIndexTool extends MongoDBToolBase { - private vectorSearchIndexDefinition = z.object({ - type: z.literal("vectorSearch"), - fields: z - .array( - z.discriminatedUnion("type", [ - z - .object({ - type: z.literal("filter"), - path: z - .string() - .describe( - "Name of the field to index. For nested fields, use dot notation to specify path to embedded fields" - ), - }) - .strict() - .describe("Definition for a field that will be used for pre-filtering results."), - z - .object({ - type: z.literal("vector"), - path: z - .string() - .describe( - "Name of the field to index. For nested fields, use dot notation to specify path to embedded fields" - ), - numDimensions: z - .number() - .min(1) - .max(8192) - .default(this.config.vectorSearchDimensions) - .describe( - "Number of vector dimensions that MongoDB Vector Search enforces at index-time and query-time" - ), - similarity: z - .enum(similarityValues) - .default(this.config.vectorSearchSimilarityFunction) - .describe( - "Vector similarity function to use to search for top K-nearest neighbors. You can set this field only for vector-type fields." - ), - quantization: quantizationEnum - .default("none") + private vectorSearchIndexDefinition = z + .object({ + type: z.literal("vectorSearch"), + fields: z + .array( + z.discriminatedUnion("type", [ + z + .object({ + type: z.literal("filter"), + path: z + .string() + .describe( + "Name of the field to index. For nested fields, use dot notation to specify path to embedded fields" + ), + }) + .strict() + .describe("Definition for a field that will be used for pre-filtering results."), + z + .object({ + type: z.literal("vector"), + path: z + .string() + .describe( + "Name of the field to index. For nested fields, use dot notation to specify path to embedded fields" + ), + numDimensions: z + .number() + .min(1) + .max(8192) + .default(this.config.vectorSearchDimensions) + .describe( + "Number of vector dimensions that MongoDB Vector Search enforces at index-time and query-time" + ), + similarity: z + .enum(similarityValues) + .default(this.config.vectorSearchSimilarityFunction) + .describe( + "Vector similarity function to use to search for top K-nearest neighbors. You can set this field only for vector-type fields." + ), + quantization: quantizationEnum + .default("none") + .describe( + "Type of automatic vector quantization for your vectors. Use this setting only if your embeddings are float or double vectors." + ), + }) + .strict() + .describe("Definition for a field that contains vector embeddings."), + ]) + ) + .nonempty() + .refine((fields) => fields.some((f) => f.type === "vector"), { + message: "At least one vector field must be defined", + }) + .describe( + "Definitions for the vector and filter fields to index, one definition per document. You must specify `vector` for fields that contain vector embeddings and `filter` for additional fields to filter on. At least one vector-type field definition is required." + ), + }) + .describe("Definition for a Vector Search index."); + + private atlasSearchIndexDefinition = z + .object({ + type: z.literal("search"), + analyzer: z + .string() + .optional() + .default("lucene.standard") + .describe( + "The analyzer to use for the index. Can be one of the built-in lucene analyzers (`lucene.standard`, `lucene.simple`, `lucene.whitespace`, `lucene.keyword`), a language-specific analyzer, such as `lucene.cjk` or `lucene.czech`, or a custom analyzer defined in the Atlas UI." + ), + mappings: z + .object({ + dynamic: z + .boolean() + .optional() + .default(false) + .describe( + "Enables or disables dynamic mapping of fields for this index. If set to true, Atlas Search recursively indexes all dynamically indexable fields. If set to false, you must specify individual fields to index using mappings.fields." + ), + fields: z + .record( + z.string().describe("The field name"), + z + .object({ + type: z + .enum([ + "autocomplete", + "boolean", + "date", + "document", + "embeddedDocuments", + "geo", + "number", + "objectId", + "string", + "token", + "uuid", + ]) + .describe("The field type"), + }) + .passthrough() .describe( - "Type of automatic vector quantization for your vectors. Use this setting only if your embeddings are float or double vectors." - ), - }) - .strict() - .describe("Definition for a field that contains vector embeddings."), - ]) - ) - .nonempty() - .refine((fields) => fields.some((f) => f.type === "vector"), { - message: "At least one vector field must be defined", - }) - .describe( - "Definitions for the vector and filter fields to index, one definition per document. You must specify `vector` for fields that contain vector embeddings and `filter` for additional fields to filter on. At least one vector-type field definition is required." - ), - }); + "The field index definition. It must contain the field type, as well as any additional options for that field type." + ) + ) + .optional() + .describe("The field mapping definitions. If `dynamic` is set to `false`, this is required."), + }) + .refine((data) => data.dynamic !== !!(data.fields && Object.keys(data.fields).length > 0), { + message: + "Either `dynamic` must be `true` and `fields` empty or `dynamic` must be `false` and at least one field must be defined in `fields`", + }) + .describe( + "Document describing the index to create. Either `dynamic` must be `true` and `fields` empty or `dynamic` must be `false` and at least one field must be defined in the `fields` document." + ), + numPartitions: z + .union([z.literal("1"), z.literal("2"), z.literal("4")]) + .default("1") + .transform((value): number => Number.parseInt(value)) + .describe( + "Specifies the number of sub-indexes to create if the document count exceeds two billion. If omitted, defaults to 1." + ), + }) + .describe("Definition for an Atlas Search (lexical) index."); public name = "create-index"; protected description = "Create an index for a collection"; @@ -72,15 +139,19 @@ export class CreateIndexTool extends MongoDBToolBase { definition: z .array( z.discriminatedUnion("type", [ - z.object({ - type: z.literal("classic"), - keys: z.object({}).catchall(z.custom()).describe("The index definition"), - }), - ...(this.isFeatureEnabled("search") ? [this.vectorSearchIndexDefinition] : []), + z + .object({ + type: z.literal("classic"), + keys: z.object({}).catchall(z.custom()).describe("The index definition"), + }) + .describe("Definition for a MongoDB index (e.g. ascending/descending/geospatial)."), + ...(this.isFeatureEnabled("search") + ? [this.vectorSearchIndexDefinition, this.atlasSearchIndexDefinition] + : []), ]) ) .describe( - `The index definition. Use 'classic' for standard indexes${this.isFeatureEnabled("search") ? " and 'vectorSearch' for vector search indexes" : ""}.` + `The index definition. Use 'classic' for standard indexes${this.isFeatureEnabled("search") ? ", 'vectorSearch' for vector search indexes, and 'search' for Atlas Search (lexical) indexes" : ""}.` ), }; @@ -130,6 +201,26 @@ export class CreateIndexTool extends MongoDBToolBase { this.session.vectorSearchEmbeddingsManager.cleanupEmbeddingsForNamespace({ database, collection }); } + break; + case "search": + { + await this.ensureSearchIsSupported(); + indexes = await provider.createSearchIndexes(database, collection, [ + { + name, + definition: { + mappings: definition.mappings, + analyzer: definition.analyzer, + numPartitions: definition.numPartitions, + }, + type: "search", + }, + ]); + + responseClarification = + " Since this is a search index, it may take a while for the index to build. Use the `list-indexes` tool to check the index status."; + } + break; } diff --git a/src/tools/mongodb/read/aggregate.ts b/src/tools/mongodb/read/aggregate.ts index 15e21bf6e..b98ed68c3 100644 --- a/src/tools/mongodb/read/aggregate.ts +++ b/src/tools/mongodb/read/aggregate.ts @@ -20,7 +20,8 @@ import { const pipelineDescriptionWithVectorSearch = `\ An array of aggregation stages to execute. -\`$vectorSearch\` **MUST** be the first stage of the pipeline, or the first stage of a \`$unionWith\` subpipeline. +If the user has asked for a vector search, \`$vectorSearch\` **MUST** be the first stage of the pipeline, or the first stage of a \`$unionWith\` subpipeline. +If the user has asked for lexical/Atlas search, use \`$search\` instead of \`$text\`. ### Usage Rules for \`$vectorSearch\` - **Unset embeddings:** Unless the user explicitly requests the embeddings, add an \`$unset\` stage **at the end of the pipeline** to remove the embedding field and avoid context limits. **The $unset stage in this situation is mandatory**. @@ -29,9 +30,12 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi NEVER include fields in $vectorSearch.filter that are not part of the vector index. - **Post-filtering:** For all remaining filters, add a $match stage after $vectorSearch. -### Note to LLM - If unsure which fields are filterable, use the collection-indexes tool to determine valid prefilter fields. -- If no requested filters are valid prefilters, omit the filter key from $vectorSearch.\ +- If no requested filters are valid prefilters, omit the filter key from $vectorSearch. + +### Usage Rules for \`$search\` +- Include the index name, unless you know for a fact there's a default index. If unsure, use the collection-indexes tool to determine the index name. +- The \`$search\` stage supports multiple operators, such as 'autocomplete', 'text', 'geoWithin', and others. Choose the approprate operator based on the user's query. If unsure of the exact syntax, consult the MongoDB Atlas Search documentation, which can be found here: https://www.mongodb.com/docs/atlas/atlas-search/operators-and-collectors/ `; const genericPipelineDescription = "An array of aggregation stages to execute."; diff --git a/tests/accuracy/aggregate.test.ts b/tests/accuracy/aggregate.test.ts index 36bd7f9c1..df0644c78 100644 --- a/tests/accuracy/aggregate.test.ts +++ b/tests/accuracy/aggregate.test.ts @@ -421,4 +421,27 @@ describeAccuracyTests([ }, }, }, + { + prompt: "Run a $search query on mflix.movies to find all movies that mention 'space travel' in the plot or title. Use the default search index.", + expectedToolCalls: [ + { + toolName: "aggregate", + parameters: { + database: "mflix", + collection: "movies", + pipeline: [ + { + $search: { + index: Matcher.anyOf(Matcher.undefined, Matcher.value("default")), + text: { + query: "space travel", + path: ["plot", "title"], + }, + }, + }, + ], + }, + }, + ], + }, ]); diff --git a/tests/accuracy/createIndex.test.ts b/tests/accuracy/createIndex.test.ts index 410464c99..6d3f2a2a8 100644 --- a/tests/accuracy/createIndex.test.ts +++ b/tests/accuracy/createIndex.test.ts @@ -1,6 +1,23 @@ +import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; +import { formatUntrustedData } from "../../src/tools/tool.js"; +import type { MockedTools } from "./sdk/accuracyTestingClient.js"; import { describeAccuracyTests } from "./sdk/describeAccuracyTests.js"; import { Matcher } from "./sdk/matcher.js"; +const mockedTools: MockedTools = { + "collection-indexes": ({ collection }: Record): CallToolResult => { + return { + content: formatUntrustedData( + `Found 1 indexes in the collection "${collection as string}".`, + JSON.stringify({ + name: "_id_", + key: { _id: 1 }, + }) + ), + }; + }, +}; + describeAccuracyTests( [ { @@ -23,6 +40,7 @@ describeAccuracyTests( }, }, ], + mockedTools, }, { prompt: "Create a text index on title field in 'mflix.movies' namespace", @@ -44,6 +62,7 @@ describeAccuracyTests( }, }, ], + mockedTools, }, { prompt: "Create a vector search index on 'mflix.movies' namespace on the 'plotSummary' field. The index should use 1024 dimensions.", @@ -61,7 +80,7 @@ describeAccuracyTests( { type: "vector", path: "plotSummary", - numDimensions: 1024, + numDimensions: "1024", }, ], }, @@ -69,6 +88,7 @@ describeAccuracyTests( }, }, ], + mockedTools, }, { prompt: "Create a vector search index on 'mflix.movies' namespace with on the 'plotSummary' field and 'genre' field, both of which contain vector embeddings. Pick a sensible number of dimensions for a voyage 3.5 model.", @@ -86,17 +106,19 @@ describeAccuracyTests( { type: "vector", path: "plotSummary", - numDimensions: Matcher.number( - (value) => value % 2 === 0 && value >= 256 && value <= 8192 - ), + numDimensions: Matcher.string((value) => { + const intValue = parseInt(value); + return intValue % 2 === 0 && intValue >= 256 && intValue <= 8192; + }), similarity: Matcher.anyOf(Matcher.undefined, Matcher.string()), }, { type: "vector", path: "genre", - numDimensions: Matcher.number( - (value) => value % 2 === 0 && value >= 256 && value <= 8192 - ), + numDimensions: Matcher.string((value) => { + const intValue = parseInt(value); + return intValue % 2 === 0 && intValue >= 256 && intValue <= 8192; + }), similarity: Matcher.anyOf(Matcher.undefined, Matcher.string()), }, ], @@ -105,6 +127,7 @@ describeAccuracyTests( }, }, ], + mockedTools, }, { prompt: "Create a vector search index on 'mflix.movies' namespace where the 'plotSummary' field is indexed as a 1024-dimensional vector and the 'releaseDate' field is indexed as a regular field.", @@ -122,7 +145,7 @@ describeAccuracyTests( { type: "vector", path: "plotSummary", - numDimensions: 1024, + numDimensions: "1024", }, { type: "filter", @@ -134,6 +157,97 @@ describeAccuracyTests( }, }, ], + mockedTools, + }, + { + prompt: "Create an Atlas search index on 'mflix.movies' namespace with dynamic mappings enabled", + expectedToolCalls: [ + { + toolName: "create-index", + parameters: { + database: "mflix", + collection: "movies", + name: Matcher.anyOf(Matcher.undefined, Matcher.string()), + definition: [ + { + type: "search", + analyzer: Matcher.anyOf(Matcher.undefined, Matcher.value("lucene.standard")), + mappings: { + dynamic: true, + }, + numPartitions: Matcher.anyOf(Matcher.undefined, Matcher.number()), + }, + ], + }, + }, + ], + mockedTools, + }, + { + prompt: "Create an Atlas search index on 'mflix.movies' namespace for searching on 'title' as string field and 'year' as number field", + expectedToolCalls: [ + { + toolName: "create-index", + parameters: { + database: "mflix", + collection: "movies", + name: Matcher.anyOf(Matcher.undefined, Matcher.string()), + definition: [ + { + type: "search", + analyzer: Matcher.anyOf(Matcher.undefined, Matcher.value("lucene.standard")), + mappings: { + dynamic: Matcher.anyOf(Matcher.undefined, Matcher.value(false)), + fields: { + title: { + type: "string", + }, + year: { + type: "number", + }, + }, + }, + numPartitions: Matcher.anyOf(Matcher.undefined, Matcher.number()), + }, + ], + }, + }, + ], + mockedTools, + }, + { + prompt: "Create an Atlas search index on 'mflix.movies' namespace with a custom 'lucene.keyword' analyzer, where 'title' is indexed as an autocomplete field and 'genres' as a string array field, and 'released' as a date field", + expectedToolCalls: [ + { + toolName: "create-index", + parameters: { + database: "mflix", + collection: "movies", + name: Matcher.anyOf(Matcher.undefined, Matcher.string()), + definition: [ + { + type: "search", + analyzer: "lucene.keyword", + mappings: { + dynamic: Matcher.anyOf(Matcher.undefined, Matcher.value(false)), + fields: { + title: { + type: "autocomplete", + }, + genres: { + type: "string", + }, + released: { + type: "date", + }, + }, + }, + }, + ], + }, + }, + ], + mockedTools, }, ], { diff --git a/tests/accuracy/sdk/matcher.ts b/tests/accuracy/sdk/matcher.ts index 7f403e3ac..bc6f91f6e 100644 --- a/tests/accuracy/sdk/matcher.ts +++ b/tests/accuracy/sdk/matcher.ts @@ -32,8 +32,8 @@ export abstract class Matcher { return new BooleanMatcher(expected); } - public static string(): Matcher { - return new StringMatcher(); + public static string(additionalFilter: (value: string) => boolean = () => true): Matcher { + return new StringMatcher(additionalFilter); } public static caseInsensitiveString(text: string): Matcher { @@ -153,8 +153,11 @@ class BooleanMatcher extends Matcher { } class StringMatcher extends Matcher { + constructor(private additionalFilter: (value: string) => boolean = () => true) { + super(); + } public match(actual: unknown): number { - return typeof actual === "string" ? 1 : 0; + return typeof actual === "string" && this.additionalFilter(actual) ? 1 : 0; } } diff --git a/tests/integration/tools/mongodb/create/createIndex.test.ts b/tests/integration/tools/mongodb/create/createIndex.test.ts index 2b8b359f4..af1fe080d 100644 --- a/tests/integration/tools/mongodb/create/createIndex.test.ts +++ b/tests/integration/tools/mongodb/create/createIndex.test.ts @@ -66,11 +66,15 @@ describeWithMongoDB( expect(definitionProperty.type).toEqual("array"); - // Because search is now enabled, we should see both "classic" and "vectorSearch" options in + // Because search is now enabled, we should see both "classic", "search", and "vectorSearch" options in // the anyOf array. - expect(definitionProperty.items.anyOf).toHaveLength(2); + expect(definitionProperty.items.anyOf).toHaveLength(3); + + // Classic index definition expect(definitionProperty.items.anyOf?.[0]?.properties?.type).toEqual({ type: "string", const: "classic" }); expect(definitionProperty.items.anyOf?.[0]?.properties?.keys).toBeDefined(); + + // Vector search index definition expect(definitionProperty.items.anyOf?.[1]?.properties?.type).toEqual({ type: "string", const: "vectorSearch", @@ -94,6 +98,23 @@ describeWithMongoDB( expectDefined(fields.items.anyOf?.[1]?.properties?.quantization); expectDefined(fields.items.anyOf?.[1]?.properties?.numDimensions); expectDefined(fields.items.anyOf?.[1]?.properties?.similarity); + + // Atlas search index definition + expect(definitionProperty.items.anyOf?.[2]?.properties?.type).toEqual({ + type: "string", + const: "search", + }); + expectDefined(definitionProperty.items.anyOf?.[2]?.properties?.analyzer); + expectDefined(definitionProperty.items.anyOf?.[2]?.properties?.mappings); + + const mappings = definitionProperty.items.anyOf?.[2]?.properties?.mappings as { + type: string; + properties: Record>; + }; + + expect(mappings.type).toEqual("object"); + expectDefined(mappings.properties?.dynamic); + expectDefined(mappings.properties?.fields); }); }, { @@ -115,7 +136,7 @@ describeWithMongoDB( name: "definition", type: "array", description: - "The index definition. Use 'classic' for standard indexes and 'vectorSearch' for vector search indexes.", + "The index definition. Use 'classic' for standard indexes, 'vectorSearch' for vector search indexes, and 'search' for Atlas Search (lexical) indexes.", required: true, }, { @@ -170,6 +191,26 @@ describeWithMongoDB( }, ], }, + { + collection: "bar", + database: "test", + definition: [{ type: "search", mappings: "invalid" }], + }, + { + collection: "bar", + database: "test", + definition: [{ type: "search", analyzer: 123 }], + }, + { + collection: "bar", + database: "test", + definition: [{ type: "search", mappings: { dynamic: "not-boolean" } }], + }, + { + collection: "bar", + database: "test", + definition: [{ type: "search", mappings: { fields: "not-an-object" } }], + }, ]); const validateIndex = async (collection: string, expected: { name: string; key: object }[]): Promise => { @@ -636,3 +677,281 @@ describeWithMongoDB( }, } ); + +describeWithMongoDB( + "createIndex tool with Atlas search indexes", + (integration) => { + beforeEach(async () => { + await integration.connectMcpClient(); + await waitUntilSearchIsReady(integration.mongoClient()); + }); + + // eslint-disable-next-line vitest/no-identical-title + describe("when the collection does not exist", () => { + it("throws an error", async () => { + const response = await integration.mcpClient().callTool({ + name: "create-index", + arguments: { + database: integration.randomDbName(), + collection: "foo", + definition: [ + { + type: "search", + mappings: { + dynamic: true, + }, + }, + ], + }, + }); + + const content = getResponseContent(response.content); + expect(content).toContain(`Collection '${integration.randomDbName()}.foo' does not exist`); + }); + }); + + // eslint-disable-next-line vitest/no-identical-title + describe("when the database does not exist", () => { + it("throws an error", async () => { + const response = await integration.mcpClient().callTool({ + name: "create-index", + arguments: { + database: "nonexistent_db", + collection: "foo", + definition: [ + { + type: "search", + mappings: { + dynamic: true, + }, + }, + ], + }, + }); + + const content = getResponseContent(response.content); + expect(content).toContain(`Collection 'nonexistent_db.foo' does not exist`); + }); + }); + + // eslint-disable-next-line vitest/no-identical-title + describe("when the collection exists", () => { + let collectionName: string; + let collection: Collection; + beforeEach(async () => { + collectionName = new ObjectId().toString(); + collection = await integration + .mongoClient() + .db(integration.randomDbName()) + .createCollection(collectionName); + }); + + afterEach(async () => { + await collection.drop(); + }); + + it("creates the index with explicit field mappings", async () => { + const response = await integration.mcpClient().callTool({ + name: "create-index", + arguments: { + database: integration.randomDbName(), + collection: collectionName, + name: "search_index", + definition: [ + { + type: "search", + analyzer: "lucene.standard", + mappings: { + dynamic: false, + fields: { + title: { type: "string" }, + content: { type: "string" }, + tags: { type: "string" }, + }, + }, + }, + ], + }, + }); + + const content = getResponseContent(response.content); + expect(content).toEqual( + `Created the index "search_index" on collection "${collectionName}" in database "${integration.randomDbName()}". Since this is a search index, it may take a while for the index to build. Use the \`list-indexes\` tool to check the index status.` + ); + + const indexes = (await collection.listSearchIndexes().toArray()) as unknown as Document[]; + expect(indexes).toHaveLength(1); + expect(indexes[0]?.name).toEqual("search_index"); + expect(indexes[0]?.type).toEqual("search"); + expect(indexes[0]?.status).toEqual("PENDING"); + expect(indexes[0]?.queryable).toEqual(false); + expect(indexes[0]?.latestDefinition).toMatchObject({ + analyzer: "lucene.standard", + mappings: { + dynamic: false, + fields: { + title: { type: "string" }, + content: { type: "string" }, + tags: { type: "string" }, + }, + }, + }); + }); + + it("creates the index with dynamic mappings", async () => { + const response = await integration.mcpClient().callTool({ + name: "create-index", + arguments: { + database: integration.randomDbName(), + collection: collectionName, + name: "dynamic_search_index", + definition: [ + { + type: "search", + mappings: { + dynamic: true, + }, + }, + ], + }, + }); + + const content = getResponseContent(response.content); + expect(content).toEqual( + `Created the index "dynamic_search_index" on collection "${collectionName}" in database "${integration.randomDbName()}". Since this is a search index, it may take a while for the index to build. Use the \`list-indexes\` tool to check the index status.` + ); + + const indexes = (await collection.listSearchIndexes().toArray()) as unknown as Document[]; + expect(indexes).toHaveLength(1); + expect(indexes[0]?.name).toEqual("dynamic_search_index"); + expect(indexes[0]?.type).toEqual("search"); + expect(indexes[0]?.status).toEqual("PENDING"); + expect(indexes[0]?.queryable).toEqual(false); + expect(indexes[0]?.latestDefinition).toEqual({ + analyzer: "lucene.standard", + mappings: { + dynamic: true, + fields: {}, + }, + }); + }); + + it("doesn't duplicate search indexes", async () => { + const response = await integration.mcpClient().callTool({ + name: "create-index", + arguments: { + database: integration.randomDbName(), + collection: collectionName, + name: "search_index", + definition: [ + { + type: "search", + mappings: { + dynamic: false, + fields: { + title: { type: "string" }, + }, + }, + }, + ], + }, + }); + + const content = getResponseContent(response.content); + expect(content).toEqual( + `Created the index "search_index" on collection "${collectionName}" in database "${integration.randomDbName()}". Since this is a search index, it may take a while for the index to build. Use the \`list-indexes\` tool to check the index status.` + ); + + // Try to create another search index with the same name + const duplicateSearchResponse = await integration.mcpClient().callTool({ + name: "create-index", + arguments: { + database: integration.randomDbName(), + collection: collectionName, + name: "search_index", + definition: [ + { + type: "search", + mappings: { + dynamic: true, + }, + }, + ], + }, + }); + + const duplicateSearchContent = getResponseContent(duplicateSearchResponse.content); + expect(duplicateSearchResponse.isError).toBe(true); + expect(duplicateSearchContent).toEqual( + "Error running create-index: Index search_index already exists with a different definition. Drop it first if needed." + ); + }); + + it("can create classic and Atlas search indexes with the same name", async () => { + const response = await integration.mcpClient().callTool({ + name: "create-index", + arguments: { + database: integration.randomDbName(), + collection: collectionName, + name: "my-search-index", + definition: [ + { + type: "search", + mappings: { + dynamic: true, + }, + }, + ], + }, + }); + + const content = getResponseContent(response.content); + expect(content).toEqual( + `Created the index "my-search-index" on collection "${collectionName}" in database "${integration.randomDbName()}". Since this is a search index, it may take a while for the index to build. Use the \`list-indexes\` tool to check the index status.` + ); + + const classicResponse = await integration.mcpClient().callTool({ + name: "create-index", + arguments: { + database: integration.randomDbName(), + collection: collectionName, + name: "my-search-index", + definition: [{ type: "classic", keys: { field1: 1 } }], + }, + }); + + // Create a classic index with the same name + const classicContent = getResponseContent(classicResponse.content); + expect(classicContent).toEqual( + `Created the index "my-search-index" on collection "${collectionName}" in database "${integration.randomDbName()}".` + ); + + const listIndexesResponse = await integration.mcpClient().callTool({ + name: "collection-indexes", + arguments: { + database: integration.randomDbName(), + collection: collectionName, + }, + }); + + const listIndexesElements = getResponseElements(listIndexesResponse.content); + expect(listIndexesElements).toHaveLength(4); // 2 elements for classic indexes, 2 for search indexes + + // Expect to find my-search-index in the classic definitions + expect(listIndexesElements[1]?.text).toContain('"name":"my-search-index"'); + + // Expect to find my-search-index in the search definitions + expect(listIndexesElements[3]?.text).toContain('"name":"my-search-index"'); + }); + }); + }, + { + getUserConfig: () => ({ + ...defaultTestConfig, + previewFeatures: ["search"], + }), + downloadOptions: { + search: true, + }, + } +); diff --git a/tests/integration/tools/mongodb/delete/dropIndex.test.ts b/tests/integration/tools/mongodb/delete/dropIndex.test.ts index 032a07690..931b2d9f4 100644 --- a/tests/integration/tools/mongodb/delete/dropIndex.test.ts +++ b/tests/integration/tools/mongodb/delete/dropIndex.test.ts @@ -53,10 +53,12 @@ function setupForClassicIndexes(integration: MongoDBIntegrationTestCase): { function setupForVectorSearchIndexes(integration: MongoDBIntegrationTestCase): { getMoviesCollection: () => Collection; - getIndexName: () => string; + getSearchIndexName: () => string; + getVectorIndexName: () => string; } { let moviesCollection: Collection; const indexName = "searchIdx"; + const vectorIndexName = "vectorIdx"; beforeEach(async () => { await integration.connectMcpClient(); const mongoClient = integration.mongoClient(); @@ -65,14 +67,24 @@ function setupForVectorSearchIndexes(integration: MongoDBIntegrationTestCase): { { name: "Movie1", plot: "This is a horrible movie about a database called BongoDB and how it tried to copy the OG MangoDB.", + embeddings: [0.1, 0.2, 0.3, 0.4], }, ]); await waitUntilSearchIsReady(mongoClient); await moviesCollection.createSearchIndex({ name: indexName, - definition: { mappings: { dynamic: true } }, + definition: { mappings: { fields: { plot: { type: "string" } } } }, + type: "search", + }); + await moviesCollection.createSearchIndex({ + name: vectorIndexName, + definition: { + fields: [{ path: "embeddings", type: "vector", numDimensions: 4, similarity: "euclidean" }], + }, + type: "vectorSearch", }); await waitUntilSearchIndexIsListed(moviesCollection, indexName); + await waitUntilSearchIndexIsListed(moviesCollection, vectorIndexName); }); afterEach(async () => { @@ -82,7 +94,8 @@ function setupForVectorSearchIndexes(integration: MongoDBIntegrationTestCase): { return { getMoviesCollection: () => moviesCollection, - getIndexName: () => indexName, + getSearchIndexName: () => indexName, + getVectorIndexName: () => vectorIndexName, }; } @@ -342,7 +355,8 @@ describe.each([{ vectorSearchEnabled: false }, { vectorSearchEnabled: true }])( describeWithMongoDB( "when connected to MongoDB with search support", (integration) => { - const { getIndexName } = setupForVectorSearchIndexes(integration); + const { getSearchIndexName, getVectorIndexName, getMoviesCollection } = + setupForVectorSearchIndexes(integration); describe.each([ { @@ -363,35 +377,40 @@ describe.each([{ vectorSearchEnabled: false }, { vectorSearchEnabled: true }])( collection: "movies", indexName: "non-existent-index", }, - ])( - "and attempting to delete $title (namespace - $database $collection)", - ({ database, collection, indexName }) => { - it("should fail with appropriate error", async () => { - const response = await integration.mcpClient().callTool({ - name: "drop-index", - arguments: { database, collection, indexName, type: "search" }, - }); - expect(response.isError).toBe(true); - const content = getResponseContent(response.content); - expect(content).toContain("Index does not exist in the provided namespace."); + ])("and attempting to delete $title", ({ database, collection, indexName }) => { + it("should fail with appropriate error", async () => { + const response = await integration.mcpClient().callTool({ + name: "drop-index", + arguments: { database, collection, indexName, type: "search" }, + }); + expect(response.isError).toBe(true); + const content = getResponseContent(response.content); + expect(content).toContain("Index does not exist in the provided namespace."); - const data = getDataFromUntrustedContent(content); - expect(JSON.parse(data)).toMatchObject({ - indexName, - namespace: `${database}.${collection}`, - }); + const data = getDataFromUntrustedContent(content); + expect(JSON.parse(data)).toMatchObject({ + indexName, + namespace: `${database}.${collection}`, }); - } - ); + }); + }); - describe("and attempting to delete an existing index", () => { + describe.each([ + { description: "search", getIndexName: getSearchIndexName }, + { description: "vector search", getIndexName: getVectorIndexName }, + ])("and attempting to delete an existing $description index", ({ getIndexName }) => { it("should succeed in deleting the index", async () => { + const indexName = getIndexName(); + const collection = getMoviesCollection(); + let indexes = await collection.listSearchIndexes().toArray(); + expect(indexes.find((idx) => idx.name === indexName)).toBeDefined(); + const response = await integration.mcpClient().callTool({ name: "drop-index", arguments: { - database: "mflix", - collection: "movies", - indexName: getIndexName(), + database: collection.dbName, + collection: collection.collectionName, + indexName, type: "search", }, }); @@ -402,9 +421,12 @@ describe.each([{ vectorSearchEnabled: false }, { vectorSearchEnabled: true }])( const data = getDataFromUntrustedContent(content); expect(JSON.parse(data)).toMatchObject({ - indexName: getIndexName(), + indexName, namespace: "mflix.movies", }); + + indexes = await collection.listSearchIndexes().toArray(); + expect(indexes.find((idx) => idx.name === indexName)).toBeUndefined(); }); }); }, @@ -418,7 +440,7 @@ describe.each([{ vectorSearchEnabled: false }, { vectorSearchEnabled: true }])( describeWithMongoDB( "when invoked via an elicitation enabled client", (integration) => { - const { getIndexName } = setupForVectorSearchIndexes(integration); + const { getSearchIndexName: getIndexName } = setupForVectorSearchIndexes(integration); let dropSearchIndexSpy: MockInstance; beforeEach(() => {