generate_random_string
Create cryptographically secure random strings with customizable length, character set, and count for enhanced data security and flexibility.
Instructions
Generate a cryptographically secure random string
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| charset | No | Character set to use | alphanumeric |
| count | No | Number of random strings to generate | |
| length | No | Length of the random string |
Implementation Reference
- src/index.ts:497-547 (handler)The core handler function implementing the generate_random_string tool logic, generating cryptographically secure random strings using Node.js crypto.randomInt from specified character sets.private async generateRandomString(args: any) { const length = args.length ?? 16; const charset = args.charset ?? "alphanumeric"; const count = args.count ?? 1; if (length < 1 || length > 256) { throw new Error("Length must be between 1 and 256"); } if (count < 1 || count > 100) { throw new Error("Count must be between 1 and 100"); } const charsets = { alphanumeric: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", alphabetic: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", numeric: "0123456789", hex: "0123456789abcdef", base64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", ascii_printable: "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", }; const chars = charsets[charset as keyof typeof charsets]; if (!chars) { throw new Error(`Invalid charset: ${charset}`); } const results: string[] = []; for (let i = 0; i < count; i++) { let result = ""; for (let j = 0; j < length; j++) { const randomIndex = randomInt(0, chars.length); result += chars[randomIndex]; } results.push(result); } return { content: [ { type: "text", text: JSON.stringify({ type: "random_strings", values: results, parameters: { length, charset, count }, timestamp: new Date().toISOString(), }, null, 2), }, ], }; }
- src/index.ts:156-185 (registration)Tool registration in the ListToolsRequestSchema handler, defining the tool name, description, and input schema.{ name: "generate_random_string", description: "Generate a cryptographically secure random string", inputSchema: { type: "object", properties: { length: { type: "integer", description: "Length of the random string", default: 16, minimum: 1, maximum: 256, }, charset: { type: "string", description: "Character set to use", enum: ["alphanumeric", "alphabetic", "numeric", "hex", "base64", "ascii_printable"], default: "alphanumeric", }, count: { type: "integer", description: "Number of random strings to generate", default: 1, minimum: 1, maximum: 100, }, }, required: [], }, },
- src/index.ts:159-183 (schema)Input schema validation for the generate_random_string tool parameters.inputSchema: { type: "object", properties: { length: { type: "integer", description: "Length of the random string", default: 16, minimum: 1, maximum: 256, }, charset: { type: "string", description: "Character set to use", enum: ["alphanumeric", "alphabetic", "numeric", "hex", "base64", "ascii_printable"], default: "alphanumeric", }, count: { type: "integer", description: "Number of random strings to generate", default: 1, minimum: 1, maximum: 100, }, }, required: [],
- src/index.ts:257-258 (handler)Dispatch case in CallToolRequestSchema handler that routes to the generateRandomString implementation.case "generate_random_string": return await this.generateRandomString(args);