Skip to content

Commit 3af84ca

Browse files
nirinchevCopilot
andauthored
feat: add back the connect tool (#210)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a803f48 commit 3af84ca

File tree

5 files changed

+21
-47
lines changed

5 files changed

+21
-47
lines changed

src/server.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,6 @@ export class Server {
174174
}
175175

176176
private async validateConfig(): Promise<void> {
177-
const isAtlasConfigured = this.userConfig.apiClientId && this.userConfig.apiClientSecret;
178-
const isMongoDbConfigured = this.userConfig.connectionString;
179-
if (!isAtlasConfigured && !isMongoDbConfigured) {
180-
console.error(
181-
"Either Atlas Client Id or a MongoDB connection string must be configured - you can provide them as environment variables or as startup arguments. \n" +
182-
"Provide the Atlas credentials as `MDB_MCP_API_CLIENT_ID` and `MDB_MCP_API_CLIENT_SECRET` environment variables or as `--apiClientId` and `--apiClientSecret` startup arguments. \n" +
183-
"Provide the MongoDB connection string as `MDB_MCP_CONNECTION_STRING` environment variable or as `--connectionString` startup argument."
184-
);
185-
throw new Error("Either Atlas Client Id or a MongoDB connection string must be configured");
186-
}
187-
188177
if (this.userConfig.connectionString) {
189178
try {
190179
await this.session.connectToMongoDB(this.userConfig.connectionString, this.userConfig.connectOptions);

src/session.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export class Session extends EventEmitter<{
104104
connectionString,
105105
defaultAppName: `${packageInfo.mcpServerName} ${packageInfo.version}`,
106106
});
107-
const provider = await NodeDriverServiceProvider.connect(connectionString, {
108-
productDocsLink: "https://docs.mongodb.com/todo-mcp",
107+
this.serviceProvider = await NodeDriverServiceProvider.connect(connectionString, {
108+
productDocsLink: "https://github.com/mongodb-js/mongodb-mcp-server/",
109109
productName: "MongoDB MCP",
110110
readConcern: {
111111
level: connectOptions.readConcern,
@@ -116,7 +116,5 @@ export class Session extends EventEmitter<{
116116
},
117117
timeoutMS: connectOptions.timeoutMS,
118118
});
119-
120-
this.serviceProvider = provider;
121119
}
122120
}

src/tools/mongodb/tools.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when the connect tool is reenabled
2-
// import { ConnectTool } from "./metadata/connect.js";
1+
import { ConnectTool } from "./metadata/connect.js";
32
import { ListCollectionsTool } from "./metadata/listCollections.js";
43
import { CollectionIndexesTool } from "./read/collectionIndexes.js";
54
import { ListDatabasesTool } from "./metadata/listDatabases.js";
@@ -21,8 +20,7 @@ import { CreateCollectionTool } from "./create/createCollection.js";
2120
import { LogsTool } from "./metadata/logs.js";
2221

2322
export const MongoDbTools = [
24-
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when the connect tool is reenabled
25-
// ConnectTool,
23+
ConnectTool,
2624
ListCollectionsTool,
2725
ListDatabasesTool,
2826
CollectionIndexesTool,

tests/integration/tools/mongodb/metadata/connect.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { describeWithMongoDB } from "../mongodbHelpers.js";
22
import { getResponseContent, validateThrowsForInvalidArguments, validateToolMetadata } from "../../../helpers.js";
33
import { config } from "../../../../../src/config.js";
44

5-
// These tests are temporarily skipped because the connect tool is disabled for the initial release.
6-
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when the connect tool is reenabled
75
describeWithMongoDB(
86
"switchConnection tool",
97
(integration) => {
@@ -77,8 +75,7 @@ describeWithMongoDB(
7775
(mdbIntegration) => ({
7876
...config,
7977
connectionString: mdbIntegration.connectionString(),
80-
}),
81-
describe.skip
78+
})
8279
);
8380
describeWithMongoDB(
8481
"Connect tool",
@@ -127,6 +124,5 @@ describeWithMongoDB(
127124
});
128125
});
129126
},
130-
() => config,
131-
describe.skip
127+
() => config
132128
);

tests/integration/tools/mongodb/mongodbHelpers.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,32 @@ interface MongoDBIntegrationTest {
1717
export function describeWithMongoDB(
1818
name: string,
1919
fn: (integration: IntegrationTest & MongoDBIntegrationTest & { connectMcpClient: () => Promise<void> }) => void,
20-
getUserConfig: (mdbIntegration: MongoDBIntegrationTest) => UserConfig = () => defaultTestConfig,
21-
describeFn = describe
20+
getUserConfig: (mdbIntegration: MongoDBIntegrationTest) => UserConfig = () => defaultTestConfig
2221
) {
23-
describeFn(name, () => {
22+
describe(name, () => {
2423
const mdbIntegration = setupMongoDBIntegrationTest();
2524
const integration = setupIntegrationTest(() => ({
2625
...getUserConfig(mdbIntegration),
27-
connectionString: mdbIntegration.connectionString(),
2826
}));
2927

30-
beforeEach(() => {
31-
integration.mcpServer().userConfig.connectionString = mdbIntegration.connectionString();
32-
});
33-
3428
fn({
3529
...integration,
3630
...mdbIntegration,
3731
connectMcpClient: async () => {
38-
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when
39-
// the connect tool is reenabled
40-
// await integration.mcpClient().callTool({
41-
// name: "connect",
42-
// arguments: { connectionString: mdbIntegration.connectionString() },
43-
// });
32+
const { tools } = await integration.mcpClient().listTools();
33+
if (tools.find((tool) => tool.name === "connect")) {
34+
await integration.mcpClient().callTool({
35+
name: "connect",
36+
arguments: { connectionString: mdbIntegration.connectionString() },
37+
});
38+
}
4439
},
4540
});
4641
});
4742
}
4843

4944
export function setupMongoDBIntegrationTest(): MongoDBIntegrationTest {
50-
let mongoCluster: // TODO: Fix this type once mongodb-runner is updated.
51-
| {
52-
connectionString: string;
53-
close: () => Promise<void>;
54-
}
55-
| undefined;
45+
let mongoCluster: MongoCluster | undefined;
5646
let mongoClient: MongoClient | undefined;
5747
let randomDbName: string;
5848

@@ -139,12 +129,15 @@ export function validateAutoConnectBehavior(
139129
},
140130
beforeEachImpl?: () => Promise<void>
141131
): void {
142-
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when the connect tool is reenabled
143-
describe.skip("when not connected", () => {
132+
describe("when not connected", () => {
144133
if (beforeEachImpl) {
145134
beforeEach(() => beforeEachImpl());
146135
}
147136

137+
afterEach(() => {
138+
integration.mcpServer().userConfig.connectionString = undefined;
139+
});
140+
148141
it("connects automatically if connection string is configured", async () => {
149142
integration.mcpServer().userConfig.connectionString = integration.connectionString();
150143

0 commit comments

Comments
 (0)