Skip to content

feat: add back the connect tool #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,6 @@ export class Server {
}

private async validateConfig(): Promise<void> {
const isAtlasConfigured = this.userConfig.apiClientId && this.userConfig.apiClientSecret;
const isMongoDbConfigured = this.userConfig.connectionString;
if (!isAtlasConfigured && !isMongoDbConfigured) {
console.error(
"Either Atlas Client Id or a MongoDB connection string must be configured - you can provide them as environment variables or as startup arguments. \n" +
"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" +
"Provide the MongoDB connection string as `MDB_MCP_CONNECTION_STRING` environment variable or as `--connectionString` startup argument."
);
throw new Error("Either Atlas Client Id or a MongoDB connection string must be configured");
}

if (this.userConfig.connectionString) {
try {
await this.session.connectToMongoDB(this.userConfig.connectionString, this.userConfig.connectOptions);
Expand Down
6 changes: 2 additions & 4 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export class Session extends EventEmitter<{
connectionString,
defaultAppName: `${packageInfo.mcpServerName} ${packageInfo.version}`,
});
const provider = await NodeDriverServiceProvider.connect(connectionString, {
productDocsLink: "https://docs.mongodb.com/todo-mcp",
this.serviceProvider = await NodeDriverServiceProvider.connect(connectionString, {
productDocsLink: "https://github.com/mongodb-js/mongodb-mcp-server/",
productName: "MongoDB MCP",
readConcern: {
level: connectOptions.readConcern,
Expand All @@ -116,7 +116,5 @@ export class Session extends EventEmitter<{
},
timeoutMS: connectOptions.timeoutMS,
});

this.serviceProvider = provider;
}
}
6 changes: 2 additions & 4 deletions src/tools/mongodb/tools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when the connect tool is reenabled
// import { ConnectTool } from "./metadata/connect.js";
import { ConnectTool } from "./metadata/connect.js";
import { ListCollectionsTool } from "./metadata/listCollections.js";
import { CollectionIndexesTool } from "./read/collectionIndexes.js";
import { ListDatabasesTool } from "./metadata/listDatabases.js";
Expand All @@ -21,8 +20,7 @@ import { CreateCollectionTool } from "./create/createCollection.js";
import { LogsTool } from "./metadata/logs.js";

export const MongoDbTools = [
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when the connect tool is reenabled
// ConnectTool,
ConnectTool,
ListCollectionsTool,
ListDatabasesTool,
CollectionIndexesTool,
Expand Down
8 changes: 2 additions & 6 deletions tests/integration/tools/mongodb/metadata/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { describeWithMongoDB } from "../mongodbHelpers.js";
import { getResponseContent, validateThrowsForInvalidArguments, validateToolMetadata } from "../../../helpers.js";
import { config } from "../../../../../src/config.js";

// These tests are temporarily skipped because the connect tool is disabled for the initial release.
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when the connect tool is reenabled
describeWithMongoDB(
"switchConnection tool",
(integration) => {
Expand Down Expand Up @@ -77,8 +75,7 @@ describeWithMongoDB(
(mdbIntegration) => ({
...config,
connectionString: mdbIntegration.connectionString(),
}),
describe.skip
})
);
describeWithMongoDB(
"Connect tool",
Expand Down Expand Up @@ -127,6 +124,5 @@ describeWithMongoDB(
});
});
},
() => config,
describe.skip
() => config
);
37 changes: 15 additions & 22 deletions tests/integration/tools/mongodb/mongodbHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,32 @@ interface MongoDBIntegrationTest {
export function describeWithMongoDB(
name: string,
fn: (integration: IntegrationTest & MongoDBIntegrationTest & { connectMcpClient: () => Promise<void> }) => void,
getUserConfig: (mdbIntegration: MongoDBIntegrationTest) => UserConfig = () => defaultTestConfig,
describeFn = describe
getUserConfig: (mdbIntegration: MongoDBIntegrationTest) => UserConfig = () => defaultTestConfig
) {
describeFn(name, () => {
describe(name, () => {
const mdbIntegration = setupMongoDBIntegrationTest();
const integration = setupIntegrationTest(() => ({
...getUserConfig(mdbIntegration),
connectionString: mdbIntegration.connectionString(),
}));

beforeEach(() => {
integration.mcpServer().userConfig.connectionString = mdbIntegration.connectionString();
});

fn({
...integration,
...mdbIntegration,
connectMcpClient: async () => {
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when
// the connect tool is reenabled
// await integration.mcpClient().callTool({
// name: "connect",
// arguments: { connectionString: mdbIntegration.connectionString() },
// });
const { tools } = await integration.mcpClient().listTools();
if (tools.find((tool) => tool.name === "connect")) {
await integration.mcpClient().callTool({
name: "connect",
arguments: { connectionString: mdbIntegration.connectionString() },
});
}
},
});
});
}

export function setupMongoDBIntegrationTest(): MongoDBIntegrationTest {
let mongoCluster: // TODO: Fix this type once mongodb-runner is updated.
| {
connectionString: string;
close: () => Promise<void>;
}
| undefined;
let mongoCluster: MongoCluster | undefined;
let mongoClient: MongoClient | undefined;
let randomDbName: string;

Expand Down Expand Up @@ -139,12 +129,15 @@ export function validateAutoConnectBehavior(
},
beforeEachImpl?: () => Promise<void>
): void {
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/141 - reenable when the connect tool is reenabled
describe.skip("when not connected", () => {
describe("when not connected", () => {
if (beforeEachImpl) {
beforeEach(() => beforeEachImpl());
}

afterEach(() => {
integration.mcpServer().userConfig.connectionString = undefined;
});

it("connects automatically if connection string is configured", async () => {
integration.mcpServer().userConfig.connectionString = integration.connectionString();

Expand Down
Loading