Skip to content

Commit 58bc8a5

Browse files
chore: revert changes done to any of the src files
1 parent da15e6d commit 58bc8a5

File tree

7 files changed

+110
-173
lines changed

7 files changed

+110
-173
lines changed

src/tools/mongodb/create/insertMany.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,6 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
33
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
44
import { ToolArgs, OperationType } from "../../tool.js";
55

6-
export function insertManyResponse(collection: string, insertedCount: number, insertedIds: unknown[]): CallToolResult {
7-
return {
8-
content: [
9-
{
10-
text: `Inserted \`${insertedCount}\` document(s) into collection "${collection}"`,
11-
type: "text",
12-
},
13-
{
14-
text: `Inserted IDs: ${insertedIds.join(", ")}`,
15-
type: "text",
16-
},
17-
],
18-
};
19-
}
20-
216
export class InsertManyTool extends MongoDBToolBase {
227
protected name = "insert-many";
238
protected description = "Insert an array of documents into a MongoDB collection";
@@ -39,6 +24,17 @@ export class InsertManyTool extends MongoDBToolBase {
3924
const provider = await this.ensureConnected();
4025
const result = await provider.insertMany(database, collection, documents);
4126

42-
return insertManyResponse(collection, result.insertedCount, Object.values(result.insertedIds));
27+
return {
28+
content: [
29+
{
30+
text: `Inserted \`${result.insertedCount}\` document(s) into collection "${collection}"`,
31+
type: "text",
32+
},
33+
{
34+
text: `Inserted IDs: ${Object.values(result.insertedIds).join(", ")}`,
35+
type: "text",
36+
},
37+
],
38+
};
4339
}
4440
}

src/tools/mongodb/delete/deleteMany.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
44
import { ToolArgs, OperationType } from "../../tool.js";
55
import { checkIndexUsage } from "../../../helpers/indexCheck.js";
66

7-
export function deleteManyResponse(collection: string, delectedCount: number): CallToolResult {
8-
return {
9-
content: [
10-
{
11-
text: `Deleted \`${delectedCount}\` document(s) from collection "${collection}"`,
12-
type: "text",
13-
},
14-
],
15-
};
16-
}
17-
187
export class DeleteManyTool extends MongoDBToolBase {
198
protected name = "delete-many";
209
protected description = "Removes all documents that match the filter from a MongoDB collection";
@@ -56,6 +45,13 @@ export class DeleteManyTool extends MongoDBToolBase {
5645

5746
const result = await provider.deleteMany(database, collection, filter);
5847

59-
return deleteManyResponse(collection, result.deletedCount);
48+
return {
49+
content: [
50+
{
51+
text: `Deleted \`${result.deletedCount}\` document(s) from collection "${collection}"`,
52+
type: "text",
53+
},
54+
],
55+
};
6056
}
6157
}
Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,7 @@
11
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
22
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
33
import { ToolArgs, OperationType } from "../../tool.js";
4-
import { getSimplifiedSchema, SimplifiedSchema } from "mongodb-schema";
5-
6-
export function collectionSchemaResponse(
7-
database: string,
8-
collection: string,
9-
schema: SimplifiedSchema
10-
): CallToolResult {
11-
const fieldsCount = Object.entries(schema).length;
12-
if (fieldsCount === 0) {
13-
return {
14-
content: [
15-
{
16-
text: `Could not deduce the schema for "${database}.${collection}". This may be because it doesn't exist or is empty.`,
17-
type: "text",
18-
},
19-
],
20-
};
21-
}
22-
23-
return {
24-
content: [
25-
{
26-
text: `Found ${fieldsCount} fields in the schema for "${database}.${collection}"`,
27-
type: "text",
28-
},
29-
{
30-
text: JSON.stringify(schema),
31-
type: "text",
32-
},
33-
],
34-
};
35-
}
4+
import { getSimplifiedSchema } from "mongodb-schema";
365

376
export class CollectionSchemaTool extends MongoDBToolBase {
387
protected name = "collection-schema";
@@ -45,6 +14,30 @@ export class CollectionSchemaTool extends MongoDBToolBase {
4514
const provider = await this.ensureConnected();
4615
const documents = await provider.find(database, collection, {}, { limit: 5 }).toArray();
4716
const schema = await getSimplifiedSchema(documents);
48-
return collectionSchemaResponse(database, collection, schema);
17+
18+
const fieldsCount = Object.entries(schema).length;
19+
if (fieldsCount === 0) {
20+
return {
21+
content: [
22+
{
23+
text: `Could not deduce the schema for "${database}.${collection}". This may be because it doesn't exist or is empty.`,
24+
type: "text",
25+
},
26+
],
27+
};
28+
}
29+
30+
return {
31+
content: [
32+
{
33+
text: `Found ${fieldsCount} fields in the schema for "${database}.${collection}"`,
34+
type: "text",
35+
},
36+
{
37+
text: JSON.stringify(schema),
38+
type: "text",
39+
},
40+
],
41+
};
4942
}
5043
}

src/tools/mongodb/metadata/listCollections.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,6 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
22
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
33
import { ToolArgs, OperationType } from "../../tool.js";
44

5-
export function listCollectionsResponse(database: string, collections: string[]): CallToolResult {
6-
if (collections.length === 0) {
7-
return {
8-
content: [
9-
{
10-
type: "text",
11-
text: `No collections found for database "${database}". To create a collection, use the "create-collection" tool.`,
12-
},
13-
],
14-
};
15-
}
16-
17-
return {
18-
content: collections.map((collection) => {
19-
return {
20-
text: `Name: "${collection}"`,
21-
type: "text",
22-
};
23-
}),
24-
};
25-
}
26-
275
export class ListCollectionsTool extends MongoDBToolBase {
286
protected name = "list-collections";
297
protected description = "List all collections for a given database";
@@ -37,9 +15,24 @@ export class ListCollectionsTool extends MongoDBToolBase {
3715
const provider = await this.ensureConnected();
3816
const collections = await provider.listCollections(database);
3917

40-
return listCollectionsResponse(
41-
database,
42-
collections.map((collection) => `${collection.name}`)
43-
);
18+
if (collections.length === 0) {
19+
return {
20+
content: [
21+
{
22+
type: "text",
23+
text: `No collections found for database "${database}". To create a collection, use the "create-collection" tool.`,
24+
},
25+
],
26+
};
27+
}
28+
29+
return {
30+
content: collections.map((collection) => {
31+
return {
32+
text: `Name: "${collection.name}"`,
33+
type: "text",
34+
};
35+
}),
36+
};
4437
}
4538
}

src/tools/mongodb/metadata/listDatabases.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ import { MongoDBToolBase } from "../mongodbTool.js";
33
import * as bson from "bson";
44
import { OperationType } from "../../tool.js";
55

6-
export function listDatabasesResponse(databases: { name: string; sizeOnDisk: string }[]): CallToolResult {
7-
return {
8-
content: databases.map((db) => {
9-
return {
10-
text: `Name: ${db.name}, Size: ${db.sizeOnDisk} bytes`,
11-
type: "text",
12-
};
13-
}),
14-
};
15-
}
16-
176
export class ListDatabasesTool extends MongoDBToolBase {
187
protected name = "list-databases";
198
protected description = "List all databases for a MongoDB connection";
@@ -24,13 +13,13 @@ export class ListDatabasesTool extends MongoDBToolBase {
2413
const provider = await this.ensureConnected();
2514
const dbs = (await provider.listDatabases("")).databases as { name: string; sizeOnDisk: bson.Long }[];
2615

27-
return listDatabasesResponse(
28-
dbs.map((db) => {
16+
return {
17+
content: dbs.map((db) => {
2918
return {
30-
name: db.name,
31-
sizeOnDisk: db.sizeOnDisk.toString(),
19+
text: `Name: ${db.name}, Size: ${db.sizeOnDisk.toString()} bytes`,
20+
type: "text",
3221
};
33-
})
34-
);
22+
}),
23+
};
3524
}
3625
}

src/tools/mongodb/read/collectionIndexes.ts

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,6 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
22
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
33
import { ToolArgs, OperationType } from "../../tool.js";
44

5-
export function collectionIndexesResponse({
6-
database,
7-
collection,
8-
indexes = [],
9-
namespaceNotFound,
10-
}: {
11-
database: string;
12-
collection: string;
13-
indexes?: { name: string; key: string }[];
14-
namespaceNotFound?: boolean;
15-
}): CallToolResult {
16-
if (namespaceNotFound) {
17-
return {
18-
content: [
19-
{
20-
text: `The indexes for "${database}.${collection}" cannot be determined because the collection does not exist.`,
21-
type: "text",
22-
},
23-
],
24-
};
25-
}
26-
27-
return {
28-
content: [
29-
{
30-
text: `Found ${indexes.length} indexes in the collection "${collection}":`,
31-
type: "text",
32-
},
33-
...(indexes.map((indexDefinition) => {
34-
return {
35-
text: `Name "${indexDefinition.name}", definition: ${JSON.stringify(indexDefinition.key)}`,
36-
type: "text",
37-
};
38-
}) as { text: string; type: "text" }[]),
39-
],
40-
};
41-
}
42-
435
export class CollectionIndexesTool extends MongoDBToolBase {
446
protected name = "collection-indexes";
457
protected description = "Describe the indexes for a collection";
@@ -49,26 +11,36 @@ export class CollectionIndexesTool extends MongoDBToolBase {
4911
protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
5012
const provider = await this.ensureConnected();
5113
const indexes = await provider.getIndexes(database, collection);
52-
return collectionIndexesResponse({
53-
database,
54-
collection,
55-
indexes: indexes.map((index) => ({
56-
name: `${index.name}`,
57-
key: JSON.stringify(index.key),
58-
})),
59-
});
14+
15+
return {
16+
content: [
17+
{
18+
text: `Found ${indexes.length} indexes in the collection "${collection}":`,
19+
type: "text",
20+
},
21+
...(indexes.map((indexDefinition) => {
22+
return {
23+
text: `Name "${indexDefinition.name}", definition: ${JSON.stringify(indexDefinition.key)}`,
24+
type: "text",
25+
};
26+
}) as { text: string; type: "text" }[]),
27+
],
28+
};
6029
}
6130

6231
protected handleError(
6332
error: unknown,
6433
args: ToolArgs<typeof this.argsShape>
6534
): Promise<CallToolResult> | CallToolResult {
6635
if (error instanceof Error && "codeName" in error && error.codeName === "NamespaceNotFound") {
67-
return collectionIndexesResponse({
68-
database: args.database,
69-
collection: args.collection,
70-
namespaceNotFound: true,
71-
});
36+
return {
37+
content: [
38+
{
39+
text: `The indexes for "${args.database}.${args.collection}" cannot be determined because the collection does not exist.`,
40+
type: "text",
41+
},
42+
],
43+
};
7244
}
7345

7446
return super.handleError(error, args);

src/tools/mongodb/read/find.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,6 @@ export const FindArgs = {
2222
.describe("A document, describing the sort order, matching the syntax of the sort argument of cursor.sort()"),
2323
};
2424

25-
export function findResponse(collection: string, documents: unknown[]): CallToolResult {
26-
return {
27-
content: [
28-
{
29-
text: `Found ${documents.length} documents in the collection "${collection}":`,
30-
type: "text",
31-
},
32-
...documents.map<{ type: "text"; text: string }>((doc) => {
33-
return {
34-
text: EJSON.stringify(doc),
35-
type: "text",
36-
};
37-
}),
38-
],
39-
};
40-
}
41-
4225
export class FindTool extends MongoDBToolBase {
4326
protected name = "find";
4427
protected description = "Run a find query against a MongoDB collection";
@@ -67,6 +50,21 @@ export class FindTool extends MongoDBToolBase {
6750

6851
const documents = await provider.find(database, collection, filter, { projection, limit, sort }).toArray();
6952

70-
return findResponse(collection, documents);
53+
const content: Array<{ text: string; type: "text" }> = [
54+
{
55+
text: `Found ${documents.length} documents in the collection "${collection}":`,
56+
type: "text",
57+
},
58+
...documents.map((doc) => {
59+
return {
60+
text: EJSON.stringify(doc),
61+
type: "text",
62+
} as { text: string; type: "text" };
63+
}),
64+
];
65+
66+
return {
67+
content,
68+
};
7169
}
7270
}

0 commit comments

Comments
 (0)