From b63916eff73e138d77c363dba36eee2522a7389b Mon Sep 17 00:00:00 2001 From: nirinchev Date: Mon, 27 Oct 2025 16:07:53 +0100 Subject: [PATCH 1/4] add support for creating atlas search indexes --- README.md | 1 - src/tools/mongodb/create/createIndex.ts | 203 +++++++---- tests/accuracy/createIndex.test.ts | 110 ++++++ tests/accuracy/sdk/accuracyTestingClient.ts | 2 +- .../tools/mongodb/create/createIndex.test.ts | 323 +++++++++++++++++- 5 files changed, 575 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 9c0675738..f50b9a8de 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/src/tools/mongodb/create/createIndex.ts b/src/tools/mongodb/create/createIndex.ts index 252d8c4c4..e950b0bfb 100644 --- a/src/tools/mongodb/create/createIndex.ts +++ b/src/tools/mongodb/create/createIndex.ts @@ -6,61 +6,121 @@ import type { IndexDirection } from "mongodb"; import { quantizationEnum, similarityEnum } from "../../../common/search/vectorSearchEmbeddingsManager.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: similarityEnum - .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: similarityEnum + .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 `mappings.dynamic` must be true or at least one field must be defined in `mappings.fields`", + }) + .describe( + "Document describing the index to create. Either `dynamic` must be true or at least one field must be defined in the `fields` document." + ), + }) + .describe("Definition for an Atlas Search (lexical) index."); public name = "create-index"; protected description = "Create an index for a collection"; @@ -70,15 +130,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. Use 'classic' for standard indexes, 'vectorSearch' for vector search indexes, and 'search' for Atlas Search (lexical) indexes." ), }; @@ -128,6 +192,25 @@ 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, + }, + 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/tests/accuracy/createIndex.test.ts b/tests/accuracy/createIndex.test.ts index 9a3a4f90c..ebcf4481a 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,95 @@ 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, + }, + }, + ], + }, + }, + ], + 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", + }, + }, + }, + }, + ], + }, + }, + ], + 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/accuracyTestingClient.ts b/tests/accuracy/sdk/accuracyTestingClient.ts index 7d00d6ec7..fc0145ce2 100644 --- a/tests/accuracy/sdk/accuracyTestingClient.ts +++ b/tests/accuracy/sdk/accuracyTestingClient.ts @@ -91,7 +91,7 @@ export class AccuracyTestingClient { return [`--${key}`, value]; }); - const args = [MCP_SERVER_CLI_SCRIPT, "--connectionString", mdbConnectionString, ...additionalArgs]; + const args = [MCP_SERVER_CLI_SCRIPT, mdbConnectionString, ...additionalArgs]; const clientTransport = new StdioClientTransport({ command: process.execPath, diff --git a/tests/integration/tools/mongodb/create/createIndex.test.ts b/tests/integration/tools/mongodb/create/createIndex.test.ts index f76bb5ba1..3914d3fad 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); }); }, { @@ -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, + }, + } +); From 3fab3b7bd3346f916e114a689e97960d11827d8b Mon Sep 17 00:00:00 2001 From: nirinchev Date: Mon, 27 Oct 2025 16:52:00 +0100 Subject: [PATCH 2/4] add more tests --- .../tools/mongodb/create/createIndex.test.ts | 2 +- .../tools/mongodb/delete/dropIndex.test.ts | 77 ++++++++++++------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/tests/integration/tools/mongodb/create/createIndex.test.ts b/tests/integration/tools/mongodb/create/createIndex.test.ts index 3914d3fad..af1c0e1e3 100644 --- a/tests/integration/tools/mongodb/create/createIndex.test.ts +++ b/tests/integration/tools/mongodb/create/createIndex.test.ts @@ -120,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, }, { diff --git a/tests/integration/tools/mongodb/delete/dropIndex.test.ts b/tests/integration/tools/mongodb/delete/dropIndex.test.ts index 9bee2457e..90efb8237 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,39 @@ 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", indexName: getSearchIndexName() }, + { description: "vector search", indexName: getVectorIndexName() }, + ])("and attempting to delete an existing $description index", ({ indexName }) => { it("should succeed in deleting the index", async () => { + 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 +419,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 +438,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(() => { From 809aa9a31da3c68467e68dac94e84a9d62f0c54e Mon Sep 17 00:00:00 2001 From: nirinchev Date: Tue, 28 Oct 2025 11:44:43 +0100 Subject: [PATCH 3/4] tweak descriptions and errors --- src/tools/mongodb/create/createIndex.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tools/mongodb/create/createIndex.ts b/src/tools/mongodb/create/createIndex.ts index e950b0bfb..edb5b5331 100644 --- a/src/tools/mongodb/create/createIndex.ts +++ b/src/tools/mongodb/create/createIndex.ts @@ -114,10 +114,10 @@ export class CreateIndexTool extends MongoDBToolBase { }) .refine((data) => data.dynamic !== !!(data.fields && Object.keys(data.fields).length > 0), { message: - "Either `mappings.dynamic` must be true or at least one field must be defined in `mappings.fields`", + "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 or at least one field must be defined in the `fields` document." + "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." ), }) .describe("Definition for an Atlas Search (lexical) index."); @@ -142,7 +142,7 @@ export class CreateIndexTool extends MongoDBToolBase { ]) ) .describe( - "The index definition. Use 'classic' for standard indexes, 'vectorSearch' for vector search indexes, and 'search' for Atlas Search (lexical) indexes." + `The index definition.${this.isFeatureEnabled("vectorSearch") ? " Use 'classic' for standard indexes, 'vectorSearch' for vector search indexes, and 'search' for Atlas Search (lexical) indexes." : ""}` ), }; From 23d1a17c66eb5a548b98ec48a10c8850112730a3 Mon Sep 17 00:00:00 2001 From: nirinchev Date: Wed, 29 Oct 2025 14:43:51 +0100 Subject: [PATCH 4/4] replace arg as a function --- tests/integration/tools/mongodb/delete/dropIndex.test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/integration/tools/mongodb/delete/dropIndex.test.ts b/tests/integration/tools/mongodb/delete/dropIndex.test.ts index 90efb8237..ee5126f7d 100644 --- a/tests/integration/tools/mongodb/delete/dropIndex.test.ts +++ b/tests/integration/tools/mongodb/delete/dropIndex.test.ts @@ -395,10 +395,11 @@ describe.each([{ vectorSearchEnabled: false }, { vectorSearchEnabled: true }])( }); describe.each([ - { description: "search", indexName: getSearchIndexName() }, - { description: "vector search", indexName: getVectorIndexName() }, - ])("and attempting to delete an existing $description index", ({ indexName }) => { + { 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();