crear-usuario
Create a new user by providing a name, surname, and DNI. This tool is part of the MCP API Server, enabling efficient user management with structured input validation.
Instructions
Crea un nuevo usuario con nombre, apellido y DNI
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| apellido | Yes | ||
| dni | Yes | ||
| nombre | Yes |
Implementation Reference
- src/tools/userCreateTool.ts:18-48 (handler)The main handler function for the 'crear-usuario' tool. It extracts parameters, calls the user service to create the user, formats a success or error response in MCP format.export async function createUserToolHandler(params: any) { const { nombre, apellido, dni } = params; try { // Utilizamos el servicio de usuarios const userData = await createUser(nombre, apellido, dni); // Devolvemos el resultado formateado para MCP return { content: [ { type: "text" as const, text: `✅ Usuario creado correctamente: - Nombre: ${userData.nombre} - Apellido: ${userData.apellido} - DNI: ${userData.dni} - ID: ${userData.id}` } ] }; } catch (error) { return { content: [ { type: "text" as const, text: `❌ Error al crear usuario: ${error.message}` } ] }; } }
- src/tools/userCreateTool.ts:7-11 (schema)Input schema for the 'crear-usuario' tool defining validation rules for nombre, apellido, and dni using Zod.export const createUserInputSchema = { nombre: z.string().min(2, "El nombre debe tener al menos 2 caracteres"), apellido: z.string().min(2, "El apellido debe tener al menos 2 caracteres"), dni: z.string().min(8, "El DNI debe tener al menos 8 caracteres") };
- src/main.ts:34-41 (registration)Registration of the 'crear-usuario' tool in the MCP server, linking the name, description, input schema, and handler function.server.registerTool( "crear-usuario", { description: "Crea un nuevo usuario con nombre, apellido y DNI", inputSchema: createUserInputSchema }, createUserToolHandler );
- src/services/userService.ts:26-47 (helper)Core helper function that performs user creation logic: validation, ID generation, in-memory storage, and returns the created user object.export async function createUser(nombre: string, apellido: string, dni: string): Promise<User> { // Validar que todos los campos estén presentes if (!nombre) throw new Error('El campo nombre es obligatorio'); if (!apellido) throw new Error('El campo apellido es obligatorio'); if (!dni) throw new Error('El campo DNI es obligatorio'); // En un caso real, aquí validaríamos también el formato del DNI // Crear un nuevo usuario const newUser: User = { id: generateId(), nombre, apellido, dni, createdAt: new Date() }; // Guardar el usuario (en un caso real, esto sería en una base de datos) users.push(newUser); return newUser; }