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

mongodb-js / mongodb-mcp-server / 14614840974

23 Apr 2025 09:33AM UTC coverage: 76.675% (+23.5%) from 53.208%
14614840974

Pull #83

github

fmenezes
fix: styles
Pull Request #83: chore: add atlas tests

81 of 152 branches covered (53.29%)

Branch coverage included in aggregate %.

28 of 39 new or added lines in 6 files covered. (71.79%)

4 existing lines in 3 files now uncovered.

560 of 684 relevant lines covered (81.87%)

27.03 hits per line

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

64.71
/src/tools/tool.ts
1
import { McpServer, ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
2
import { z, ZodNever, ZodRawShape } from "zod";
3
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
4
import { Session } from "../session.js";
5
import logger from "../logger.js";
16✔
6
import { mongoLogId } from "mongodb-log-writer";
16✔
7
import config from "../config.js";
16✔
8

9
export type ToolArgs<Args extends ZodRawShape> = z.objectOutputType<Args, ZodNever>;
10

11
export type OperationType = "metadata" | "read" | "create" | "delete" | "update" | "cluster";
12
export type ToolCategory = "mongodb" | "atlas";
13

14
export abstract class ToolBase {
16✔
15
    protected abstract name: string;
16

17
    protected abstract category: ToolCategory;
18

19
    protected abstract operationType: OperationType;
20

21
    protected abstract description: string;
22

23
    protected abstract argsShape: ZodRawShape;
24

25
    protected abstract execute(...args: Parameters<ToolCallback<typeof this.argsShape>>): Promise<CallToolResult>;
26

27
    protected constructor(protected session: Session) {}
464✔
28

29
    public register(server: McpServer): void {
30
        if (!this.verifyAllowed()) {
464!
31
            return;
×
32
        }
33

34
        const callback: ToolCallback<typeof this.argsShape> = async (...args) => {
464✔
35
            try {
151✔
36
                // TODO: add telemetry here
37
                logger.debug(
151✔
38
                    mongoLogId(1_000_006),
39
                    "tool",
40
                    `Executing ${this.name} with args: ${JSON.stringify(args)}`
41
                );
42

43
                return await this.execute(...args);
151✔
44
            } catch (error: unknown) {
45
                logger.error(mongoLogId(1_000_000), "tool", `Error executing ${this.name}: ${error as string}`);
12✔
46

47
                return await this.handleError(error);
12✔
48
            }
49
        };
50

51
        server.tool(this.name, this.description, this.argsShape, callback);
464✔
52
    }
53

54
    // Checks if a tool is allowed to run based on the config
55
    private verifyAllowed(): boolean {
56
        let errorClarification: string | undefined;
57
        if (config.disabledTools.includes(this.category)) {
464!
58
            errorClarification = `its category, \`${this.category}\`,`;
×
59
        } else if (config.disabledTools.includes(this.operationType)) {
464!
60
            errorClarification = `its operation type, \`${this.operationType}\`,`;
×
61
        } else if (config.disabledTools.includes(this.name)) {
464!
62
            errorClarification = `it`;
×
63
        }
64

65
        if (errorClarification) {
464!
66
            logger.debug(
×
67
                mongoLogId(1_000_010),
68
                "tool",
69
                `Prevented registration of ${this.name} because ${errorClarification} is disabled in the config`
70
            );
71

72
            return false;
×
73
        }
74

75
        return true;
464✔
76
    }
77

78
    // This method is intended to be overridden by subclasses to handle errors
79
    protected handleError(error: unknown): Promise<CallToolResult> | CallToolResult {
80
        return {
3✔
81
            content: [
82
                {
83
                    type: "text",
84
                    text: `Error running ${this.name}: ${error instanceof Error ? error.message : String(error)}`,
3!
85
                },
86
            ],
87
            isError: true,
88
        };
89
    }
90
}
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