calculate_otp_codes
Generate OTP codes for tokens matching a specific pattern using TOTP or HOTP algorithms to verify identity and secure access.
Instructions
Calculate the OTP code for all tokens matching the pattern.
Args:
pattern: Token pattern (part of the name or token number)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes |
Implementation Reference
- otp_mcp/tool.py:98-116 (handler)The core handler function for the 'calculate_otp_codes' MCP tool, decorated with @mcp.tool() for automatic registration. It finds matching OTP tokens using the helper find_tokens, computes their OTP codes, and returns formatted results.@mcp.tool() async def calculate_otp_codes(pattern: str) -> str: """ Calculate the OTP code for all tokens matching the pattern. Args: pattern: Token pattern (part of the name or token number) """ codes = [] tokens = find_tokens(pattern) for token in tokens: try: otp = token.calculate() codes.append(f"{token.rowid}# {str(token)} {otp}") except Exception: raise ToolError(f"Error generating OTP code for token {token.rowid}# {str(token)}") if not codes: raise ToolError("No OTP tokens found.") return "\n".join(codes)
- otp_mcp/tool.py:36-49 (helper)Supporting helper function used by calculate_otp_codes (and other tools) to retrieve OTP tokens matching a search pattern in issuer, account, or ID.def find_tokens(pattern: str) -> list[Token]: """ Find tokens matching the given pattern. """ db = get_token_db() if not pattern: return db.get_tokens() tokens_list = [] pattern = pattern.lower() for token in db.get_tokens(): tmp = str(token).lower().strip() if pattern in tmp or pattern == f"{token.rowid}#": tokens_list.append(token) return tokens_list
- otp_mcp/tool.py:98-98 (registration)The @mcp.tool() decorator registers the calculate_otp_codes function with the MCP server (imported from .server). Registration is triggered when tool.py is imported (e.g., in __main__.py).@mcp.tool()
- otp_mcp/server.py:43-45 (helper)Global helper function providing access to the shared TokenDb instance, used in tool.py helpers and handlers.def get_token_db() -> TokenDb: """Get the token database instance.""" return _token_db