Skip to content

Commit 648115b

Browse files
committed
fix: tweak the connect tool arguments to instruct the model to prefer preconfigured connection string
1 parent 14d74e7 commit 648115b

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/server.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AtlasTools } from "./tools/atlas/tools.js";
55
import { MongoDbTools } from "./tools/mongodb/tools.js";
66
import logger, { initializeLogger } from "./logger.js";
77
import { mongoLogId } from "mongodb-log-writer";
8+
import config from "./config.js";
89

910
export class Server {
1011
public readonly session: Session;
@@ -19,6 +20,7 @@ export class Server {
1920
this.mcpServer.server.registerCapabilities({ logging: {} });
2021

2122
this.registerTools();
23+
this.registerResources();
2224

2325
await initializeLogger(this.mcpServer);
2426

@@ -37,4 +39,26 @@ export class Server {
3739
new tool(this.session).register(this.mcpServer);
3840
}
3941
}
42+
43+
private registerResources() {
44+
if (config.connectionString) {
45+
this.mcpServer.resource(
46+
"connection-string",
47+
"config://connection-string",
48+
{
49+
description: "Preconfigured connection string that will be used as a default in the `connect` tool",
50+
},
51+
(uri) => {
52+
return {
53+
contents: [
54+
{
55+
text: `Preconfigured connection string: ${config.connectionString}`,
56+
uri: uri.href,
57+
},
58+
],
59+
};
60+
}
61+
);
62+
}
63+
}
4064
}

src/tools/mongodb/metadata/connect.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,32 @@ export class ConnectTool extends MongoDBToolBase {
99
protected name = "connect";
1010
protected description = "Connect to a MongoDB instance";
1111
protected argsShape = {
12-
connectionStringOrClusterName: z
13-
.string()
12+
options: z
13+
.array(
14+
z
15+
.union([
16+
z.object({
17+
connectionString: z
18+
.string()
19+
.describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format)"),
20+
}),
21+
z.object({
22+
clusterName: z.string().describe("MongoDB cluster name"),
23+
}),
24+
])
25+
.optional()
26+
)
1427
.optional()
15-
.describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format) or cluster name"),
28+
.describe(
29+
"Options for connecting to MongoDB. If not provided, the connection string from the config://connection-string resource will be used. If the user hasn't specified Atlas cluster name or a connection string explicitly and the `config://connection-string` resource is present, always invoke this with no arguments."
30+
),
1631
};
1732

1833
protected operationType: OperationType = "metadata";
1934

20-
protected async execute({
21-
connectionStringOrClusterName,
22-
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
23-
connectionStringOrClusterName ??= config.connectionString;
24-
if (!connectionStringOrClusterName) {
35+
protected async execute({ options: optionsArr }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
36+
const options = optionsArr?.[0];
37+
if (!options && !config.connectionString) {
2538
return {
2639
content: [
2740
{ type: "text", text: "No connection details provided." },
@@ -36,11 +49,10 @@ export class ConnectTool extends MongoDBToolBase {
3649

3750
let connectionString: string;
3851

39-
if (
40-
connectionStringOrClusterName.startsWith("mongodb://") ||
41-
connectionStringOrClusterName.startsWith("mongodb+srv://")
42-
) {
43-
connectionString = connectionStringOrClusterName;
52+
if (!options) {
53+
connectionString = config.connectionString!;
54+
} else if ("connectionString" in options) {
55+
connectionString = options.connectionString;
4456
} else {
4557
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/19
4658
// We don't support connecting via cluster name since we'd need to obtain the user credentials

0 commit comments

Comments
 (0)