generate_password
Create secure random passwords with customizable length and optional special characters for enhanced account protection.
Instructions
Generate a random password with specified length, optionally including special characters.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| length | No | The length of the password to generate (between 8 and 64 characters). | |
| use_special_chars | No | Include special characters in the password. |
Implementation Reference
- src/pymcp/server.py:120-174 (handler)Handler function for the 'generate_password' tool. Generates a secure random password ensuring complexity requirements are met (lowercase, uppercase, digits, optional special chars). Includes input schema via Annotated Fields with Pydantic validation.async def generate_password( self, ctx: Context, length: Annotated[ int, Field( default=12, ge=8, le=64, description="The length of the password to generate (between 8 and 64 characters).", ), ] = 12, use_special_chars: Annotated[ bool, Field( default=False, description="Include special characters in the password.", ), ] = False, ) -> ToolResult: """Generate a random password with specified length, optionally including special characters.""" """The password will meet the complexity requirements of at least one lowercase letter, one uppercase letter, and two digits. If special characters are included, it will also contain at least one such character. Until the password meets these requirements, it will keep regenerating. This is a simple example of a tool that can be used to generate passwords. It is not intended for production use.""" characters = string.ascii_letters + string.digits if use_special_chars: characters += string.punctuation password_generation_attempts = 0 while True: password = "".join(secrets.choice(characters) for _ in range(length)) password_generation_attempts += 1 if ( any(c.islower() for c in password) and any(c.isupper() for c in password) and sum(c.isdigit() for c in password) >= 2 and (not use_special_chars or any(c in string.punctuation for c in password)) ): await ctx.info("Generated password meets complexity requirements.") break else: # Exclude from coverage because this may not always happen in tests await ctx.warning( # pragma: no cover f"Re-generating since the generated password did not meet complexity requirements: {password}" ) return self.get_tool_result( result=password, metadata={ "generate_password": { "length_satisfied": len(password) == length, "character_set": characters, "generation_attempts": password_generation_attempts, } }, )
- src/pymcp/server.py:57-61 (registration)Registration of the 'generate_password' tool in the PyMCP class's tools list, specifying name, tags, and read-only annotation.{ "fn": "generate_password", "tags": ["password-generation", "example"], "annotations": {"readOnlyHint": True}, },
- src/pymcp/server.py:120-139 (schema)Input schema defined via Pydantic Annotated parameters with Field descriptions, defaults, and constraints for length and use_special_chars.async def generate_password( self, ctx: Context, length: Annotated[ int, Field( default=12, ge=8, le=64, description="The length of the password to generate (between 8 and 64 characters).", ), ] = 12, use_special_chars: Annotated[ bool, Field( default=False, description="Include special characters in the password.", ), ] = False, ) -> ToolResult: