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@10.0.11
#:package ModelContextProtocol@2.2.0
#:property PublishAot=false
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

Put it in your project, in a .mcp-servers/ directory. Copy the code above, or download it from /servers/generate-password/generate-password.cs. Commit it: the file is the whole server.

2. Build it once

The first build restores the packages. It takes longer than the startup timeout of most clients, so run it by hand before you go on:

dotnet build .mcp-servers/generate-password.cs -v q

This adds no bin or obj directory to your project. A file-based app is not a project, so the SDK caches the build under your temp directory instead. Your .gitignore needs no new line.

3. Configure your MCP client

Claude Code and Codex read different files, in different formats. Pick yours, add the snippet to your project root, and commit it. Everyone who clones the project then gets the server. Don't paste one client's format into the other's file.

Claude Code — .mcp.json

Note: This example also covers Claude Desktop, Cursor, and Windsurf. Visual Studio and VS Code use servers instead of mcpServers.

{
  "mcpServers": {
    "generate-password": {
      "type": "stdio",
      "command": "dotnet",
      "args": ["run", "./.mcp-servers/generate-password.cs", "-v", "q"]
    }
  }
}

claude mcp add generate-password --scope project -- dotnet run ./.mcp-servers/generate-password.cs -v q writes the same entry for you. Full details: /install/claude-code.md.

Codex — .codex/config.toml

[mcp_servers.generate-password]
command = "dotnet"
args = [
    "run",
    "./.mcp-servers/generate-password.cs",
    "-v",
    "q",
]
startup_timeout_sec = 60

Codex needs the project trusted. It ignores a project .codex/config.toml unless your personal ~/.codex/config.toml holds [projects.'<absolute path>'] with trust_level = "trusted" — and there is no error message when it doesn't. On Windows use the plain C:\Users\... path, never the extended \\?\C:\... form. Don't commit that entry. See /install/codex.md.

Both paths are relative to the project, so they work for everyone who clones it, on every operating system. -v q is required in both: standard output carries the JSON-RPC stream, and without it the build output can reach that stream and break the connection.

4. Restart your LLM client

Restart your client in the project directory, then check that generate-password shows as connected. Claude Code asks you to approve a project server the first time it reads .mcp.json. Codex reads its configuration at startup only, so restart it and open a new chat.

Statistics

Version 1.1.0
License MIT

Author

XAKPC Dev Labs

Maintained by the AnyMCP community