Skip to content

Commit 53d4599

Browse files
committed
Add more data access tools
1 parent 4987a6b commit 53d4599

22 files changed

+540
-18
lines changed

src/tools/mongodb/collectionIndexes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2-
import { DbOperationArgs, MongoDBToolBase } from "./mongodbTool.js";
2+
import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "./mongodbTool.js";
33
import { ToolArgs } from "../tool.js";
44

55
export class CollectionIndexesTool extends MongoDBToolBase {
66
protected name = "collection-indexes";
77
protected description = "Describe the indexes for a collection";
88
protected argsShape = DbOperationArgs;
9+
protected operationType: DbOperationType = "read";
910

1011
protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
1112
const provider = this.ensureConnected();

src/tools/mongodb/connect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { z } from "zod";
22
import { CallToolResult, McpError } from "@modelcontextprotocol/sdk/types.js";
33
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
4-
import { MongoDBToolBase } from "./mongodbTool.js";
4+
import { DbOperationType, MongoDBToolBase } from "./mongodbTool.js";
55
import { ToolArgs } from "../tool";
66
import { ErrorCodes } from "../../errors.js";
77

@@ -15,6 +15,8 @@ export class ConnectTool extends MongoDBToolBase {
1515
.describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format) or cluster name"),
1616
};
1717

18+
protected operationType: DbOperationType = "metadata";
19+
1820
protected async execute({
1921
connectionStringOrClusterName,
2022
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { z } from "zod";
2+
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3+
import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "../mongodbTool.js";
4+
import { ToolArgs } from "../../tool.js";
5+
6+
export class InsertManyTool extends MongoDBToolBase {
7+
protected name = "insert-many";
8+
protected description = "Insert an array of documents into a MongoDB collection";
9+
protected argsShape = {
10+
...DbOperationArgs,
11+
documents: z
12+
.array(z.object({}).passthrough().describe("An individual MongoDB document"))
13+
.describe(
14+
"The array of documents to insert, matching the syntax of the document argument of db.collection.insertMany()"
15+
),
16+
};
17+
protected operationType: DbOperationType = "create";
18+
19+
protected async execute({
20+
database,
21+
collection,
22+
documents,
23+
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
24+
const provider = this.ensureConnected();
25+
const result = await provider.insertMany(database, collection, documents);
26+
27+
return {
28+
content: [
29+
{
30+
text: `Inserted \`${result.insertedCount}\` documents into collection \`${collection}\``,
31+
type: "text",
32+
},
33+
{
34+
text: `Inserted IDs: ${Object.values(result.insertedIds).join(", ")}`,
35+
type: "text",
36+
},
37+
],
38+
};
39+
}
40+
}

src/tools/mongodb/insert/insertOne.ts renamed to src/tools/mongodb/create/insertOne.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { z } from "zod";
22
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3-
import { MongoDBToolBase } from "../mongodbTool.js";
3+
import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "../mongodbTool.js";
44
import { ToolArgs } from "../../tool.js";
55

66
export class InsertOneTool extends MongoDBToolBase {
77
protected name = "insert-one";
88
protected description = "Insert a document into a MongoDB collection";
99
protected argsShape = {
10-
collection: z.string().describe("Collection name"),
11-
database: z.string().describe("Database name"),
10+
...DbOperationArgs,
1211
document: z
1312
.object({})
1413
.passthrough()
@@ -17,6 +16,8 @@ export class InsertOneTool extends MongoDBToolBase {
1716
),
1817
};
1918

19+
protected operationType: DbOperationType = "create";
20+
2021
protected async execute({
2122
database,
2223
collection,

src/tools/mongodb/createIndex.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from "zod";
22
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3-
import { DbOperationArgs, MongoDBToolBase } from "./mongodbTool.js";
3+
import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "./mongodbTool.js";
44
import { ToolArgs } from "../tool.js";
55
import { IndexDirection } from "mongodb";
66

@@ -12,6 +12,8 @@ export class CreateIndexTool extends MongoDBToolBase {
1212
keys: z.record(z.string(), z.custom<IndexDirection>()).describe("The index definition"),
1313
};
1414

15+
protected operationType: DbOperationType = "create";
16+
1517
protected async execute({ database, collection, keys }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
1618
const provider = this.ensureConnected();
1719
const indexes = await provider.createIndexes(database, collection, [
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { z } from "zod";
2+
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3+
import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "../mongodbTool.js";
4+
import { ToolArgs } from "../../tool.js";
5+
6+
export class DeleteManyTool extends MongoDBToolBase {
7+
protected name = "delete-many";
8+
protected description = "Removes all documents that match the filter from a MongoDB collection";
9+
protected argsShape = {
10+
...DbOperationArgs,
11+
filter: z
12+
.object({})
13+
.passthrough()
14+
.optional()
15+
.describe(
16+
"The query filter, specifying the deletion criteria. Matches the syntax of the filter argument of db.collection.deleteMany()"
17+
),
18+
};
19+
protected operationType: DbOperationType = "delete";
20+
21+
protected async execute({
22+
database,
23+
collection,
24+
filter,
25+
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
26+
const provider = this.ensureConnected();
27+
const result = await provider.deleteMany(database, collection, filter);
28+
29+
return {
30+
content: [
31+
{
32+
text: `Deleted \`${result.deletedCount}\` documents from collection \`${collection}\``,
33+
type: "text",
34+
},
35+
],
36+
};
37+
}
38+
}

src/tools/mongodb/delete/deleteOne.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { z } from "zod";
2+
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3+
import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "../mongodbTool.js";
4+
import { ToolArgs } from "../../tool.js";
5+
6+
export class DeleteOneTool extends MongoDBToolBase {
7+
protected name = "delete-one";
8+
protected description = "Removes a single document that match the filter from a MongoDB collection";
9+
protected argsShape = {
10+
...DbOperationArgs,
11+
filter: z
12+
.object({})
13+
.passthrough()
14+
.optional()
15+
.describe(
16+
"The query filter, specifying the deletion criteria. Matches the syntax of the filter argument of db.collection.deleteMany()"
17+
),
18+
};
19+
protected operationType: DbOperationType = "delete";
20+
21+
protected async execute({
22+
database,
23+
collection,
24+
filter,
25+
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
26+
const provider = this.ensureConnected();
27+
const result = await provider.deleteOne(database, collection, filter);
28+
29+
return {
30+
content: [
31+
{
32+
text: `Deleted \`${result.deletedCount}\` documents from collection \`${collection}\``,
33+
type: "text",
34+
},
35+
],
36+
};
37+
}
38+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2+
import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "../mongodbTool.js";
3+
import { ToolArgs } from "../../tool.js";
4+
5+
export class DropCollectionTool extends MongoDBToolBase {
6+
protected name = "drop-collection";
7+
protected description =
8+
"Removes a collection or view from the database. The method also removes any indexes associated with the dropped collection.";
9+
protected argsShape = {
10+
...DbOperationArgs,
11+
};
12+
protected operationType: DbOperationType = "delete";
13+
14+
protected async execute({ database, collection }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
15+
const provider = this.ensureConnected();
16+
const result = await provider.dropCollection(database, collection);
17+
18+
return {
19+
content: [
20+
{
21+
text: `${result ? "Successfully dropped" : "Failed to drop"} collection \`${collection}\` from database \`${database}\``,
22+
type: "text",
23+
},
24+
],
25+
};
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2+
import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "../mongodbTool.js";
3+
import { ToolArgs } from "../../tool.js";
4+
5+
export class DropDatabaseTool extends MongoDBToolBase {
6+
protected name = "drop-database";
7+
protected description = "Removes the specified database, deleting the associated data files";
8+
protected argsShape = {
9+
database: DbOperationArgs.database,
10+
};
11+
protected operationType: DbOperationType = "delete";
12+
13+
protected async execute({ database }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
14+
const provider = this.ensureConnected();
15+
const result = await provider.dropDatabase(database);
16+
17+
return {
18+
content: [
19+
{
20+
text: `${result.ok ? "Successfully dropped" : "Failed to drop"} database \`${database}\``,
21+
type: "text",
22+
},
23+
],
24+
};
25+
}
26+
}

src/tools/mongodb/index.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
22
import { State } from "../../state.js";
33
import { ConnectTool } from "./connect.js";
4-
import { ListCollectionsTool } from "./listCollections.js";
4+
import { ListCollectionsTool } from "./metadata/listCollections.js";
55
import { CollectionIndexesTool } from "./collectionIndexes.js";
6-
import { ListDatabasesTool } from "./listDatabases.js";
6+
import { ListDatabasesTool } from "./metadata/listDatabases.js";
77
import { MongoDBToolState } from "./mongodbTool.js";
88
import { CreateIndexTool } from "./createIndex.js";
9-
import { CollectionSchemaTool } from "./collectionSchema.js";
10-
import { InsertOneTool } from "./insert/insertOne.js";
11-
import { FindTool } from "./find/find.js";
9+
import { CollectionSchemaTool } from "./metadata/collectionSchema.js";
10+
import { InsertOneTool } from "./create/insertOne.js";
11+
import { FindTool } from "./read/find.js";
12+
import { InsertManyTool } from "./create/insertMany.js";
13+
import { DeleteManyTool } from "./delete/deleteMany.js";
14+
import { DeleteOneTool } from "./delete/deleteOne.js";
15+
import { CollectionStorageSizeTool } from "./metadata/collectionStorageSize.js";
16+
import { CountTool } from "./read/count.js";
17+
import { DbStatsTool } from "./metadata/dbStats.js";
18+
import { AggregateTool } from "./read/aggregate.js";
19+
import { UpdateOneTool } from "./update/updateOne.js";
20+
import { UpdateManyTool } from "./update/updateMany.js";
21+
import { RenameCollectionTool } from "./update/renameCollection.js";
22+
import { DropDatabaseTool } from "./delete/dropDatabase.js";
23+
import { DropCollectionTool } from "./delete/dropCollection.js";
1224

1325
export function registerMongoDBTools(server: McpServer, state: State) {
1426
const mongodbToolState: MongoDBToolState = {};
@@ -22,6 +34,18 @@ export function registerMongoDBTools(server: McpServer, state: State) {
2234
CollectionSchemaTool,
2335
InsertOneTool,
2436
FindTool,
37+
InsertManyTool,
38+
DeleteManyTool,
39+
DeleteOneTool,
40+
CollectionStorageSizeTool,
41+
CountTool,
42+
DbStatsTool,
43+
AggregateTool,
44+
UpdateOneTool,
45+
UpdateManyTool,
46+
RenameCollectionTool,
47+
DropDatabaseTool,
48+
DropCollectionTool,
2549
];
2650

2751
for (const tool of tools) {

0 commit comments

Comments
 (0)