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

mongodb-js / mongodb-mcp-server / 18978566012

31 Oct 2025 04:18PM UTC coverage: 80.142% (+0.2%) from 79.922%
18978566012

Pull #653

github

web-flow
Merge f636ea300 into f56f77206
Pull Request #653: chore: update atlas tools output to json - MCP-264

1349 of 1803 branches covered (74.82%)

Branch coverage included in aggregate %.

37 of 60 new or added lines in 6 files covered. (61.67%)

46 existing lines in 7 files now uncovered.

6428 of 7901 relevant lines covered (81.36%)

70.31 hits per line

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

97.93
/src/tools/mongodb/create/createIndex.ts
1
import { z } from "zod";
3✔
2
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
3✔
4
import { type ToolArgs, type OperationType } from "../../tool.js";
5
import type { IndexDirection } from "mongodb";
6
import { quantizationEnum } from "../../../common/search/vectorSearchEmbeddingsManager.js";
3✔
7
import { similarityValues } from "../../../common/schemas.js";
3✔
8

9
export class CreateIndexTool extends MongoDBToolBase {
3✔
10
    private vectorSearchIndexDefinition = z.object({
99✔
11
        type: z.literal("vectorSearch"),
99✔
12
        fields: z
99✔
13
            .array(
99✔
14
                z.discriminatedUnion("type", [
99✔
15
                    z
99✔
16
                        .object({
99✔
17
                            type: z.literal("filter"),
99✔
18
                            path: z
99✔
19
                                .string()
99✔
20
                                .describe(
99✔
21
                                    "Name of the field to index. For nested fields, use dot notation to specify path to embedded fields"
99✔
22
                                ),
99✔
23
                        })
99✔
24
                        .strict()
99✔
25
                        .describe("Definition for a field that will be used for pre-filtering results."),
99✔
26
                    z
99✔
27
                        .object({
99✔
28
                            type: z.literal("vector"),
99✔
29
                            path: z
99✔
30
                                .string()
99✔
31
                                .describe(
99✔
32
                                    "Name of the field to index. For nested fields, use dot notation to specify path to embedded fields"
99✔
33
                                ),
99✔
34
                            numDimensions: z
99✔
35
                                .number()
99✔
36
                                .min(1)
99✔
37
                                .max(8192)
99✔
38
                                .default(this.config.vectorSearchDimensions)
99✔
39
                                .describe(
99✔
40
                                    "Number of vector dimensions that MongoDB Vector Search enforces at index-time and query-time"
99✔
41
                                ),
99✔
42
                            similarity: z
99✔
43
                                .enum(similarityValues)
99✔
44
                                .default(this.config.vectorSearchSimilarityFunction)
99✔
45
                                .describe(
99✔
46
                                    "Vector similarity function to use to search for top K-nearest neighbors. You can set this field only for vector-type fields."
99✔
47
                                ),
99✔
48
                            quantization: quantizationEnum
99✔
49
                                .default("none")
99✔
50
                                .describe(
99✔
51
                                    "Type of automatic vector quantization for your vectors. Use this setting only if your embeddings are float or double vectors."
99✔
52
                                ),
99✔
53
                        })
99✔
54
                        .strict()
99✔
55
                        .describe("Definition for a field that contains vector embeddings."),
99✔
56
                ])
99✔
57
            )
99✔
58
            .nonempty()
99✔
59
            .refine((fields) => fields.some((f) => f.type === "vector"), {
99✔
60
                message: "At least one vector field must be defined",
99✔
61
            })
99✔
62
            .describe(
99✔
63
                "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."
99✔
64
            ),
99✔
65
    });
99✔
66

67
    public name = "create-index";
99✔
68
    protected description = "Create an index for a collection";
99✔
69
    protected argsShape = {
99✔
70
        ...DbOperationArgs,
99✔
71
        name: z.string().optional().describe("The name of the index"),
99✔
72
        definition: z
99✔
73
            .array(
99✔
74
                z.discriminatedUnion("type", [
99✔
75
                    z.object({
99✔
76
                        type: z.literal("classic"),
99✔
77
                        keys: z.object({}).catchall(z.custom<IndexDirection>()).describe("The index definition"),
99✔
78
                    }),
99✔
79
                    ...(this.isFeatureEnabled("vectorSearch") ? [this.vectorSearchIndexDefinition] : []),
99✔
80
                ])
99✔
81
            )
99✔
82
            .describe(
99✔
83
                "The index definition. Use 'classic' for standard indexes and 'vectorSearch' for vector search indexes"
99✔
84
            ),
99✔
85
    };
99✔
86

87
    public operationType: OperationType = "create";
99✔
88

89
    protected async execute({
3✔
90
        database,
24✔
91
        collection,
24✔
92
        name,
24✔
93
        definition: definitions,
24✔
94
    }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
24✔
95
        const provider = await this.ensureConnected();
24✔
96
        let indexes: string[] = [];
23✔
97
        const definition = definitions[0];
23✔
98
        if (!definition) {
24!
UNCOV
99
            throw new Error("Index definition not provided. Expected one of the following: `classic`, `vectorSearch`");
×
UNCOV
100
        }
✔
101

102
        let responseClarification = "";
23✔
103

104
        switch (definition.type) {
23✔
105
            case "classic":
24✔
106
                indexes = await provider.createIndexes(database, collection, [
16✔
107
                    {
16✔
108
                        key: definition.keys,
16✔
109
                        name,
16✔
110
                    },
16✔
111
                ]);
16✔
112
                break;
16✔
113
            case "vectorSearch":
24✔
114
                {
7✔
115
                    await this.ensureSearchIsSupported();
7✔
116
                    indexes = await provider.createSearchIndexes(database, collection, [
6✔
117
                        {
6✔
118
                            name,
6✔
119
                            definition: {
6✔
120
                                fields: definition.fields,
6✔
121
                            },
6✔
122
                            type: "vectorSearch",
6✔
123
                        },
6✔
124
                    ]);
6✔
125

126
                    responseClarification =
3✔
127
                        " Since this is a vector search index, it may take a while for the index to build. Use the `list-indexes` tool to check the index status.";
3✔
128

129
                    // clean up the embeddings cache so it considers the new index
130
                    this.session.vectorSearchEmbeddingsManager.cleanupEmbeddingsForNamespace({ database, collection });
3✔
131
                }
3✔
132

133
                break;
3✔
134
        }
24✔
135

136
        return {
19✔
137
            content: [
19✔
138
                {
19✔
139
                    text: `Created the index "${indexes[0]}" on collection "${collection}" in database "${database}".${responseClarification}`,
19✔
140
                    type: "text",
19✔
141
                },
19✔
142
            ],
19✔
143
        };
19✔
144
    }
24✔
145
}
3✔
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