Skip to main content
Glama

yeepay_yop_gen_key_pair

Generates an asymmetric key pair (RSA or SM2) in PKCS1 or PKCS8 format, saved to a file or returned as a string.

Instructions

根据密钥算法生成非对称加密的密钥对(公钥和私钥),并保存到本地路径

参数: algorithm: 密钥算法,可选值为 "RSA" 或 "SM2",默认为 "RSA" key_format: 密钥格式,可选值为 "pkcs8"或"pkcs1",默认为 "pkcs8" storage_type: 密钥存储类型,"file"或"string",默认为 "file"

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
algorithmNoRSA
key_formatNopkcs8
storage_typeNofile

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The MCP tool handler for 'yeepay_yop_gen_key_pair'. Decorated with @mcp.tool() and delegates to gen_key_pair() from tools.cert_utils.
    def yeepay_yop_gen_key_pair(
        algorithm: str = "RSA", key_format: str = "pkcs8", storage_type: str = "file"
    ) -> Dict[str, Any]:
        """
        根据密钥算法生成非对称加密的密钥对(公钥和私钥),并保存到本地路径
    
        参数:
            algorithm: 密钥算法,可选值为 "RSA" 或 "SM2",默认为 "RSA"
            key_format: 密钥格式,可选值为 "pkcs8"或"pkcs1",默认为 "pkcs8"
            storage_type: 密钥存储类型,"file"或"string",默认为 "file"
        """
        return gen_key_pair(
            algorithm=algorithm, format=key_format, storage_type=storage_type
        )
  • The actual implementation function gen_key_pair() that generates RSA or SM2 key pairs, formats them (PKCS8 or PKCS1), and optionally saves to file.
    def gen_key_pair(  # pylint: disable=too-many-arguments,too-many-positional-arguments,redefined-builtin
        algorithm: str = "RSA", format: str = "pkcs8", storage_type: str = "file"
    ) -> Dict[str, Any]:
        try:
            private_key_str = None
            public_key_str = None
    
            if algorithm.upper() == "RSA":
                # 生成RSA密钥对,使用2048位密钥长度
                private_key = rsa.generate_private_key(
                    public_exponent=65537, key_size=2048, backend=default_backend()
                )
                public_key = private_key.public_key()
    
                # 处理私钥格式
                if format.lower() == "pkcs8":
                    private_key_bytes = private_key.private_bytes(
                        encoding=serialization.Encoding.PEM,
                        format=serialization.PrivateFormat.PKCS8,
                        encryption_algorithm=serialization.NoEncryption(),
                    )
                else:  # pkcs1
                    private_key_bytes = private_key.private_bytes(
                        encoding=serialization.Encoding.PEM,
                        format=serialization.PrivateFormat.TraditionalOpenSSL,
                        encryption_algorithm=serialization.NoEncryption(),
                    )
    
                # 处理公钥格式
                public_key_bytes = public_key.public_bytes(
                    encoding=serialization.Encoding.PEM,
                    format=serialization.PublicFormat.SubjectPublicKeyInfo,
                )
    
                # 去除PEM头尾并转为Base64字符串
                private_key_str = private_key_bytes.decode("utf-8")
                private_key_str = private_key_str.replace(
                    "-----BEGIN PRIVATE KEY-----\n", ""
                )
                private_key_str = private_key_str.replace("-----END PRIVATE KEY-----\n", "")
                private_key_str = private_key_str.replace(
                    "-----BEGIN RSA PRIVATE KEY-----\n", ""
                )
                private_key_str = private_key_str.replace(
                    "-----END RSA PRIVATE KEY-----\n", ""
                )
                private_key_str = private_key_str.replace("\n", "")
    
                public_key_str = public_key_bytes.decode("utf-8")
                public_key_str = public_key_str.replace("-----BEGIN PUBLIC KEY-----\n", "")
                public_key_str = public_key_str.replace("-----END PUBLIC KEY-----\n", "")
                public_key_str = public_key_str.replace("\n", "")
    
            elif algorithm.upper() == "SM2":
                if format.lower() != "pkcs8":
                    return {
                        "message": "SM2密钥只支持生成PKCS8格式",
                        "privateKey": None,
                        "publicKey": None,
                        "privateCert": None,
                        "publicCert": None,
                    }
                private_key_str, public_key_str = CertUtils.generate_sm2_key_pair()
            else:
                return {
                    "message": f"不支持的密钥算法: {algorithm}",
                    "privateKey": None,
                    "publicKey": None,
                    "privateCert": None,
                    "publicCert": None,
                }
    
            # 如果需要保存到文件
            private_cert_path = None
            public_cert_path = None
    
            if storage_type.lower() == "file":
                # 创建目录
                key_dir = "./keys/"
                os.makedirs(key_dir, exist_ok=True)
    
                algorithm_name = "RSA2048" if algorithm.upper() == "RSA" else "SM2"
    
                # 保存私钥
                private_cert_path = os.path.join(key_dir, f"应用私钥{algorithm_name}.txt")
                with open(private_cert_path, "w", encoding="utf-8") as f:
                    f.write(private_key_str)
    
                # 保存公钥
                public_cert_path = os.path.join(key_dir, f"应用公钥{algorithm_name}.txt")
                with open(public_cert_path, "w", encoding="utf-8") as f:
                    f.write(public_key_str)
    
            return {
                "message": "密钥对生成成功"
                + (",并已保存到文件" if storage_type.lower() == "file" else ""),
                "privateKey": private_key_str,
                "publicKey": public_key_str,
                "privateCert": private_cert_path,
                "publicCert": public_cert_path,
            }
    
        except Exception as e:
            return {
                "message": f"生成密钥对失败: {str(e)}",
                "privateKey": None,
                "publicKey": None,
                "privateCert": None,
                "publicCert": None,
            }
  • The tool is registered via the @mcp.tool() decorator on the yeepay_yop_gen_key_pair function.
    @mcp.tool()
    def yeepay_yop_gen_key_pair(
        algorithm: str = "RSA", key_format: str = "pkcs8", storage_type: str = "file"
    ) -> Dict[str, Any]:
        """
        根据密钥算法生成非对称加密的密钥对(公钥和私钥),并保存到本地路径
    
        参数:
            algorithm: 密钥算法,可选值为 "RSA" 或 "SM2",默认为 "RSA"
            key_format: 密钥格式,可选值为 "pkcs8"或"pkcs1",默认为 "pkcs8"
            storage_type: 密钥存储类型,"file"或"string",默认为 "file"
        """
        return gen_key_pair(
            algorithm=algorithm, format=key_format, storage_type=storage_type
        )
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. It states the tool saves keys to a local path, but does not disclose potential side effects (e.g., overwriting existing files), permissions required, or whether generation occurs locally or involves external calls. Basic transparency but lacks depth.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise: one sentence explaining the purpose followed by a succinct parameter list. It is front-loaded with the main action. However, the parameter descriptions are minimal (just enumerating options without explanations), which could be improved without adding much length.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

The tool saves to local path and has an output schema, but the description does not mention what the output represents (e.g., file paths or success status). For a simple generation tool, it is adequate but misses the chance to specify return semantics. Given the context signals (3 params, no nested objects), the description is somewhat complete but could be more explicit about outputs.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description adds value by listing three parameters with possible values and defaults (algorithm: RSA/SM2, key_format: pkcs8/pkcs1, storage_type: file/string). However, it does not explain the semantics of each option (e.g., difference between pkcs8 and pkcs1), which would further aid the agent. Still, it provides meaning beyond the bare schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states it generates an asymmetric encryption key pair (public and private) based on an algorithm and saves to a local path. This specific verb+resource distinguishes it from sibling tools like yeepay_yop_download_cert or yeepay_yop_parse_certificates.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no explicit guidance on when to use this tool vs alternatives. While the purpose is clear, there is no mention of prerequisites, scenarios, or exclusions. Given it is a key generation tool, usage is implied but not explicitly guided.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/yop-platform/yop-mcp'

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