otp-code-generator
Generate Time-based One-Time Password (TOTP) codes using a Base32 secret key. Specify code length and time period for secure, time-sensitive authentication.
Instructions
Generate Time-based One-Time Password (TOTP) codes
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| digits | No | Number of digits in the code | |
| period | No | Time period in seconds | |
| secret | Yes | Base32 encoded secret key |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"digits": {
"description": "Number of digits in the code",
"type": "number"
},
"period": {
"description": "Time period in seconds",
"type": "number"
},
"secret": {
"description": "Base32 encoded secret key",
"type": "string"
}
},
"required": [
"secret"
],
"type": "object"
}
Implementation Reference
- Handler function that generates and verifies TOTP (OTP) code using the speakeasy library based on provided secret, digits, and period parameters.}, async ({ secret, digits = 6, period = 30 }) => { try { if (digits < 4 || digits > 10) { return { content: [ { type: "text", text: "Digits must be between 4 and 10.", }, ], }; } // Generate TOTP code using proper speakeasy library const token = speakeasy.totp({ secret: secret, encoding: 'base32', digits: digits, step: period }); // Calculate remaining time for this token const now = Math.floor(Date.now() / 1000); const timeRemaining = period - (now % period); // Verify the token is valid (for demonstration) const verified = speakeasy.totp.verify({ secret: secret, encoding: 'base32', token: token, step: period, window: 1 }); return { content: [ { type: "text", text: `TOTP Code: ${token} Valid for: ${timeRemaining} seconds Digits: ${digits} Period: ${period} seconds Secret: ${secret} Verified: ${verified ? 'Yes ✅' : 'No ❌'} This code can be used for two-factor authentication. The token changes every ${period} seconds.`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error generating OTP: ${error instanceof Error ? error.message : 'Unknown error'}`, }, ], }; } }
- Zod input schema defining parameters for the OTP generator tool: secret (required Base32 string), optional digits (number), optional period (seconds).inputSchema: { secret: z.string().describe("Base32 encoded secret key"), digits: z.number().describe("Number of digits in the code").optional(), period: z.number().describe("Time period in seconds").optional(), },
- src/tools/crypto/generate_otp/index.ts:5-82 (registration)Registration function for the 'generate_otp' tool (OTP code generator) with MCP server, including description, schema, annotations, and inline handler.export function registerGenerateOtp(server: McpServer) { server.registerTool("generate_otp", { description: "Generate Time-based One-Time Password (TOTP) codes", inputSchema: { secret: z.string().describe("Base32 encoded secret key"), digits: z.number().describe("Number of digits in the code").optional(), period: z.number().describe("Time period in seconds").optional(), }, // VS Code compliance annotations annotations: { title: "Generate Otp", description: "Generate Time-based One-Time Password (TOTP) codes", readOnlyHint: false } }, async ({ secret, digits = 6, period = 30 }) => { try { if (digits < 4 || digits > 10) { return { content: [ { type: "text", text: "Digits must be between 4 and 10.", }, ], }; } // Generate TOTP code using proper speakeasy library const token = speakeasy.totp({ secret: secret, encoding: 'base32', digits: digits, step: period }); // Calculate remaining time for this token const now = Math.floor(Date.now() / 1000); const timeRemaining = period - (now % period); // Verify the token is valid (for demonstration) const verified = speakeasy.totp.verify({ secret: secret, encoding: 'base32', token: token, step: period, window: 1 }); return { content: [ { type: "text", text: `TOTP Code: ${token} Valid for: ${timeRemaining} seconds Digits: ${digits} Period: ${period} seconds Secret: ${secret} Verified: ${verified ? 'Yes ✅' : 'No ❌'} This code can be used for two-factor authentication. The token changes every ${period} seconds.`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error generating OTP: ${error instanceof Error ? error.message : 'Unknown error'}`, }, ], }; } } ); }