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

mongodb-js / mongodb-mcp-server / 18556878086

16 Oct 2025 09:33AM UTC coverage: 82.747% (+0.2%) from 82.527%
18556878086

Pull #626

github

web-flow
Merge 3d69362c7 into 87ce0cf2d
Pull Request #626: chore: Add new session-level service for getting embeddings of a specific collection MCP-246

1244 of 1635 branches covered (76.09%)

Branch coverage included in aggregate %.

210 of 227 new or added lines in 15 files covered. (92.51%)

14 existing lines in 1 file now uncovered.

5950 of 7059 relevant lines covered (84.29%)

70.62 hits per line

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

74.03
/src/tools/mongodb/mongodbTool.ts
1
import { z } from "zod";
2✔
2
import type { ToolArgs, ToolCategory, TelemetryToolMetadata } from "../tool.js";
3
import { ToolBase } from "../tool.js";
2✔
4
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
5
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
6
import { ErrorCodes, MongoDBError } from "../../common/errors.js";
2✔
7
import { LogId } from "../../common/logger.js";
2✔
8
import type { Server } from "../../server.js";
9

10
export const DbOperationArgs = {
2✔
11
    database: z.string().describe("Database name"),
2✔
12
    collection: z.string().describe("Collection name"),
2✔
13
};
2✔
14

15
export abstract class MongoDBToolBase extends ToolBase {
2✔
16
    protected server?: Server;
17
    public category: ToolCategory = "mongodb";
1,994✔
18

19
    protected async ensureConnected(): Promise<NodeDriverServiceProvider> {
2✔
20
        if (!this.session.isConnectedToMongoDB) {
246✔
21
            if (this.session.connectedAtlasCluster) {
58!
22
                throw new MongoDBError(
×
23
                    ErrorCodes.NotConnectedToMongoDB,
×
24
                    `Attempting to connect to Atlas cluster "${this.session.connectedAtlasCluster.clusterName}", try again in a few seconds.`
×
25
                );
×
26
            }
×
27

28
            if (this.config.connectionString) {
58!
29
                try {
24✔
30
                    await this.connectToMongoDB(this.config.connectionString);
24✔
31
                } catch (error) {
22✔
32
                    this.session.logger.error({
2✔
33
                        id: LogId.mongodbConnectFailure,
2✔
34
                        context: "mongodbTool",
2✔
35
                        message: `Failed to connect to MongoDB instance using the connection string from the config: ${error as string}`,
2✔
36
                    });
2✔
37
                    throw new MongoDBError(ErrorCodes.MisconfiguredConnectionString, "Not connected to MongoDB.");
2✔
38
                }
2✔
39
            }
24✔
40
        }
58✔
41

42
        if (!this.session.isConnectedToMongoDB) {
246✔
43
            throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
34✔
44
        }
34!
45

46
        return this.session.serviceProvider;
210✔
47
    }
246✔
48

49
    protected async ensureSearchIsSupported(): Promise<void> {
2✔
50
        return await this.session.assertSearchSupported();
9✔
51
    }
9✔
52

53
    public register(server: Server): boolean {
2✔
54
        this.server = server;
1,994✔
55
        return super.register(server);
1,994✔
56
    }
1,994✔
57

58
    protected handleError(
2✔
59
        error: unknown,
63✔
60
        args: ToolArgs<typeof this.argsShape>
63✔
61
    ): Promise<CallToolResult> | CallToolResult {
63✔
62
        if (error instanceof MongoDBError) {
63✔
63
            switch (error.code) {
55✔
64
                case ErrorCodes.NotConnectedToMongoDB:
55✔
65
                case ErrorCodes.MisconfiguredConnectionString: {
55✔
66
                    const connectionError = error as MongoDBError<
38✔
67
                        ErrorCodes.NotConnectedToMongoDB | ErrorCodes.MisconfiguredConnectionString
68
                    >;
69
                    const outcome = this.server?.connectionErrorHandler(connectionError, {
38✔
70
                        availableTools: this.server?.tools ?? [],
38!
71
                        connectionState: this.session.connectionManager.currentConnectionState,
38✔
72
                    });
38✔
73
                    if (outcome?.errorHandled) {
38✔
74
                        return outcome.result;
38✔
75
                    }
38!
76

77
                    return super.handleError(error, args);
×
78
                }
×
79
                case ErrorCodes.ForbiddenCollscan:
55!
80
                    return {
7✔
81
                        content: [
7✔
82
                            {
7✔
83
                                type: "text",
7✔
84
                                text: error.message,
7✔
85
                            },
7✔
86
                        ],
7✔
87
                        isError: true,
7✔
88
                    };
7✔
89
                case ErrorCodes.AtlasSearchNotSupported: {
55!
90
                    const CTA = this.isToolCategoryAvailable("atlas-local" as unknown as ToolCategory)
2!
NEW
91
                        ? "`atlas-local` tools"
×
92
                        : "Atlas CLI";
2✔
93
                    return {
2✔
94
                        content: [
2✔
95
                            {
2✔
96
                                text: `The connected MongoDB deployment does not support vector search indexes. Either connect to a MongoDB Atlas cluster or use the ${CTA} to create and manage a local Atlas deployment.`,
2✔
97
                                type: "text",
2✔
98
                            },
2✔
99
                        ],
2✔
100
                        isError: true,
2✔
101
                    };
2✔
102
                }
2✔
103
                case ErrorCodes.AtlasSearchNotAvailable:
55!
NEW
UNCOV
104
                    return {
×
NEW
UNCOV
105
                        content: [
×
NEW
UNCOV
106
                            {
×
NEW
UNCOV
107
                                text: `The connected MongoDB deployment does support vector search indexes but they are not ready yet. Try again later.`,
×
NEW
UNCOV
108
                                type: "text",
×
NEW
UNCOV
109
                            },
×
NEW
UNCOV
110
                        ],
×
NEW
UNCOV
111
                        isError: true,
×
NEW
UNCOV
112
                    };
×
113
            }
55✔
114
        }
55!
115

116
        return super.handleError(error, args);
16✔
117
    }
63✔
118

119
    protected connectToMongoDB(connectionString: string): Promise<void> {
2✔
120
        return this.session.connectToMongoDB({ connectionString });
255✔
121
    }
255✔
122

123
    protected resolveTelemetryMetadata(
2✔
124
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
125
        args: ToolArgs<typeof this.argsShape>
×
126
    ): TelemetryToolMetadata {
×
127
        const metadata: TelemetryToolMetadata = {};
×
128

129
        // Add projectId to the metadata if running a MongoDB operation to an Atlas cluster
UNCOV
130
        if (this.session.connectedAtlasCluster?.projectId) {
×
UNCOV
131
            metadata.projectId = this.session.connectedAtlasCluster.projectId;
×
UNCOV
132
        }
×
133

UNCOV
134
        return metadata;
×
UNCOV
135
    }
×
136

137
    protected isToolCategoryAvailable(name: ToolCategory): boolean {
2✔
138
        return (this.server?.tools.filter((t) => t.category === name).length ?? 0) > 0;
2!
139
    }
2✔
140
}
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