Skip to content

Commit 4987a6b

Browse files
committed
simplify tool types
1 parent 9fd13b9 commit 4987a6b

File tree

12 files changed

+66
-72
lines changed

12 files changed

+66
-72
lines changed

src/tools/atlas/atlasTool.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { ZodRawShape } from "zod";
21
import { ToolBase } from "../tool.js";
32
import { ApiClient } from "../../client.js";
43
import { State } from "../../state.js";
54

6-
export abstract class AtlasToolBase<Args extends ZodRawShape = ZodRawShape> extends ToolBase<Args> {
5+
export abstract class AtlasToolBase extends ToolBase {
76
constructor(
87
state: State,
98
protected apiClient: ApiClient

src/tools/atlas/listClusters.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { AtlasToolBase } from "./atlasTool.js";
77
import { State } from "../../state.js";
88
import { ToolArgs } from "../tool.js";
99

10-
export class ListClustersTool extends AtlasToolBase<{
11-
projectId: ZodString | ZodOptional<ZodString>;
12-
}> {
10+
export class ListClustersTool extends AtlasToolBase {
1311
protected name = "listClusters";
1412
protected description = "List MongoDB Atlas clusters";
1513
protected argsShape;

src/tools/atlas/tools.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { ZodRawShape } from "zod";
21
import { ToolBase } from "../tool.js";
32
import { ApiClient } from "../../client.js";
43
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -8,7 +7,7 @@ import { ListClustersTool } from "./listClusters.js";
87
import { ListProjectsTool } from "./listProjects.js";
98

109
export function registerAtlasTools(server: McpServer, state: State, apiClient: ApiClient) {
11-
const tools: ToolBase<ZodRawShape>[] = [
10+
const tools: ToolBase[] = [
1211
new AuthTool(state, apiClient),
1312
new ListClustersTool(state, apiClient),
1413
new ListProjectsTool(state, apiClient),

src/tools/mongodb/collectionIndexes.ts

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

5-
export class CollectionIndexesTool extends MongoDBToolBase<typeof DbOperationArgs> {
5+
export class CollectionIndexesTool extends MongoDBToolBase {
66
protected name = "collection-indexes";
77
protected description = "Describe the indexes for a collection";
88
protected argsShape = DbOperationArgs;

src/tools/mongodb/collectionSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DbOperationArgs, MongoDBToolBase } from "./mongodbTool.js";
33
import { ToolArgs } from "../tool.js";
44
import { parseSchema, SchemaField } from "mongodb-schema";
55

6-
export class CollectionSchemaTool extends MongoDBToolBase<typeof DbOperationArgs> {
6+
export class CollectionSchemaTool extends MongoDBToolBase {
77
protected name = "collection-schema";
88
protected description = "Describe the schema for a collection";
99
protected argsShape = DbOperationArgs;

src/tools/mongodb/connect.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import { MongoDBToolBase } from "./mongodbTool.js";
55
import { ToolArgs } from "../tool";
66
import { ErrorCodes } from "../../errors.js";
77

8-
const argsShape = {
9-
connectionStringOrClusterName: z
10-
.string()
11-
.optional()
12-
.describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format) or cluster name"),
13-
};
14-
15-
export class ConnectTool extends MongoDBToolBase<typeof argsShape> {
8+
export class ConnectTool extends MongoDBToolBase {
169
protected name = "connect";
1710
protected description = "Connect to a MongoDB instance";
18-
protected argsShape = argsShape;
11+
protected argsShape = {
12+
connectionStringOrClusterName: z
13+
.string()
14+
.optional()
15+
.describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format) or cluster name"),
16+
};
1917

20-
protected async execute({ connectionStringOrClusterName }: ToolArgs<typeof argsShape>): Promise<CallToolResult> {
18+
protected async execute({
19+
connectionStringOrClusterName,
20+
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
2121
if (!connectionStringOrClusterName) {
2222
// TODO: try reconnecting to the default connection
2323
return {

src/tools/mongodb/createIndex.ts

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

7-
const argsShape = {
8-
...DbOperationArgs,
9-
keys: z.record(z.string(), z.custom<IndexDirection>()).describe("The index definition"),
10-
};
11-
12-
export class CreateIndexTool extends MongoDBToolBase<typeof argsShape> {
7+
export class CreateIndexTool extends MongoDBToolBase {
138
protected name = "create-index";
149
protected description = "Create an index for a collection";
15-
protected argsShape = argsShape;
10+
protected argsShape = {
11+
...DbOperationArgs,
12+
keys: z.record(z.string(), z.custom<IndexDirection>()).describe("The index definition"),
13+
};
1614

17-
protected async execute({ database, collection, keys }: ToolArgs<typeof argsShape>): Promise<CallToolResult> {
15+
protected async execute({ database, collection, keys }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
1816
const provider = this.ensureConnected();
1917
const indexes = await provider.createIndexes(database, collection, [
2018
{

src/tools/mongodb/find/find.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,32 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
33
import { MongoDBToolBase } from "../mongodbTool.js";
44
import { ToolArgs } from "../../tool.js";
55

6-
const argsShape = {
7-
collection: z.string().describe("Collection name"),
8-
database: z.string().describe("Database name"),
9-
filter: z
10-
.object({})
11-
.passthrough()
12-
.optional()
13-
.describe("The query filter, matching the syntax of the query argument of db.collection.find()"),
14-
projection: z
15-
.object({})
16-
.passthrough()
17-
.optional()
18-
.describe("The projection, matching the syntax of the projection argument of db.collection.find()"),
19-
limit: z.number().optional().default(10).describe("The maximum number of documents to return"),
20-
};
21-
22-
export class FindTool extends MongoDBToolBase<typeof argsShape> {
6+
export class FindTool extends MongoDBToolBase {
237
protected name = "find";
248
protected description = "Run a find query against a MongoDB collection";
25-
protected argsShape = argsShape;
9+
protected argsShape = {
10+
collection: z.string().describe("Collection name"),
11+
database: z.string().describe("Database name"),
12+
filter: z
13+
.object({})
14+
.passthrough()
15+
.optional()
16+
.describe("The query filter, matching the syntax of the query argument of db.collection.find()"),
17+
projection: z
18+
.object({})
19+
.passthrough()
20+
.optional()
21+
.describe("The projection, matching the syntax of the projection argument of db.collection.find()"),
22+
limit: z.number().optional().default(10).describe("The maximum number of documents to return"),
23+
};
2624

2725
protected async execute({
2826
database,
2927
collection,
3028
filter,
3129
projection,
3230
limit,
33-
}: ToolArgs<typeof argsShape>): Promise<CallToolResult> {
31+
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
3432
const provider = this.ensureConnected();
3533
const documents = await provider.find(database, collection, filter, { projection, limit }).toArray();
3634

src/tools/mongodb/insert/insertOne.ts

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

6-
const argsShape = {
7-
collection: z.string().describe("Collection name"),
8-
database: z.string().describe("Database name"),
9-
document: z
10-
.object({})
11-
.passthrough()
12-
.describe("The document to insert, matching the syntax of the document argument of db.collection.insertOne()"),
13-
};
14-
15-
export class InsertOneTool extends MongoDBToolBase<typeof argsShape> {
6+
export class InsertOneTool extends MongoDBToolBase {
167
protected name = "insert-one";
178
protected description = "Insert a document into a MongoDB collection";
18-
protected argsShape = argsShape;
9+
protected argsShape = {
10+
collection: z.string().describe("Collection name"),
11+
database: z.string().describe("Database name"),
12+
document: z
13+
.object({})
14+
.passthrough()
15+
.describe(
16+
"The document to insert, matching the syntax of the document argument of db.collection.insertOne()"
17+
),
18+
};
1919

20-
protected async execute({ database, collection, document }: ToolArgs<typeof argsShape>): Promise<CallToolResult> {
20+
protected async execute({
21+
database,
22+
collection,
23+
document,
24+
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
2125
const provider = this.ensureConnected();
2226
const result = await provider.insertOne(database, collection, document);
2327

src/tools/mongodb/listCollections.ts

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

5-
const argsShape = {
6-
database: DbOperationArgs.database,
7-
};
8-
9-
export class ListCollectionsTool extends MongoDBToolBase<typeof argsShape> {
5+
export class ListCollectionsTool extends MongoDBToolBase {
106
protected name = "list-collections";
117
protected description = "List all collections for a given database";
12-
protected argsShape = argsShape;
8+
protected argsShape = {
9+
database: DbOperationArgs.database,
10+
};
1311

14-
protected async execute({ database }: ToolArgs<typeof argsShape>): Promise<CallToolResult> {
12+
protected async execute({ database }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
1513
const provider = this.ensureConnected();
1614
const collections = await provider.listCollections(database);
1715

0 commit comments

Comments
 (0)