Skip to main content
Glama
andreax79

otp-mcp-server

by andreax79

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
NameRequiredDescriptionDefault
patternYes

Implementation Reference

  • 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)
  • 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()
  • 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

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/andreax79/otp-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server