generate_string
Create a random string of a specified length and character set, such as alphanumeric, numeric, lowercase, uppercase, or special characters.
Instructions
Generate a random string with specified length and character set
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| charset | No | Character set to use (alphanumeric, numeric, lowercase, uppercase, special). Defaults to alphanumeric. | |
| length | No | Length of the string to generate. Defaults to 10. |
Implementation Reference
- The generateStringHandler function that executes the tool: parses arguments, validates inputs, generates random string using helper, and returns as MCP tool result.export const generateStringHandler = async ( request: CallToolRequest ): Promise<CallToolResult> => { const args = request.params.arguments as { length?: number; charset?: CharsetType }; const length = args.length ?? 10; const charsetType = args.charset ?? 'alphanumeric'; if (length <= 0) { throw new McpError( ErrorCode.InvalidParams, 'Length must be a positive number' ); } if (!Object.keys(charsets).includes(charsetType)) { throw new McpError( ErrorCode.InvalidParams, 'Invalid charset specified' ); } const randomString = generateRandomString(length, charsets[charsetType]); return { content: [ { type: 'text', text: randomString } ] }; };
- The toolSpec defining the name, description, and input schema for 'generate_string' tool.export const toolSpec = { name: 'generate_string', description: 'Generate a random string with specified length and character set', inputSchema: { type: 'object' as const, properties: { length: { type: 'number', description: 'Length of the string to generate. Defaults to 10.', }, charset: { type: 'string', description: 'Character set to use (alphanumeric, numeric, lowercase, uppercase, special). Defaults to alphanumeric.', enum: ['alphanumeric', 'numeric', 'lowercase', 'uppercase', 'special'] } } } };
- src/index.ts:22-22 (registration)Registration of the generateStringHandler under the 'generate_string' tool name in the MCP handler registry.registry.register('tools/call', 'generate_string', generateStringHandler as Handler);
- Helper function to generate the random string by sampling characters from the specified charset.function generateRandomString(length: number, charset: string): string { let result = ''; for (let i = 0; i < length; i++) { const randomIndex = Math.floor(Math.random() * charset.length); result += charset[randomIndex]; } return result; }
- Charset definitions used by the generate_string tool for different character sets.const charsets: Record<CharsetType, string> = { alphanumeric: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', numeric: '0123456789', lowercase: 'abcdefghijklmnopqrstuvwxyz', uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', special: '!@#$%^&*()_+-=[]{};\'"\\|,.<>/?' };