Skip to content

Commit b89637f

Browse files
committed
fix: add argument validation
1 parent 76aa332 commit b89637f

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/tools/atlas/create/createFreeCluster.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { z } from "zod";
21
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
32
import { AtlasToolBase } from "../atlasTool.js";
43
import type { ToolArgs, OperationType } from "../../tool.js";
54
import type { ClusterDescription20240805 } from "../../../common/atlas/openapi.js";
65
import { ensureCurrentIpInAccessList } from "../../../common/atlas/accessListUtils.js";
6+
import { AtlasArgs } from "../validators.js";
77

88
export class CreateFreeClusterTool extends AtlasToolBase {
99
public name = "atlas-create-free-cluster";
1010
protected description = "Create a free MongoDB Atlas cluster";
1111
public operationType: OperationType = "create";
1212
protected argsShape = {
13-
projectId: z.string().describe("Atlas project ID to create the cluster in"),
14-
name: z.string().describe("Name of the cluster"),
15-
region: z.string().describe("Region of the cluster").default("US_EAST_1"),
13+
projectId: AtlasArgs.projectId().describe("Atlas project ID to create the cluster in"),
14+
name: AtlasArgs.clusterName().describe("Name of the cluster"),
15+
region: AtlasArgs.region().describe("Region of the cluster").default("US_EAST_1"),
1616
};
1717

1818
protected async execute({ projectId, name, region }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {

src/tools/atlas/validators.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+
/**
3+
* Common Zod validators for Atlas tools
4+
* These can be reused across different Atlas tools for consistent validation
5+
*/
6+
export const AtlasArgs = {
7+
objectId: (fieldName: string): z.ZodString =>
8+
z
9+
.string()
10+
.min(1, `${fieldName} is required`)
11+
.regex(/^[0-9a-fA-F]{24}$/, `${fieldName} must be a valid 24-character hexadecimal string`),
12+
13+
projectId: (): z.ZodString => AtlasArgs.objectId("Project ID"),
14+
15+
organizationId: (): z.ZodString => AtlasArgs.objectId("Organization ID"),
16+
17+
clusterName: (): z.ZodString =>
18+
z
19+
.string()
20+
.min(1, "Cluster name is required")
21+
.max(64, "Cluster name must be 64 characters or less")
22+
.regex(/^[a-zA-Z0-9_-]+$/, "Cluster name can only contain letters, numbers, hyphens, and underscores"),
23+
24+
username: (): z.ZodString =>
25+
z
26+
.string()
27+
.min(1, "Username is required")
28+
.max(100, "Username must be 100 characters or less")
29+
.regex(/^[a-zA-Z0-9._-]+$/, "Username can only contain letters, numbers, dots, hyphens, and underscores"),
30+
31+
ipAddress: (): z.ZodString => z.string().ip({ version: "v4" }),
32+
33+
cidrBlock: (): z.ZodString =>
34+
z.string().regex(/^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$/, "Must be a valid CIDR block (e.g., 192.168.1.0/24)"),
35+
36+
region: (): z.ZodString =>
37+
z.string().regex(/^[a-zA-Z0-9_-]+$/, "Region can only contain letters, numbers, hyphens, and underscores"),
38+
};

0 commit comments

Comments
 (0)