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 d16928200..ab5c5bf4b 100644 --- a/README.md +++ b/README.md @@ -663,7 +663,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 24dab3daf..4930f1998 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,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": "npm run build", "build:clean": "rm -rf dist", diff --git a/src/tools/args.ts b/src/tools/args.ts index 11b5b8b80..ef9436184 100644 --- a/src/tools/args.ts +++ b/src/tools/args.ts @@ -18,6 +18,17 @@ 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: ( ) @ & + : . _ - ' ,"; + +// Zod does not undestand JS boxed numbers (like Int32) as integer literals, +// so we preprocess them to unwrap them so Zod understands them. +function unboxNumber(v: unknown): number { + if (v && typeof v === "object" && typeof v.valueOf === "function") { + const n = Number(v.valueOf()); + if (!Number.isNaN(n)) return n; + } + return v as number; +} + export const CommonArgs = { string: (): ZodString => z.string().regex(NO_UNICODE_REGEX, NO_UNICODE_ERROR), @@ -27,6 +38,11 @@ export const CommonArgs = { .min(1, `${fieldName} is required`) .length(24, `${fieldName} must be exactly 24 characters`) .regex(/^[0-9a-fA-F]+$/, `${fieldName} must contain only hexadecimal characters`), + numberEnum: >( + values: Options + ): z.ZodEffects> => { + return z.preprocess(unboxNumber, z.union(values)); + }, }; export const AtlasArgs = { diff --git a/src/tools/mongodb/create/createIndex.ts b/src/tools/mongodb/create/createIndex.ts index 68ad4d918..192024f2c 100644 --- a/src/tools/mongodb/create/createIndex.ts +++ b/src/tools/mongodb/create/createIndex.ts @@ -5,64 +5,130 @@ import { type ToolArgs, type OperationType } from "../../tool.js"; import type { IndexDirection } from "mongodb"; import { quantizationEnum } from "../../../common/search/vectorSearchEmbeddingsManager.js"; import { similarityValues } from "../../../common/schemas.js"; +import { CommonArgs } from "../../args.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: CommonArgs.numberEnum([z.literal(1), z.literal(2), z.literal(4)]) + .default(1) + .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 +138,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("vectorSearch") ? [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("vectorSearch") + ? [this.vectorSearchIndexDefinition, this.atlasSearchIndexDefinition] + : []), ]) ) .describe( - "The index definition. Use 'classic' for standard indexes and 'vectorSearch' for vector search indexes" + `The index definition.${this.isFeatureEnabled("vectorSearch") ? " Use 'classic' for standard indexes, 'vectorSearch' for vector search indexes, and 'search' for Atlas Search (lexical) indexes." : ""}` ), }; @@ -130,6 +200,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/mongodbSchemas.ts b/src/tools/mongodb/mongodbSchemas.ts index cfae16b23..fbabe611b 100644 --- a/src/tools/mongodb/mongodbSchemas.ts +++ b/src/tools/mongodb/mongodbSchemas.ts @@ -1,26 +1,18 @@ import z from "zod"; -import { zEJSON } from "../args.js"; +import { CommonArgs, zEJSON } from "../args.js"; export const zVoyageModels = z .enum(["voyage-3-large", "voyage-3.5", "voyage-3.5-lite", "voyage-code-3"]) .default("voyage-3-large"); -// Zod does not undestand JS boxed numbers (like Int32) as integer literals, -// so we preprocess them to unwrap them so Zod understands them. -function unboxNumber(v: unknown): number { - if (v && typeof v === "object" && typeof v.valueOf === "function") { - const n = Number(v.valueOf()); - if (!Number.isNaN(n)) return n; - } - return v as number; -} - export const zVoyageEmbeddingParameters = z.object({ - outputDimension: z - .preprocess( - unboxNumber, - z.union([z.literal(256), z.literal(512), z.literal(1024), z.literal(2048), z.literal(4096)]) - ) + outputDimension: CommonArgs.numberEnum([ + z.literal(256), + z.literal(512), + z.literal(1024), + z.literal(2048), + z.literal(4096), + ]) .optional() .default(1024), outputDtype: z.enum(["float", "int8", "uint8", "binary", "ubinary"]).optional().default("float"), diff --git a/tests/accuracy/aggregate.test.ts b/tests/accuracy/aggregate.test.ts index 36bd7f9c1..c57f040a4 100644 --- a/tests/accuracy/aggregate.test.ts +++ b/tests/accuracy/aggregate.test.ts @@ -421,4 +421,26 @@ describeAccuracyTests([ }, }, }, + { + prompt: "Run a search query on mflix.movies to find all movies that mention 'space travel' in the plot.", + expectedToolCalls: [ + { + toolName: "aggregate", + parameters: { + database: "mflix", + collection: "movies", + pipeline: [ + { + $search: { + index: "default", + text: { + query: "space travel", + }, + }, + }, + ], + }, + }, + ], + }, ]); diff --git a/tests/accuracy/createIndex.test.ts b/tests/accuracy/createIndex.test.ts index 9a3a4f90c..18216d0ef 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.", @@ -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.", @@ -105,6 +125,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.", @@ -134,6 +155,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/integration/tools/mongodb/create/createIndex.test.ts b/tests/integration/tools/mongodb/create/createIndex.test.ts index f76bb5ba1..af1c0e1e3 100644 --- a/tests/integration/tools/mongodb/create/createIndex.test.ts +++ b/tests/integration/tools/mongodb/create/createIndex.test.ts @@ -50,11 +50,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", @@ -78,6 +82,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); }); }, { @@ -99,7 +120,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, }, { @@ -154,6 +175,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 => { @@ -620,3 +661,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: ["vectorSearch"], + }), + downloadOptions: { + search: true, + }, + } +); diff --git a/tests/integration/tools/mongodb/delete/dropIndex.test.ts b/tests/integration/tools/mongodb/delete/dropIndex.test.ts index 9bee2457e..ee5126f7d 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, }; } @@ -341,7 +354,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([ { @@ -362,35 +376,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", }, }); @@ -401,9 +420,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(); }); }); }, @@ -417,7 +439,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(() => {