Skip to content

Commit 9fd13b9

Browse files
committed
fix: lint issues
1 parent b191e43 commit 9fd13b9

File tree

10 files changed

+60
-73
lines changed

10 files changed

+60
-73
lines changed

src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export enum ErrorCodes {
22
NotConnectedToMongoDB = 1_000_000,
3+
InvalidParams = 1_000_001,
34
}

src/tools/mongodb/collectionIndexes.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import { z } from "zod";
21
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3-
import { MongoDBToolBase } from "./mongodbTool.js";
2+
import { DbOperationArgs, MongoDBToolBase } from "./mongodbTool.js";
43
import { ToolArgs } from "../tool.js";
54

6-
const argsShape = {
7-
database: z.string().describe("Database name"),
8-
collection: z.string().describe("Collection name"),
9-
};
10-
11-
export class CollectionIndexesTool extends MongoDBToolBase<typeof argsShape> {
5+
export class CollectionIndexesTool extends MongoDBToolBase<typeof DbOperationArgs> {
126
protected name = "collection-indexes";
137
protected description = "Describe the indexes for a collection";
14-
protected argsShape = argsShape;
8+
protected argsShape = DbOperationArgs;
159

16-
protected async execute({ database, collection }: ToolArgs<typeof argsShape>): Promise<CallToolResult> {
10+
protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
1711
const provider = this.ensureConnected();
1812
const indexes = await provider.getIndexes(database, collection);
1913

src/tools/mongodb/collectionSchema.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import { z } from "zod";
21
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3-
import { MongoDBToolBase } from "./mongodbTool.js";
2+
import { DbOperationArgs, MongoDBToolBase } from "./mongodbTool.js";
43
import { ToolArgs } from "../tool.js";
54
import { parseSchema, SchemaField } from "mongodb-schema";
65

7-
const argsShape = {
8-
database: z.string().describe("Database name"),
9-
collection: z.string().describe("Collection name"),
10-
};
11-
12-
export class CollectionSchemaTool extends MongoDBToolBase<typeof argsShape> {
6+
export class CollectionSchemaTool extends MongoDBToolBase<typeof DbOperationArgs> {
137
protected name = "collection-schema";
148
protected description = "Describe the schema for a collection";
15-
protected argsShape = argsShape;
9+
protected argsShape = DbOperationArgs;
1610

17-
protected async execute({ database, collection }: ToolArgs<typeof argsShape>): Promise<CallToolResult> {
11+
protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
1812
const provider = this.ensureConnected();
1913
const documents = await provider.find(database, collection, {}, { limit: 5 }).toArray();
2014
const schema = await parseSchema(documents);

src/tools/mongodb/connect.ts

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CallToolResult, McpError } from "@modelcontextprotocol/sdk/types.js";
33
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
44
import { MongoDBToolBase } from "./mongodbTool.js";
55
import { ToolArgs } from "../tool";
6+
import { ErrorCodes } from "../../errors.js";
67

78
const argsShape = {
89
connectionStringOrClusterName: z
@@ -17,55 +18,48 @@ export class ConnectTool extends MongoDBToolBase<typeof argsShape> {
1718
protected argsShape = argsShape;
1819

1920
protected async execute({ connectionStringOrClusterName }: ToolArgs<typeof argsShape>): Promise<CallToolResult> {
20-
try {
21-
if (!connectionStringOrClusterName) {
22-
// TODO: try reconnecting to the default connection
21+
if (!connectionStringOrClusterName) {
22+
// TODO: try reconnecting to the default connection
23+
return {
24+
content: [
25+
{ type: "text", text: "No connection details provided." },
26+
{ type: "text", text: "Please provide either a connection string or a cluster name" },
27+
{
28+
type: "text",
29+
text: "Alternatively, you can use the default deployment at mongodb://localhost:27017",
30+
},
31+
],
32+
};
33+
}
34+
35+
let connectionString: string;
36+
37+
if (typeof connectionStringOrClusterName === "string") {
38+
if (
39+
connectionStringOrClusterName.startsWith("mongodb://") ||
40+
connectionStringOrClusterName.startsWith("mongodb+srv://")
41+
) {
42+
connectionString = connectionStringOrClusterName;
43+
} else {
44+
// TODO:
2345
return {
2446
content: [
25-
{ type: "text", text: "No connection details provided." },
26-
{ type: "text", text: "Please provide either a connection string or a cluster name" },
2747
{
2848
type: "text",
29-
text: "Alternatively, you can use the default deployment at mongodb://localhost:27017",
49+
text: `Connecting via cluster name not supported yet. Please provide a connection string.`,
3050
},
3151
],
3252
};
3353
}
54+
} else {
55+
throw new McpError(ErrorCodes.InvalidParams, "Invalid connection options");
56+
}
3457

35-
let connectionString: string;
36-
37-
if (typeof connectionStringOrClusterName === "string") {
38-
if (
39-
connectionStringOrClusterName.startsWith("mongodb://") ||
40-
connectionStringOrClusterName.startsWith("mongodb+srv://")
41-
) {
42-
connectionString = connectionStringOrClusterName;
43-
} else {
44-
// TODO:
45-
return {
46-
content: [
47-
{
48-
type: "text",
49-
text: `Connecting via cluster name not supported yet. Please provide a connection string.`,
50-
},
51-
],
52-
};
53-
}
54-
} else {
55-
throw new McpError(2, "Invalid connection options");
56-
}
57-
58-
await this.connect(connectionString);
58+
await this.connect(connectionString);
5959

60-
return {
61-
content: [{ type: "text", text: `Successfully connected to ${connectionString}.` }],
62-
};
63-
} catch (error) {
64-
return {
65-
content: [{ type: "text", text: "Failed to get cluster connection string" }],
66-
isError: true,
67-
};
68-
}
60+
return {
61+
content: [{ type: "text", text: `Successfully connected to ${connectionString}.` }],
62+
};
6963
}
7064

7165
private async connect(connectionString: string): Promise<void> {

src/tools/mongodb/createIndex.ts

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

77
const argsShape = {
8-
database: z.string().describe("Database name"),
9-
collection: z.string().describe("Collection name"),
8+
...DbOperationArgs,
109
keys: z.record(z.string(), z.custom<IndexDirection>()).describe("The index definition"),
1110
};
1211

src/tools/mongodb/find/find.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ export class FindTool extends MongoDBToolBase<typeof argsShape> {
3434
const provider = this.ensureConnected();
3535
const documents = await provider.find(database, collection, filter, { projection, limit }).toArray();
3636

37-
const content: Array<{ text: string; type: string }> = [
37+
const content: Array<{ text: string; type: "text" }> = [
3838
{
39-
text: `Found ${documents.length} documents in the collection \`${collection}\``,
39+
text: `Found ${documents.length} documents in the collection \`${collection}\`:`,
4040
type: "text",
4141
},
4242
...documents.map((doc) => {
4343
return {
44-
text: `Document: \`${JSON.stringify(doc)}\``,
44+
text: JSON.stringify(doc),
4545
type: "text",
46-
};
46+
} as { text: string; type: "text" };
4747
}),
4848
];
4949

5050
return {
51-
content: content as any,
51+
content,
5252
};
5353
}
5454
}

src/tools/mongodb/listCollections.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { z } from "zod";
21
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3-
import { MongoDBToolBase } from "./mongodbTool.js";
2+
import { DbOperationArgs, MongoDBToolBase } from "./mongodbTool.js";
43
import { ToolArgs } from "../tool.js";
54

65
const argsShape = {
7-
database: z.string().describe("Database name"),
6+
database: DbOperationArgs.database,
87
};
98

109
export class ListCollectionsTool extends MongoDBToolBase<typeof argsShape> {

src/tools/mongodb/listDatabases.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
22
import { MongoDBToolBase } from "./mongodbTool.js";
3-
import { ToolArgs } from "../tool.js";
43
import * as bson from "bson";
54

6-
export class ListDatabasesTool extends MongoDBToolBase<{}> {
5+
export class ListDatabasesTool extends MongoDBToolBase {
76
protected name = "list-databases";
87
protected description = "List all databases for a MongoDB connection";
98
protected argsShape = {};

src/tools/mongodb/mongodbTool.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ZodRawShape } from "zod";
1+
import { z, ZodRawShape } from "zod";
22
import { ToolBase } from "../tool.js";
33
import { State } from "../../state.js";
44
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
@@ -7,6 +7,11 @@ import { ErrorCodes } from "../../errors.js";
77

88
export type MongoDBToolState = { serviceProvider?: NodeDriverServiceProvider };
99

10+
export const DbOperationArgs = {
11+
database: z.string().describe("Database name"),
12+
collection: z.string().describe("Collection name"),
13+
};
14+
1015
export abstract class MongoDBToolBase<Args extends ZodRawShape = ZodRawShape> extends ToolBase<Args> {
1116
constructor(
1217
state: State,

src/tools/tool.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export abstract class ToolBase<Args extends ZodRawShape> {
6363
}
6464
}
6565

66+
// This method is intended to be overridden by subclasses to handle errors
67+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6668
protected handleError(error: unknown): CallToolResult | undefined {
6769
return undefined;
6870
}

0 commit comments

Comments
 (0)