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

mongodb-js / mongodb-mcp-server / 15979273150

30 Jun 2025 05:11PM UTC coverage: 75.857% (+0.9%) from 74.965%
15979273150

Pull #330

github

web-flow
Merge df8668ab0 into 53ea41c00
Pull Request #330: revert: rollback "chore: add is_container_env to telemetry MCP-2

235 of 392 branches covered (59.95%)

Branch coverage included in aggregate %.

41 of 45 new or added lines in 3 files covered. (91.11%)

1 existing line in 1 file now uncovered.

827 of 1008 relevant lines covered (82.04%)

59.8 hits per line

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

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

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

13
export abstract class MongoDBToolBase extends ToolBase {
14
    protected category: ToolCategory = "mongodb";
660✔
15

16
    protected async ensureConnected(): Promise<NodeDriverServiceProvider> {
17
        if (!this.session.serviceProvider && this.config.connectionString) {
144✔
18
            try {
19✔
19
                await this.connectToMongoDB(this.config.connectionString);
19✔
20
            } catch (error) {
21
                logger.error(
×
22
                    LogId.mongodbConnectFailure,
23
                    "mongodbTool",
24
                    `Failed to connect to MongoDB instance using the connection string from the config: ${error as string}`
25
                );
26
                throw new MongoDBError(ErrorCodes.MisconfiguredConnectionString, "Not connected to MongoDB.");
×
27
            }
28
        }
29

30
        if (!this.session.serviceProvider) {
144✔
31
            throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
19✔
32
        }
33

34
        return this.session.serviceProvider;
125✔
35
    }
36

37
    protected handleError(
38
        error: unknown,
39
        args: ToolArgs<typeof this.argsShape>
40
    ): Promise<CallToolResult> | CallToolResult {
41
        if (error instanceof MongoDBError) {
28✔
42
            switch (error.code) {
24!
43
                case ErrorCodes.NotConnectedToMongoDB:
44
                    return {
19✔
45
                        content: [
46
                            {
47
                                type: "text",
48
                                text: "You need to connect to a MongoDB instance before you can access its data.",
49
                            },
50
                            {
51
                                type: "text",
52
                                text: "Please use the 'connect' or 'switch-connection' tool to connect to a MongoDB instance.",
53
                            },
54
                        ],
55
                        isError: true,
56
                    };
57
                case ErrorCodes.MisconfiguredConnectionString:
58
                    return {
×
59
                        content: [
60
                            {
61
                                type: "text",
62
                                text: "The configured connection string is not valid. Please check the connection string and confirm it points to a valid MongoDB instance. Alternatively, use the 'switch-connection' tool to connect to a different instance.",
63
                            },
64
                        ],
65
                        isError: true,
66
                    };
67
                case ErrorCodes.ForbiddenCollscan:
68
                    return {
5✔
69
                        content: [
70
                            {
71
                                type: "text",
72
                                text: error.message,
73
                            },
74
                        ],
75
                        isError: true,
76
                    };
77
            }
78
        }
79

80
        return super.handleError(error, args);
4✔
81
    }
82

83
    protected connectToMongoDB(connectionString: string): Promise<void> {
84
        return this.session.connectToMongoDB(connectionString, this.config.connectOptions);
127✔
85
    }
86

87
    protected resolveTelemetryMetadata(
88
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
89
        args: ToolArgs<typeof this.argsShape>
90
    ): TelemetryToolMetadata {
91
        const metadata: TelemetryToolMetadata = {};
×
92

93
        // Add projectId to the metadata if running a MongoDB operation to an Atlas cluster
94
        if (this.session.connectedAtlasCluster?.projectId) {
×
95
            metadata.projectId = this.session.connectedAtlasCluster.projectId;
×
96
        }
97

98
        return metadata;
×
99
    }
100
}
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