Skip to content

Commit 1d7ea85

Browse files
committed
don't hardcode tool names
1 parent 84e706e commit 1d7ea85

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/tools/atlas/connect/connectCluster.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ function sleep(ms: number): Promise<void> {
1212
return new Promise((resolve) => setTimeout(resolve, ms));
1313
}
1414

15-
export const name = "atlas-connect-cluster";
16-
1715
export class ConnectClusterTool extends AtlasToolBase {
18-
public name = name;
16+
public name = "atlas-connect-cluster";
1917
protected description = "Connect to MongoDB Atlas cluster";
2018
public operationType: OperationType = "connect";
2119
protected argsShape = {

src/tools/mongodb/mongodbTool.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
55
import { ErrorCodes, MongoDBError } from "../../errors.js";
66
import logger, { LogId } from "../../logger.js";
77
import { Server } from "../../server.js";
8-
import { name as AtlasConnectToolName } from "../atlas/connect/connectCluster.js";
98

109
export const DbOperationArgs = {
1110
database: z.string().describe("Database name"),
@@ -49,12 +48,16 @@ export abstract class MongoDBToolBase extends ToolBase {
4948
if (error instanceof MongoDBError) {
5049
const connectTools = this.server?.tools
5150
.filter((t) => t.operationType === "connect")
52-
.sort((a, b) => a.category.localeCompare(b.category)) // Sort Altas tools before MongoDB tools
53-
.map((t) => t.name)
54-
.join(", ");
55-
const llmConnectHint = connectTools?.includes(AtlasConnectToolName)
56-
? `Note to LLM: prefer using the ${AtlasConnectToolName} 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.`
51+
.sort((a, b) => a.category.localeCompare(b.category)); // Sort Altas tools before MongoDB tools
52+
53+
// Find the first Atlas connect tool if available and suggest to the LLM to use it.
54+
// Note: if we ever have multiple Atlas connect tools, we may want to refine this logic to select the most appropriate one.
55+
const atlasConnectTool = connectTools?.find((t) => t.category === "atlas");
56+
const llmConnectHint = atlasConnectTool
57+
? `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.`
5758
: "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.";
59+
60+
const connectToolsNames = connectTools?.map((t) => t.name).join(", ");
5861
switch (error.code) {
5962
case ErrorCodes.NotConnectedToMongoDB:
6063
return {
@@ -66,7 +69,7 @@ export abstract class MongoDBToolBase extends ToolBase {
6669
{
6770
type: "text",
6871
text: connectTools
69-
? `Please use one of the following tools: ${connectTools} to connect to a MongoDB instance or update the MCP server configuration to include a connection string. ${llmConnectHint}`
72+
? `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}`
7073
: "There are no tools available to connect. Please update the configuration to include a connection string and restart the server.",
7174
},
7275
],
@@ -82,7 +85,7 @@ export abstract class MongoDBToolBase extends ToolBase {
8285
{
8386
type: "text",
8487
text: connectTools
85-
? `Alternatively, you can use one of the following tools: ${connectTools} to connect to a MongoDB instance. ${llmConnectHint}`
88+
? `Alternatively, you can use one of the following tools: ${connectToolsNames} to connect to a MongoDB instance. ${llmConnectHint}`
8689
: "Please update the configuration to use a valid connection string and restart the server.",
8790
},
8891
],

0 commit comments

Comments
 (0)