generate-id
Generate unique identifiers using UUID, CUID2, Nanoid, or ULID algorithms for database records, API requests, or distributed systems.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "generate-id"
}
}Input 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 unique IDs using the chosen algorithm (cuid2, uuid, nanoid, or ulid) and returns them comma-separated 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)Zod schema defining the input parameters for the tool: 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 using server.tool(name, schema, handler).
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 handler to format responses as CallToolResult objects, with optional error flag.
function respond(text: string, isError: boolean = false): CallToolResult { return { content: [ { type: 'text' as const, text } ], isError }; }