generate-password.cs

Generates secure passwords with customizable length and complexity.

password security utilities
View on GitHub

Available Tools

GeneratePassword

Generates a password with specified length and complexity

Source Code

#:package Microsoft.Extensions.Hosting@9.0.8
#:package ModelContextProtocol@0.3.0-preview.3
#:property PublishAot=false // disable AOT because apparently it enabled by default and WithToolsFromAssembly uses reflection
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;

var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
    // Configure all logs to go to stderr
    consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});

// Register the MCP server
builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();

// Build and run the MCP Server Application
await builder.Build().RunAsync();

//====== TOOLS ======

public record GeneratePasswordResult(string Password);

[McpServerToolType]
public static class PasswordTools
{
    [McpServerTool, Description("Generates a password with specified length and complexity")]
    public static GeneratePasswordResult GeneratePassword(
        [Description("Password length (default: 12)")] int length = 12, 
        [Description("Include symbols (default: true)")] bool includeSymbols = true, 
        [Description("Include numbers (default: true)")] bool includeNumbers = true)
    {
        if (length < 4 || length > 256)
            throw new ArgumentException("Password length must be between 4 and 256 characters");
            
        const string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        const string numbers = "0123456789";
        const string symbols = "!@#$%^&*()_+-=[]{}|;:,.<>?";

        var chars = letters;
        if (includeNumbers) chars += numbers;
        if (includeSymbols) chars += symbols;

        var password = new string(Enumerable.Repeat(chars, length)
            .Select(s => s[Random.Shared.Next(s.Length)]).ToArray());
            
        return new GeneratePasswordResult(password);
    }
}

How to Run

1. Save the server code

Copy the code above and save it as generate-password.cs

2. Configure your MCP client

Add this configuration to your .mcp.json file:

Note: This example is for Visual Studio and similar clients. For Claude Code, use mcpServers instead of servers.

{
  "servers": {
    "generate-password": {
      "type": "stdio",
      "command": "dotnet",
      "args": ["run", "C:\\path\\to\\generate-password.cs"]
    }
  }
}

3. Set environment variables

Configure any required environment variables (see sidebar)

4. Restart your LLM client

Restart Claude Desktop, Continue, or your preferred MCP client

Statistics

Version 1.1.0
License MIT
Updated 12/10/2025

Author

XAKPC Dev Labs

Maintained by the AnyMCP community