create-user
Add new users to the system by providing required details like name, email, address, age, and phone number for user management operations.
Instructions
Create a new user in the system
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| Yes | |||
| address | Yes | ||
| age | Yes | ||
| phone | Yes |
Implementation Reference
- src/server.ts:98-120 (handler)The main handler function for the 'create-user' tool. It calls the createUser helper with the input params, handles success/error responses, and returns formatted content.async (params) => { try { const userId = await createUser(params); return { content: [ { type: "text", text: `User created successfully with ID: ${userId}`, }, ], }; } catch (error) { console.error("Error creating user:", error); return { content: [ { type: "text", text: `Failed to create user`, }, ], }; } }
- src/server.ts:84-90 (schema)Input schema using Zod for validating parameters: name, email, address, age, phone.{ name: z.string(), email: z.string().email(), address: z.string(), age: z.number().int().min(0), phone: z.string(), },
- src/server.ts:81-121 (registration)Registration of the 'create-user' tool using server.tool, including name, description, input schema, metadata, and inline handler.server.tool( "create-user", "Create a new user in the system", { name: z.string(), email: z.string().email(), address: z.string(), age: z.number().int().min(0), phone: z.string(), }, { title: "Create User", readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true, }, async (params) => { try { const userId = await createUser(params); return { content: [ { type: "text", text: `User created successfully with ID: ${userId}`, }, ], }; } catch (error) { console.error("Error creating user:", error); return { content: [ { type: "text", text: `Failed to create user`, }, ], }; } } );
- src/server.ts:201-207 (helper)Helper function that loads existing users from JSON, appends a new user with incremented ID, writes back to file, and returns the new ID.async function createUser(params: { name: string; email: string; address: string; age: number; phone: string }) { const users = await import("./data/users.json", { with: { type: "json" } }).then((m) => m.default); const newId = users.length + 1; const newUsers = [...users, { id: newId, ...params }]; writeFileSync("./src/data/users.json", JSON.stringify(newUsers, null, 2)); return newId; }