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

mongodb-js / mongodb-mcp-server / 19374881356

14 Nov 2025 07:07PM UTC coverage: 80.199% (+0.05%) from 80.151%
19374881356

Pull #716

github

web-flow
Merge 13978612d into d462ff9f0
Pull Request #716: chore: add connection metadata to telemetry

1382 of 1835 branches covered (75.31%)

Branch coverage included in aggregate %.

20 of 36 new or added lines in 7 files covered. (55.56%)

111 existing lines in 5 files now uncovered.

6508 of 8003 relevant lines covered (81.32%)

71.61 hits per line

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

51.92
/src/tools/atlasLocal/atlasLocalTool.ts
1
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2
import type { ToolArgs, ToolCategory } from "../tool.js";
3
import { ToolBase } from "../tool.js";
3✔
4
import type { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
5
import type { Client } from "@mongodb-js/atlas-local";
6
import { LogId } from "../../common/logger.js";
3✔
7
import type { ConnectionMetadata } from "../../telemetry/types.js";
8

9
export const AtlasLocalToolMetadataDeploymentIdKey = "deploymentId";
3✔
10

11
export abstract class AtlasLocalToolBase extends ToolBase {
3✔
12
    public category: ToolCategory = "atlas-local";
340✔
13

14
    protected verifyAllowed(): boolean {
3✔
15
        return this.session.atlasLocalClient !== undefined && super.verifyAllowed();
340✔
16
    }
340✔
17

18
    protected async execute(...args: Parameters<ToolCallback<typeof this.argsShape>>): Promise<CallToolResult> {
3✔
19
        const client = this.session.atlasLocalClient;
26✔
20

21
        // If the client is not found, throw an error
22
        // This should never happen:
23
        // - atlas-local tools are only added after the client is set
24
        //   this means that if we were unable to get the client, the tool will not be registered
25
        // - in case the tool was registered by accident
26
        //   verifyAllowed would still return false preventing the tool from being registered,
27
        //   preventing the tool from being executed
28
        if (!client) {
26!
29
            return {
×
30
                content: [
×
31
                    {
×
32
                        type: "text",
×
33
                        text: `Something went wrong on our end, this tool should have been disabled but it was not.
×
34
please log a ticket here: https://github.com/mongodb-js/mongodb-mcp-server/issues/new?template=bug_report.yml`,
35
                    },
×
36
                ],
×
37
                isError: true,
×
38
            };
×
39
        }
×
40

41
        return this.executeWithAtlasLocalClient(client, ...args);
26✔
42
    }
26✔
43

44
    private async lookupDeploymentId(client: Client, containerId: string): Promise<string | undefined> {
3✔
45
        try {
×
46
            // Lookup and return the deployment id for telemetry metadata.
47
            return await client.getDeploymentId(containerId);
×
48
        } catch (error) {
×
49
            this.session.logger.debug({
×
50
                id: LogId.telemetryMetadataError,
×
51
                context: "tool",
×
52
                message: `Error looking up deployment ID: ${String(error)}`,
×
53
            });
×
54

55
            return undefined;
×
56
        }
×
57
    }
×
58

59
    protected async lookupTelemetryMetadata(client: Client, containerId: string): Promise<{ [key: string]: unknown }> {
3✔
60
        if (!this.telemetry.isTelemetryEnabled()) {
17✔
61
            return {};
17✔
62
        }
17!
63

64
        const deploymentId = await this.lookupDeploymentId(client, containerId);
×
65
        if (deploymentId === undefined) {
×
66
            return {};
×
67
        }
×
68

69
        return {
×
70
            [AtlasLocalToolMetadataDeploymentIdKey]: deploymentId,
×
71
        };
×
72
    }
17✔
73

74
    protected abstract executeWithAtlasLocalClient(
75
        client: Client,
76
        ...args: Parameters<ToolCallback<typeof this.argsShape>>
77
    ): Promise<CallToolResult>;
78

79
    protected handleError(
3✔
80
        error: unknown,
3✔
81
        args: ToolArgs<typeof this.argsShape>
3✔
82
    ): Promise<CallToolResult> | CallToolResult {
3✔
83
        // Error Handling for expected Atlas Local errors go here
84
        const errorMessage = error instanceof Error ? error.message : String(error);
3!
85

86
        // Check if Docker daemon is not running
87
        if (
3✔
88
            errorMessage.includes("Cannot connect to the Docker daemon") ||
3✔
89
            errorMessage.includes("Is the docker daemon running") ||
3✔
90
            errorMessage.includes("connect ENOENT") ||
3✔
91
            errorMessage.includes("ECONNREFUSED")
3✔
92
        ) {
3!
93
            return {
×
94
                content: [
×
95
                    {
×
96
                        type: "text",
×
97
                        text: "Docker is not running. Please start Docker and try again. Atlas Local tools require Docker to be running.",
×
98
                    },
×
99
                ],
×
100
                isError: true,
×
101
            };
×
102
        }
×
103

104
        if (errorMessage.includes("No such container")) {
3✔
105
            const deploymentName =
2✔
106
                "deploymentName" in args ? (args.deploymentName as string) : "the specified deployment";
2!
107
            return {
2✔
108
                content: [
2✔
109
                    {
2✔
110
                        type: "text",
2✔
111
                        text: `The Atlas Local deployment "${deploymentName}" was not found. Please check the deployment name or use "atlas-local-list-deployments" to see available deployments.`,
2✔
112
                    },
2✔
113
                ],
2✔
114
                isError: true,
2✔
115
            };
2✔
116
        }
2✔
117

118
        // For other types of errors, use the default error handling from the base class
119
        return super.handleError(error, args);
1✔
120
    }
3✔
121

122
    protected resolveTelemetryMetadata(result: CallToolResult): ConnectionMetadata {
3✔
NEW
123
        const toolMetadata: ConnectionMetadata = {};
×
124

125
        // Atlas Local tools set the deployment ID in the result metadata for telemetry
126
        // If the deployment ID is set, we use it for telemetry
127
        const resultDeploymentId = result._meta?.[AtlasLocalToolMetadataDeploymentIdKey];
×
128
        if (resultDeploymentId !== undefined && typeof resultDeploymentId === "string") {
×
129
            toolMetadata.atlas_local_deployment_id = resultDeploymentId;
×
130
        }
×
131

132
        return toolMetadata;
×
133
    }
×
134
}
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