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

mongodb-js / mongodb-mcp-server / 19166075826

07 Nov 2025 10:49AM UTC coverage: 80.026% (-0.1%) from 80.151%
19166075826

Pull #716

github

web-flow
Merge b5964dd5c into 454e81617
Pull Request #716: chore: add connection metadata to telemetry

1363 of 1802 branches covered (75.64%)

Branch coverage included in aggregate %.

21 of 51 new or added lines in 7 files covered. (41.18%)

8 existing lines in 1 file now uncovered.

6490 of 8011 relevant lines covered (81.01%)

70.28 hits per line

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

43.33
/src/tools/atlas/atlasTool.ts
1
import type { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
2
import type { AtlasMetadata } from "../../telemetry/types.js";
3
import { ToolBase, type ToolArgs, type ToolCategory } from "../tool.js";
3✔
4
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
5
import { LogId } from "../../common/logger.js";
3✔
6
import { z } from "zod";
3✔
7
import { ApiClientError } from "../../common/atlas/apiClientError.js";
3✔
8

9
export abstract class AtlasToolBase extends ToolBase {
3✔
10
    public category: ToolCategory = "atlas";
1,105✔
11

12
    protected verifyAllowed(): boolean {
3✔
13
        if (!this.config.apiClientId || !this.config.apiClientSecret) {
1,105✔
14
            return false;
936✔
15
        }
936!
16
        return super.verifyAllowed();
169✔
17
    }
1,105✔
18

19
    protected handleError(
3✔
20
        error: unknown,
3✔
21
        args: ToolArgs<typeof this.argsShape>
3✔
22
    ): Promise<CallToolResult> | CallToolResult {
3✔
23
        if (error instanceof ApiClientError) {
3✔
24
            const statusCode = error.response.status;
3✔
25

26
            if (statusCode === 401) {
3✔
27
                return {
3✔
28
                    content: [
3✔
29
                        {
3✔
30
                            type: "text",
3✔
31
                            text: `Unable to authenticate with MongoDB Atlas, API error: ${error.message}
3✔
32

33
Hint: Your API credentials may be invalid, expired or lack permissions.
34
Please check your Atlas API credentials and ensure they have the appropriate permissions.
35
For more information on setting up API keys, visit: https://www.mongodb.com/docs/atlas/configure-api-access/`,
36
                        },
3✔
37
                    ],
3✔
38
                    isError: true,
3✔
39
                };
3✔
40
            }
3!
41

42
            if (statusCode === 402) {
×
43
                return {
×
44
                    content: [
×
45
                        {
×
46
                            type: "text",
×
47
                            text: `Received a Payment Required API Error: ${error.message}
×
48

49
Payment setup is required to perform this action in MongoDB Atlas.
50
Please ensure that your payment method for your organization has been set up and is active.
51
For more information on setting up payment, visit: https://www.mongodb.com/docs/atlas/billing/`,
52
                        },
×
53
                    ],
×
54
                };
×
55
            }
×
56

57
            if (statusCode === 403) {
×
58
                return {
×
59
                    content: [
×
60
                        {
×
61
                            type: "text",
×
62
                            text: `Received a Forbidden API Error: ${error.message}
×
63

64
You don't have sufficient permissions to perform this action in MongoDB Atlas.
65
Please ensure your API key has the necessary roles assigned.
66
For more information on Atlas API access roles, visit: https://www.mongodb.com/docs/atlas/api/service-accounts-overview/`,
67
                        },
×
68
                    ],
×
69
                    isError: true,
×
70
                };
×
71
            }
×
72
        }
3!
73

74
        // For other types of errors, use the default error handling from the base class
75
        return super.handleError(error, args);
×
76
    }
3✔
77

78
    /**
79
     *
80
     * Resolves the tool metadata from the arguments passed to the tool
81
     *
82
     * @param args - The arguments passed to the tool
83
     * @returns The tool metadata
84
     */
85
    protected resolveTelemetryMetadata(
3✔
86
        result: CallToolResult,
×
87
        ...args: Parameters<ToolCallback<typeof this.argsShape>>
×
NEW
88
    ): AtlasMetadata {
×
NEW
89
        const toolMetadata: AtlasMetadata = {};
×
90
        if (!args.length) {
×
91
            return toolMetadata;
×
92
        }
×
93

94
        // Create a typed parser for the exact shape we expect
95
        const argsShape = z.object(this.argsShape);
×
96
        const parsedResult = argsShape.safeParse(args[0]);
×
97

98
        if (!parsedResult.success) {
×
99
            this.session.logger.debug({
×
100
                id: LogId.telemetryMetadataError,
×
101
                context: "tool",
×
102
                message: `Error parsing tool arguments: ${parsedResult.error.message}`,
×
103
            });
×
104
            return toolMetadata;
×
105
        }
×
106

107
        const data = parsedResult.data;
×
108

109
        // Extract projectId using type guard
110
        if ("projectId" in data && typeof data.projectId === "string" && data.projectId.trim() !== "") {
×
111
            toolMetadata.project_id = data.projectId;
×
112
        }
×
113

114
        // Extract orgId using type guard
115
        if ("orgId" in data && typeof data.orgId === "string" && data.orgId.trim() !== "") {
×
116
            toolMetadata.org_id = data.orgId;
×
117
        }
×
118
        return toolMetadata;
×
119
    }
×
120
}
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