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

mongodb-js / mongodb-mcp-server / 18338555347

08 Oct 2025 08:25AM UTC coverage: 82.603% (+0.1%) from 82.505%
18338555347

Pull #610

github

web-flow
Merge fd5bb0348 into 6c5168d35
Pull Request #610: feat(search): Add a new tool to list search and vector search indexes MCP-235

1109 of 1458 branches covered (76.06%)

Branch coverage included in aggregate %.

54 of 63 new or added lines in 2 files covered. (85.71%)

5358 of 6371 relevant lines covered (84.1%)

68.78 hits per line

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

81.08
/src/tools/mongodb/search/listSearchIndexes.ts
1
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2
import type { ToolArgs, OperationType } from "../../tool.js";
3
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
2✔
4
import { formatUntrustedData } from "../../tool.js";
2✔
5
import { EJSON } from "bson";
2✔
6

7
export type SearchIndexStatus = {
8
    name: string;
9
    type: string;
10
    status: string;
11
    queryable: boolean;
12
    latestDefinition: Document;
13
};
14

15
export class ListSearchIndexesTool extends MongoDBToolBase {
2✔
16
    public name = "list-search-indexes";
78✔
17
    protected description = "Describes the search and vector search indexes for a single collection";
78✔
18
    protected argsShape = DbOperationArgs;
78✔
19
    public operationType: OperationType = "metadata";
78✔
20

21
    protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
2✔
22
        const provider = await this.ensureConnected();
5✔
23
        const indexes = await provider.getSearchIndexes(database, collection);
5✔
24
        const trimmedIndexDefinitions = this.pickRelevantInformation(indexes);
4✔
25

26
        if (trimmedIndexDefinitions.length > 0) {
5✔
27
            return {
2✔
28
                content: formatUntrustedData(
2✔
29
                    `Found ${trimmedIndexDefinitions.length} search and vector search indexes in ${database}.${collection}`,
2✔
30
                    trimmedIndexDefinitions.map((index) => EJSON.stringify(index)).join("\n")
2✔
31
                ),
2✔
32
            };
2✔
33
        } else {
2✔
34
            return {
2✔
35
                content: formatUntrustedData(
2✔
36
                    `There are no search or vector search indexes in ${database}.${collection}`
2✔
37
                ),
2✔
38
            };
2✔
39
        }
2✔
40
    }
5✔
41

42
    /**
43
     * Atlas Search index status contains a lot of information that is not relevant for the agent at this stage.
44
     * Like for example, the status on each of the dedicated nodes. We only care about the main status, if it's
45
     * queryable and the index name. We are also picking the index definition as it can be used by the agent to
46
     * understand which fields are available for searching.
47
     **/
48
    protected pickRelevantInformation(indexes: Record<string, unknown>[]): SearchIndexStatus[] {
2✔
49
        return indexes.map((index) => ({
4✔
50
            name: (index["name"] ?? "default") as string,
2!
51
            type: (index["type"] ?? "UNKNOWN") as string,
2!
52
            status: (index["status"] ?? "UNKNOWN") as string,
2!
53
            queryable: (index["queryable"] ?? false) as boolean,
2!
54
            latestDefinition: index["latestDefinition"] as Document,
2✔
55
        }));
4✔
56
    }
4✔
57

58
    protected handleError(
2✔
59
        error: unknown,
1✔
60
        { database, collection }: ToolArgs<typeof DbOperationArgs>
1✔
61
    ): Promise<CallToolResult> | CallToolResult {
1✔
62
        if (error instanceof Error && "codeName" in error && error.codeName === "SearchNotEnabled") {
1✔
63
            return {
1✔
64
                content: [
1✔
65
                    {
1✔
66
                        text: "This MongoDB cluster does not support Search Indexes. Make sure you are using an Atlas Cluster, either remotely in Atlas or using the Atlas Local image, or your cluster supports MongoDB Search.",
1✔
67
                        type: "text",
1✔
68
                    },
1✔
69
                ],
1✔
70
            };
1✔
71
        } else {
1!
NEW
72
            return {
×
NEW
73
                content: [
×
NEW
74
                    {
×
NEW
75
                        text: `Could not retrieve indexes in ${database}.${collection}: ${EJSON.stringify(error)}.`,
×
NEW
76
                        type: "text",
×
NEW
77
                    },
×
NEW
78
                ],
×
NEW
79
            };
×
NEW
80
        }
×
81
    }
1✔
82
}
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