get_random_user
Generate a single random user with customizable fields, gender, nationality, and format options like JSON, CSV, SQL, or XML. Includes password generation and advanced filtering.
Instructions
Get a single random user
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fields | No | Specify which fields to include | |
| format | No | ||
| gender | No | Filter results by gender | |
| nationality | No | Specify nationality | |
| password | No |
Implementation Reference
- src/index.ts:531-562 (handler)The main handler function that executes the 'get_random_user' tool. It constructs parameters for the randomuser.me API based on input arguments (gender, nationality, fields, password), fetches the data, formats the results if specified, and handles errors.private async handleGetRandomUser(args: any) { try { const params: any = {}; if (args.gender) params.gender = args.gender; if (args.nationality) params.nat = args.nationality; if (args.fields?.mode === 'include') { params.inc = args.fields.values.join(','); } else if (args.fields?.mode === 'exclude') { params.exc = args.fields.values.join(','); } if (args.password) { const charsets = args.password.charsets?.join(',') || 'upper,lower,number'; params.password = args.password.maxLength ? `${charsets},${args.password.minLength || 8}-${args.password.maxLength}` : `${charsets},${args.password.minLength || 8}`; } const response = await this.axiosInstance.get('', { params }); return this.formatResults(response.data.results, args.format); } catch (error) { if (axios.isAxiosError(error)) { throw new McpError( ErrorCode.InternalError, `API Error: ${error.response?.data.error || error.message}` ); } throw error; } }
- src/index.ts:95-118 (schema)Input schema definition for the 'get_random_user' tool, specifying parameters like gender, nationality, fields selection, output format, and password generation options.inputSchema: { type: 'object', properties: { gender: { type: 'string', enum: ['male', 'female'], description: 'Filter results by gender' }, nationality: { type: 'string', enum: NATIONALITIES, description: 'Specify nationality' }, fields: { type: 'object', description: 'Specify which fields to include', properties: this.getFieldProperties() }, format: this.getFormatOptionsSchema(), password: { type: 'object', properties: this.getPasswordOptionsSchema() } }
- src/index.ts:92-120 (registration)Tool registration in the ListTools response, defining name, description, and input schema for 'get_random_user'.{ name: 'get_random_user', description: 'Get a single random user', inputSchema: { type: 'object', properties: { gender: { type: 'string', enum: ['male', 'female'], description: 'Filter results by gender' }, nationality: { type: 'string', enum: NATIONALITIES, description: 'Specify nationality' }, fields: { type: 'object', description: 'Specify which fields to include', properties: this.getFieldProperties() }, format: this.getFormatOptionsSchema(), password: { type: 'object', properties: this.getPasswordOptionsSchema() } } } },
- src/index.ts:181-182 (registration)Dispatches tool calls to the specific handler in the CallToolRequestSchema handler switch statement.case 'get_random_user': return this.handleGetRandomUser(request.params.arguments);
- src/index.ts:304-335 (helper)Helper function called by the handler to format the raw API results into requested output formats: json, csv, sql, or xml.private formatResults(results: any[], format?: FormatOptions): { content: [{ type: 'text', text: string }] } { if (!format) { return { content: [ { type: 'text', text: JSON.stringify(results, null, 2) } ] }; } switch (format.type) { case 'json': return this.formatJson(results, format.structure); case 'csv': return this.formatCsv(results, format.csv, format.structure); case 'sql': return this.formatSql(results, format.sql, format.structure); case 'xml': return this.formatXml(results, format.structure); default: return { content: [ { type: 'text', text: JSON.stringify(results, null, 2) } ] }; } }