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

mongodb-js / mongodb-mcp-server / 17429932259

03 Sep 2025 09:59AM UTC coverage: 81.125% (+0.2%) from 80.965%
17429932259

Pull #502

github

web-flow
Merge ed6adc6d6 into 47c0a09e0
Pull Request #502: chore: extend library interfaces to allow injecting a custom connection error handler MCP-132

901 of 1199 branches covered (75.15%)

Branch coverage included in aggregate %.

92 of 106 new or added lines in 9 files covered. (86.79%)

2 existing lines in 2 files now uncovered.

4579 of 5556 relevant lines covered (82.42%)

44.39 hits per line

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

89.29
/src/common/connectionErrorHandler.ts
1
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2
import { ErrorCodes, type MongoDBError } from "./errors.js";
2✔
3
import type { AnyConnectionState } from "./connectionManager.js";
4
import type { ToolBase } from "../tools/tool.js";
5

6
export type ConnectionErrorHandler = (
7
    error: MongoDBError<ErrorCodes.NotConnectedToMongoDB | ErrorCodes.MisconfiguredConnectionString>,
8
    additionalContext: ConnectionErrorHandlerContext
9
) => ConnectionErrorUnhandled | ConnectionErrorHandled;
10

11
export type ConnectionErrorHandlerContext = { availableTools: ToolBase[]; connectionState: AnyConnectionState };
12
export type ConnectionErrorUnhandled = { errorHandled: false };
13
export type ConnectionErrorHandled = { errorHandled: true; result: CallToolResult };
14

15
export const connectionErrorHandler: ConnectionErrorHandler = (error, { availableTools, connectionState }) => {
2✔
16
    const connectTools = availableTools
26✔
17
        .filter((t) => t.operationType === "connect")
26✔
18
        .sort((a, b) => a.category.localeCompare(b.category)); // Sort Atlas tools before MongoDB tools
26✔
19

20
    // Find the first Atlas connect tool if available and suggest to the LLM to use it.
21
    // Note: if we ever have multiple Atlas connect tools, we may want to refine this logic to select the most appropriate one.
22
    const atlasConnectTool = connectTools?.find((t) => t.category === "atlas");
26✔
23
    const llmConnectHint = atlasConnectTool
26!
24
        ? `Note to LLM: prefer using the "${atlasConnectTool.name}" tool to connect to an Atlas cluster over using a connection string. Make sure to ask the user to specify a cluster name they want to connect to or ask them if they want to use the "list-clusters" tool to list all their clusters. Do not invent cluster names or connection strings unless the user has explicitly specified them. If they've previously connected to MongoDB using MCP, you can ask them if they want to reconnect using the same cluster/connection.`
1!
25
        : "Note to LLM: do not invent connection strings and explicitly ask the user to provide one. If they have previously connected to MongoDB using MCP, you can ask them if they want to reconnect using the same connection string.";
25✔
26

27
    const connectToolsNames = connectTools?.map((t) => `"${t.name}"`).join(", ");
26✔
28
    const additionalPromptForConnectivity: { type: "text"; text: string }[] = [];
26✔
29

30
    if (connectionState.tag === "connecting" && connectionState.oidcConnectionType) {
26✔
31
        additionalPromptForConnectivity.push({
1✔
32
            type: "text",
1✔
33
            text: `The user needs to finish their OIDC connection by opening '${connectionState.oidcLoginUrl}' in the browser and use the following user code: '${connectionState.oidcUserCode}'`,
1✔
34
        });
1✔
35
    } else {
26✔
36
        additionalPromptForConnectivity.push({
25✔
37
            type: "text",
25✔
38
            text: connectToolsNames
25✔
39
                ? `Please use one of the following tools: ${connectToolsNames} to connect to a MongoDB instance or update the MCP server configuration to include a connection string. ${llmConnectHint}`
24!
40
                : "There are no tools available to connect. Please update the configuration to include a connection string and restart the server.",
1✔
41
        });
25✔
42
    }
25✔
43

44
    switch (error.code) {
26✔
45
        case ErrorCodes.NotConnectedToMongoDB:
26✔
46
            return {
23✔
47
                errorHandled: true,
23✔
48
                result: {
23✔
49
                    content: [
23✔
50
                        {
23✔
51
                            type: "text",
23✔
52
                            text: "You need to connect to a MongoDB instance before you can access its data.",
23✔
53
                        },
23✔
54
                        ...additionalPromptForConnectivity,
23✔
55
                    ],
23✔
56
                    isError: true,
23✔
57
                },
23✔
58
            };
23✔
59
        case ErrorCodes.MisconfiguredConnectionString:
26!
60
            return {
3✔
61
                errorHandled: true,
3✔
62
                result: {
3✔
63
                    content: [
3✔
64
                        {
3✔
65
                            type: "text",
3✔
66
                            text: "The configured connection string is not valid. Please check the connection string and confirm it points to a valid MongoDB instance.",
3✔
67
                        },
3✔
68
                        {
3✔
69
                            type: "text",
3✔
70
                            text: connectTools
3✔
71
                                ? `Alternatively, you can use one of the following tools: ${connectToolsNames} to connect to a MongoDB instance. ${llmConnectHint}`
3!
NEW
72
                                : "Please update the configuration to use a valid connection string and restart the server.",
×
73
                        },
3✔
74
                    ],
3✔
75
                    isError: true,
3✔
76
                },
3✔
77
            };
3✔
78

79
        default:
26!
NEW
80
            return { errorHandled: false };
×
81
    }
26✔
82
};
26✔
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