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

mongodb-js / mongodb-mcp-server / 19532221561

20 Nov 2025 09:34AM UTC coverage: 80.12% (-0.2%) from 80.322%
19532221561

Pull #740

github

web-flow
Merge a49fad576 into 40cb62e9f
Pull Request #740: chore: add createSessionConfig hook MCP-294

1338 of 1762 branches covered (75.94%)

Branch coverage included in aggregate %.

10 of 10 new or added lines in 1 file covered. (100.0%)

34 existing lines in 3 files now uncovered.

6388 of 7881 relevant lines covered (81.06%)

73.18 hits per line

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

84.09
/src/tools/mongodb/mongodbTool.ts
1
import { z } from "zod";
3✔
2
import type { ToolArgs, ToolCategory } from "../tool.js";
3
import { ToolBase } from "../tool.js";
3✔
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";
3✔
7
import { LogId } from "../../common/logger.js";
3✔
8
import type { Server } from "../../server.js";
9
import type { ConnectionMetadata } from "../../telemetry/types.js";
10
import type { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
11

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

17
export abstract class MongoDBToolBase extends ToolBase {
3✔
18
    protected server?: Server;
19
    public category: ToolCategory = "mongodb";
2,330✔
20

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

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

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

48
        return this.session.serviceProvider;
230✔
49
    }
266✔
50

51
    protected ensureSearchIsSupported(): Promise<void> {
3✔
52
        return this.session.assertSearchSupported();
13✔
53
    }
13✔
54

55
    public register(server: Server): boolean {
3✔
56
        this.server = server;
2,330✔
57
        return super.register(server);
2,330✔
58
    }
2,330✔
59

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

79
                    return super.handleError(error, args);
×
80
                }
×
81
                case ErrorCodes.ForbiddenCollscan:
56!
82
                    return {
7✔
83
                        content: [
7✔
84
                            {
7✔
85
                                type: "text",
7✔
86
                                text: error.message,
7✔
87
                            },
7✔
88
                        ],
7✔
89
                        isError: true,
7✔
90
                    };
7✔
91
                case ErrorCodes.AtlasSearchNotSupported: {
56!
92
                    const CTA = this.server?.isToolCategoryAvailable("atlas-local" as unknown as ToolCategory)
2✔
93
                        ? "`atlas-local` tools"
2!
94
                        : "Atlas CLI";
×
95
                    return {
2✔
96
                        content: [
2✔
97
                            {
2✔
98
                                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✔
99
                                type: "text",
2✔
100
                            },
2✔
101
                        ],
2✔
102
                        isError: true,
2✔
103
                    };
2✔
104
                }
2✔
105
            }
56✔
106
        }
56!
107

108
        return super.handleError(error, args);
21✔
109
    }
68✔
110

111
    /**
112
     * Resolves the tool metadata from the arguments passed to the mongoDB tools.
113
     *
114
     * Since MongoDB tools are executed against a MongoDB instance, the tool calls will always have the connection information.
115
     *
116
     * @param result - The result of the tool call.
117
     * @param args - The arguments passed to the tool
118
     * @returns The tool metadata
119
     */
120
    protected resolveTelemetryMetadata(
3✔
121
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
122
        _result: CallToolResult,
2✔
123
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
124
        _args: Parameters<ToolCallback<typeof this.argsShape>>
2✔
125
    ): ConnectionMetadata {
2✔
126
        return this.getConnectionInfoMetadata();
2✔
127
    }
2✔
128
}
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