add_user
Create and manage new users on the MCP Test Server by specifying name, email, and role. Simplify user administration with this tool.
Instructions
添加新用户
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| Yes | 邮箱地址 | ||
| name | Yes | 用户姓名 | |
| role | No | 用户角色 | user |
Implementation Reference
- src/index.ts:100-118 (handler)The handler function for the 'add_user' tool. It destructures the input args, generates a new user ID, creates the new user object, appends it to testData.users, logs the action to testData.logs, and returns a success message with the new user details.}, async (args) => { const { name, email, role = "user" } = args; const newUser = { id: Math.max(...testData.users.map(u => u.id)) + 1, name, email, role }; testData.users.push(newUser); testData.logs.push(`添加用户: ${name} (${email})`); return { content: [{ type: "text", text: `✅ 成功添加用户: ${JSON.stringify(newUser, null, 2)}` }] }; });
- src/index.ts:97-99 (schema)Zod input schema for the 'add_user' tool defining required string fields 'name' and 'email', and optional 'role' enum with default 'user'.name: z.string().describe("用户姓名"), email: z.string().describe("邮箱地址"), role: z.enum(["admin", "user"]).optional().default("user").describe("用户角色")
- src/index.ts:96-118 (registration)Registration of the 'add_user' tool on the MCP server, including name, description in Chinese, input schema, and inline handler function.server.tool("add_user", "添加新用户", { name: z.string().describe("用户姓名"), email: z.string().describe("邮箱地址"), role: z.enum(["admin", "user"]).optional().default("user").describe("用户角色") }, async (args) => { const { name, email, role = "user" } = args; const newUser = { id: Math.max(...testData.users.map(u => u.id)) + 1, name, email, role }; testData.users.push(newUser); testData.logs.push(`添加用户: ${name} (${email})`); return { content: [{ type: "text", text: `✅ 成功添加用户: ${JSON.stringify(newUser, null, 2)}` }] }; });
- src/index.ts:18-30 (helper)Shared testData object that stores the users array (modified by add_user) and logs array (updated by handler). Used across tools and resources.const testData = { users: [ { id: 1, name: "张三", email: "zhangsan@example.com", role: "admin" }, { id: 2, name: "李四", email: "lisi@example.com", role: "user" }, { id: 3, name: "王五", email: "wangwu@example.com", role: "user" } ], todos: [ { id: 1, title: "完成MCP测试项目", completed: false, userId: 1 }, { id: 2, title: "学习TypeScript", completed: true, userId: 2 }, { id: 3, title: "测试MCP功能", completed: false, userId: 1 } ], logs: [] as string[] };