generate_password
Create a strong, random password with customizable length using the MCP Rand server. This tool ensures a mix of character types for enhanced security while operating locally on your machine.
Instructions
Generate a strong password with a mix of character types. WARNING: While this password is generated locally on your machine, it is recommended to use a dedicated password manager for generating and storing passwords securely.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| length | No | Password length (minimum 8, default 16) |
Implementation Reference
- The handler function for the 'generate_password' tool. It extracts the optional length parameter, calls generateStrongPassword, and returns the generated password as tool result content.export const generatePasswordHandler = async ( request: CallToolRequest ): Promise<CallToolResult> => { const args = request.params.arguments as { length?: number }; const length = args.length ?? DEFAULT_LENGTH; const password = generateStrongPassword(length); return { content: [ { type: 'text', text: password } ] }; };
- The tool specification (schema) defining the name, description, and input schema for the 'generate_password' tool.export const toolSpec = { name: 'generate_password', description: 'Generate a strong password with a mix of character types. WARNING: While this password is generated locally on your machine, it is recommended to use a dedicated password manager for generating and storing passwords securely.', inputSchema: { type: 'object' as const, properties: { length: { type: 'number', description: `Password length (minimum ${MIN_LENGTH}, default ${DEFAULT_LENGTH})`, } } } };
- src/index.ts:23-23 (registration)Registers the 'generate_password' tool handler in the MCP server registry under the 'tools/call' method.registry.register('tools/call', 'generate_password', generatePasswordHandler as Handler);
- Core helper function that implements the password generation logic, ensuring diversity and minimum length.function generateStrongPassword(length: number): string { if (length < MIN_LENGTH) { throw new McpError( ErrorCode.InvalidParams, `Password length must be at least ${MIN_LENGTH} characters` ); } // Ensure at least one character from each set let password = ''; password += charsets.uppercase[Math.floor(Math.random() * charsets.uppercase.length)]; password += charsets.lowercase[Math.floor(Math.random() * charsets.lowercase.length)]; password += charsets.numbers[Math.floor(Math.random() * charsets.numbers.length)]; password += charsets.special[Math.floor(Math.random() * charsets.special.length)]; // Fill the rest with random characters from all sets const allChars = Object.values(charsets).join(''); while (password.length < length) { password += allChars[Math.floor(Math.random() * allChars.length)]; } // Shuffle to avoid predictable pattern of character types return shuffleString(password); }
- Helper function to shuffle the characters in the generated password for randomness.function shuffleString(str: string): string { const array = str.split(''); for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array.join(''); }