• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb-js / mongodb-mcp-server / 18342272475

08 Oct 2025 10:52AM UTC coverage: 82.531% (+0.03%) from 82.505%
18342272475

Pull #621

github

web-flow
Merge 33ba4d126 into 543301c2d
Pull Request #621: feat: add ability to create vector search indexes MCP-234

1101 of 1447 branches covered (76.09%)

Branch coverage included in aggregate %.

63 of 73 new or added lines in 1 file covered. (86.3%)

37 existing lines in 2 files now uncovered.

5362 of 6384 relevant lines covered (83.99%)

67.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.42
/src/tools/mongodb/create/createIndex.ts
1
import { z } from "zod";
2✔
2
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
2✔
4
import type { ToolArgs, OperationType } from "../../tool.js";
5
import type { IndexDirection } from "mongodb";
6

7
export class CreateIndexTool extends MongoDBToolBase {
2✔
8
    public name = "create-index";
77✔
9
    protected description = "Create an index for a collection";
77✔
10
    protected argsShape = {
77✔
11
        ...DbOperationArgs,
77✔
12
        name: z.string().optional().describe("The name of the index"),
77✔
13
        definition: z
77✔
14
            .discriminatedUnion("type", [
77✔
15
                z.object({
77✔
16
                    type: z.literal("classic"),
77✔
17
                    keys: z.object({}).catchall(z.custom<IndexDirection>()).describe("The index definition"),
77✔
18
                }),
77✔
19
                z.object({
77✔
20
                    type: z.literal("vectorSearch"),
77✔
21
                    fields: z
77✔
22
                        .array(
77✔
23
                            z.object({
77✔
24
                                type: z
77✔
25
                                    .enum(["vector", "filter"])
77✔
26
                                    .describe(
77✔
27
                                        "Field type to use to index fields for vector search. You must specify `vector` for fields that contain vector embeddings and `filter` for additional fields to filter on."
77✔
28
                                    ),
77✔
29
                                path: z
77✔
30
                                    .string()
77✔
31
                                    .describe(
77✔
32
                                        "Name of the field to index. For nested fields, use dot notation to specify path to embedded fields"
77✔
33
                                    ),
77✔
34
                                numDimensions: z
77✔
35
                                    .number()
77✔
36
                                    .min(1)
77✔
37
                                    .max(8192)
77✔
38
                                    .describe(
77✔
39
                                        "Number of vector dimensions that MongoDB Vector Search enforces at index-time and query-time"
77✔
40
                                    ),
77✔
41
                                similarity: z
77✔
42
                                    .enum(["cosine", "euclidean", "dotProduct"])
77✔
43
                                    .describe(
77✔
44
                                        "Vector similarity function to use to search for top K-nearest neighbors. You can set this field only for vector-type fields."
77✔
45
                                    ),
77✔
46
                                quantization: z
77✔
47
                                    .enum(["none", "scalar", "binary"])
77✔
48
                                    .optional()
77✔
49
                                    .default("none")
77✔
50
                                    .describe(
77✔
51
                                        "Type of automatic vector quantization for your vectors. Use this setting only if your embeddings are float or double vectors."
77✔
52
                                    ),
77✔
53
                            })
77✔
54
                        )
77✔
55
                        .describe(
77✔
56
                            "Definitions for the vector and filter fields to index, one definition per document. The fields array must contain at least one vector-type field definition."
77✔
57
                        ),
77✔
58
                }),
77✔
59
            ])
77✔
60
            .describe(
77✔
61
                "The index definition. Use 'classic' for standard indexes and 'vectorSearch' for vector search indexes"
77✔
62
            ),
77✔
63
    };
77✔
64

65
    public operationType: OperationType = "create";
77✔
66

67
    protected async execute({
2✔
68
        database,
16✔
69
        collection,
16✔
70
        name,
16✔
71
        definition,
16✔
72
    }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
16✔
73
        const provider = await this.ensureConnected();
16✔
74
        let indexes: string[] = [];
15✔
75
        switch (definition.type) {
15✔
76
            case "classic":
15✔
77
                indexes = await provider.createIndexes(database, collection, [
15✔
78
                    {
15✔
79
                        key: definition.keys,
15✔
80
                        name,
15✔
81
                    },
15✔
82
                ]);
15✔
83

84
                break;
15✔
85
            case "vectorSearch":
16!
NEW
86
                indexes = await provider.createSearchIndexes(database, collection, [
×
NEW
87
                    {
×
NEW
88
                        name,
×
NEW
89
                        definition: {
×
NEW
90
                            fields: definition.fields,
×
NEW
91
                        },
×
NEW
92
                        type: "vectorSearch",
×
NEW
93
                    },
×
NEW
94
                ]);
×
NEW
95
                break;
×
96
        }
16✔
97

98
        return {
15✔
99
            content: [
15✔
100
                {
15✔
101
                    text: `Created the index "${indexes[0]}" on collection "${collection}" in database "${database}"`,
15✔
102
                    type: "text",
15✔
103
                },
15✔
104
            ],
15✔
105
        };
15✔
106
    }
16✔
107
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc