Skip to content

Commit 74fe9fe

Browse files
committed
Improves llmConntectHint
1 parent 6bf04ce commit 74fe9fe

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/common/connectionErrorHandler.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,29 @@ export const connectionErrorHandler: ConnectionErrorHandler = (error, { availabl
1717
.filter((t) => t.operationType === "connect")
1818
.sort((a, b) => a.category.localeCompare(b.category)); // Sort Atlas tools before MongoDB tools
1919

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.
20+
// Find what Atlas connect tools are available and suggest when the LLM should to use each. If no Atlas tools are found, return a suggestion for the MongoDB connect tool.
2221
const atlasConnectTool = connectTools?.find((t) => t.category === "atlas");
2322
const atlasLocalConnectTool = connectTools?.find((t) => t.category === "atlas-local");
2423

25-
let llmConnectHint: string;
24+
const llmConnectHint = ((): string => {
25+
const hints: string[] = [];
2626

27-
if (atlasConnectTool) {
28-
llmConnectHint = `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.`;
29-
} else if (atlasLocalConnectTool) {
30-
llmConnectHint = `Note to LLM: prefer using the "${atlasLocalConnectTool.name}" tool for connecting to local MongoDB deployments. Ask the user to specify a deployment name or use "atlas-local-list-deployments" to show available local deployments. Do not invent deployment names unless the user has explicitly specified them. If they've previously connected to a local MongoDB deployment using MCP, you can ask them if they want to reconnect using the same deployment.`;
31-
} else {
32-
llmConnectHint =
33-
"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.";
34-
}
27+
if (atlasConnectTool) {
28+
hints.push(
29+
`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.`
30+
);
31+
}
32+
33+
if (atlasLocalConnectTool) {
34+
hints.push(
35+
`Note to LLM: For MongoDB Atlas Local deployments, ask the user to either provide a connection string, specify a deployment name, or use "atlas-local-list-deployments" to show available local deployments. If a deployment name is provided, prefer using the "${atlasLocalConnectTool.name}" tool. If a connection string is provided, prefer using the "connect" tool. Do not invent deployment names or connection strings unless the user has explicitly specified them. If they've previously connected to a MongoDB Atlas Local deployment using MCP, you can ask them if they want to reconnect using the same deployment.`
36+
);
37+
}
38+
39+
return hints.length > 0
40+
? hints.join("\n")
41+
: "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.";
42+
})();
3543

3644
const connectToolsNames = connectTools?.map((t) => `"${t.name}"`).join(", ");
3745
const additionalPromptForConnectivity: { type: "text"; text: string }[] = [];

0 commit comments

Comments
 (0)