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

mongodb-js / mongodb-mcp-server / 14637601756

24 Apr 2025 08:57AM UTC coverage: 42.737% (-32.7%) from 75.474%
14637601756

Pull #103

github

fmenezes
fix: rename describes
Pull Request #103: refactor: split test helpers

30 of 187 branches covered (16.04%)

Branch coverage included in aggregate %.

376 of 763 relevant lines covered (49.28%)

1.85 hits per line

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

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

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

12
export abstract class MongoDBToolBase extends ToolBase {
1✔
13
    protected category: ToolCategory = "mongodb";
38✔
14

15
    protected async ensureConnected(): Promise<NodeDriverServiceProvider> {
16
        if (!this.session.serviceProvider && this.config.connectionString) {
×
17
            await this.connectToMongoDB(this.config.connectionString);
×
18
        }
19

20
        if (!this.session.serviceProvider) {
×
21
            throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
×
22
        }
23

24
        return this.session.serviceProvider;
×
25
    }
26

27
    protected handleError(
28
        error: unknown,
29
        args: ToolArgs<typeof this.argsShape>
30
    ): Promise<CallToolResult> | CallToolResult {
31
        if (error instanceof MongoDBError && error.code === ErrorCodes.NotConnectedToMongoDB) {
×
32
            return {
×
33
                content: [
34
                    {
35
                        type: "text",
36
                        text: "You need to connect to a MongoDB instance before you can access its data.",
37
                    },
38
                    {
39
                        type: "text",
40
                        text: "Please use the 'connect' tool to connect to a MongoDB instance.",
41
                    },
42
                ],
43
                isError: true,
44
            };
45
        }
46

47
        return super.handleError(error, args);
×
48
    }
49

50
    protected async connectToMongoDB(connectionString: string): Promise<void> {
51
        const provider = await NodeDriverServiceProvider.connect(connectionString, {
×
52
            productDocsLink: "https://docs.mongodb.com/todo-mcp",
53
            productName: "MongoDB MCP",
54
            readConcern: {
55
                level: this.config.connectOptions.readConcern,
56
            },
57
            readPreference: this.config.connectOptions.readPreference,
58
            writeConcern: {
59
                w: this.config.connectOptions.writeConcern,
60
            },
61
            timeoutMS: this.config.connectOptions.timeoutMS,
62
        });
63

64
        this.session.serviceProvider = provider;
×
65
    }
66
}
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