generate-id
Generate unique identifiers using algorithms like CUID2, UUID, NanoID, or ULID with customizable quantity. Designed for creating collision-resistant IDs in various applications.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| algorithm | No | cuid2 | |
| quantity | No |
Implementation Reference
- src/index.ts:25-56 (handler)The handler function for the 'generate-id' tool. It generates the specified quantity of IDs using the chosen algorithm (cuid2, uuid, nanoid, or ulid) and returns them as a comma-separated string or an error.async ({ algorithm, quantity }) => { try { let ids: string[] = []; for (let i = 0; i < quantity; i++) { switch (algorithm) { case 'cuid2': ids.push(createId()); break; case 'nanoid': ids.push(nanoid()); break; case 'ulid': ids.push(ulid()); break; case 'uuid': default: ids.push(uuidv4()); break; } } if (ids.length === 0) { return respond('Failed to generate IDs', true); } return respond(ids.join(',')); } catch (err: unknown) { const error = err as Error; return respond(`Error: ${error.message}`, true); } }
- src/index.ts:21-24 (schema)Input schema for the 'generate-id' tool using Zod, defining 'algorithm' (enum with default 'cuid2') and 'quantity' (number with default 1).{ algorithm: z.enum(['cuid2', 'uuid', 'nanoid', 'ulid']).default('cuid2'), quantity: z.number().default(1), },
- src/index.ts:19-57 (registration)Registration of the 'generate-id' tool on the MCP server, specifying the tool name, input schema, and handler function.server.tool( 'generate-id', { algorithm: z.enum(['cuid2', 'uuid', 'nanoid', 'ulid']).default('cuid2'), quantity: z.number().default(1), }, async ({ algorithm, quantity }) => { try { let ids: string[] = []; for (let i = 0; i < quantity; i++) { switch (algorithm) { case 'cuid2': ids.push(createId()); break; case 'nanoid': ids.push(nanoid()); break; case 'ulid': ids.push(ulid()); break; case 'uuid': default: ids.push(uuidv4()); break; } } if (ids.length === 0) { return respond('Failed to generate IDs', true); } return respond(ids.join(',')); } catch (err: unknown) { const error = err as Error; return respond(`Error: ${error.message}`, true); } } );
- src/index.ts:59-69 (helper)Helper function used by the tool handler to format successful or error responses as CallToolResult objects.function respond(text: string, isError: boolean = false): CallToolResult { return { content: [ { type: 'text' as const, text } ], isError }; }