Skip to content

Commit d92adf1

Browse files
authored
Merge branch 'main' into telemetry
2 parents 2e33584 + 8da49a1 commit d92adf1

21 files changed

+967
-203
lines changed

src/tools/mongodb/create/createIndex.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,29 @@ export class CreateIndexTool extends MongoDBToolBase {
1010
protected argsShape = {
1111
...DbOperationArgs,
1212
keys: z.record(z.string(), z.custom<IndexDirection>()).describe("The index definition"),
13+
name: z.string().optional().describe("The name of the index"),
1314
};
1415

1516
protected operationType: OperationType = "create";
1617

17-
protected async execute({ database, collection, keys }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
18+
protected async execute({
19+
database,
20+
collection,
21+
keys,
22+
name,
23+
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
1824
const provider = await this.ensureConnected();
1925
const indexes = await provider.createIndexes(database, collection, [
2026
{
2127
key: keys,
28+
name,
2229
},
2330
]);
2431

2532
return {
2633
content: [
2734
{
28-
text: `Created the index \`${indexes[0]}\``,
35+
text: `Created the index "${indexes[0]}" on collection "${collection}" in database "${database}"`,
2936
type: "text",
3037
},
3138
],

src/tools/mongodb/create/insertMany.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class InsertManyTool extends MongoDBToolBase {
2727
return {
2828
content: [
2929
{
30-
text: `Inserted \`${result.insertedCount}\` documents into collection \`${collection}\``,
30+
text: `Inserted \`${result.insertedCount}\` document(s) into collection "${collection}"`,
3131
type: "text",
3232
},
3333
{

src/tools/mongodb/create/insertOne.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/tools/mongodb/delete/deleteMany.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class DeleteManyTool extends MongoDBToolBase {
2929
return {
3030
content: [
3131
{
32-
text: `Deleted \`${result.deletedCount}\` documents from collection \`${collection}\``,
32+
text: `Deleted \`${result.deletedCount}\` document(s) from collection "${collection}"`,
3333
type: "text",
3434
},
3535
],

src/tools/mongodb/delete/deleteOne.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/tools/mongodb/delete/dropCollection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class DropCollectionTool extends MongoDBToolBase {
1818
return {
1919
content: [
2020
{
21-
text: `${result ? "Successfully dropped" : "Failed to drop"} collection \`${collection}\` from database \`${database}\``,
21+
text: `${result ? "Successfully dropped" : "Failed to drop"} collection "${collection}" from database "${database}"`,
2222
type: "text",
2323
},
2424
],

src/tools/mongodb/delete/dropDatabase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class DropDatabaseTool extends MongoDBToolBase {
1717
return {
1818
content: [
1919
{
20-
text: `${result.ok ? "Successfully dropped" : "Failed to drop"} database \`${database}\``,
20+
text: `${result.ok ? "Successfully dropped" : "Failed to drop"} database "${database}"`,
2121
type: "text",
2222
},
2323
],

src/tools/mongodb/metadata/connect.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { z } from "zod";
22
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
33
import { MongoDBToolBase } from "../mongodbTool.js";
44
import { ToolArgs, OperationType } from "../../tool.js";
5-
import { ErrorCodes, MongoDBError } from "../../../errors.js";
65
import config from "../../../config.js";
76
import { MongoError as DriverError } from "mongodb";
87

@@ -37,25 +36,23 @@ export class ConnectTool extends MongoDBToolBase {
3736

3837
let connectionString: string;
3938

40-
if (typeof connectionStringOrClusterName === "string") {
41-
if (
42-
connectionStringOrClusterName.startsWith("mongodb://") ||
43-
connectionStringOrClusterName.startsWith("mongodb+srv://")
44-
) {
45-
connectionString = connectionStringOrClusterName;
46-
} else {
47-
// TODO:
48-
return {
49-
content: [
50-
{
51-
type: "text",
52-
text: `Connecting via cluster name not supported yet. Please provide a connection string.`,
53-
},
54-
],
55-
};
56-
}
39+
if (
40+
connectionStringOrClusterName.startsWith("mongodb://") ||
41+
connectionStringOrClusterName.startsWith("mongodb+srv://")
42+
) {
43+
connectionString = connectionStringOrClusterName;
5744
} else {
58-
throw new MongoDBError(ErrorCodes.InvalidParams, "Invalid connection options");
45+
// TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/19
46+
// We don't support connecting via cluster name since we'd need to obtain the user credentials
47+
// and fill in the connection string.
48+
return {
49+
content: [
50+
{
51+
type: "text",
52+
text: `Connecting via cluster name not supported yet. Please provide a connection string.`,
53+
},
54+
],
55+
};
5956
}
6057

6158
try {

src/tools/mongodb/mongodbTool.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ export abstract class MongoDBToolBase extends ToolBase {
1919
protected category: ToolCategory = "mongodb";
2020

2121
protected async ensureConnected(): Promise<NodeDriverServiceProvider> {
22-
const provider = this.session.serviceProvider;
23-
if (!provider && config.connectionString) {
22+
if (!this.session.serviceProvider && config.connectionString) {
2423
await this.connectToMongoDB(config.connectionString);
2524
}
2625

27-
if (!provider) {
26+
if (!this.session.serviceProvider) {
2827
throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
2928
}
3029

31-
return provider;
30+
return this.session.serviceProvider;
3231
}
3332

3433
protected handleError(error: unknown): Promise<CallToolResult> | CallToolResult {

src/tools/mongodb/tools.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import { CollectionIndexesTool } from "./read/collectionIndexes.js";
44
import { ListDatabasesTool } from "./metadata/listDatabases.js";
55
import { CreateIndexTool } from "./create/createIndex.js";
66
import { CollectionSchemaTool } from "./metadata/collectionSchema.js";
7-
import { InsertOneTool } from "./create/insertOne.js";
87
import { FindTool } from "./read/find.js";
98
import { InsertManyTool } from "./create/insertMany.js";
109
import { DeleteManyTool } from "./delete/deleteMany.js";
11-
import { DeleteOneTool } from "./delete/deleteOne.js";
1210
import { CollectionStorageSizeTool } from "./metadata/collectionStorageSize.js";
1311
import { CountTool } from "./read/count.js";
1412
import { DbStatsTool } from "./metadata/dbStats.js";
1513
import { AggregateTool } from "./read/aggregate.js";
16-
import { UpdateOneTool } from "./update/updateOne.js";
1714
import { UpdateManyTool } from "./update/updateMany.js";
1815
import { RenameCollectionTool } from "./update/renameCollection.js";
1916
import { DropDatabaseTool } from "./delete/dropDatabase.js";
@@ -28,16 +25,13 @@ export const MongoDbTools = [
2825
CollectionIndexesTool,
2926
CreateIndexTool,
3027
CollectionSchemaTool,
31-
InsertOneTool,
3228
FindTool,
3329
InsertManyTool,
3430
DeleteManyTool,
35-
DeleteOneTool,
3631
CollectionStorageSizeTool,
3732
CountTool,
3833
DbStatsTool,
3934
AggregateTool,
40-
UpdateOneTool,
4135
UpdateManyTool,
4236
RenameCollectionTool,
4337
DropDatabaseTool,

0 commit comments

Comments
 (0)