Generates secure passwords with customizable length and complexity.
Generates a password with specified length and complexity
#:package [email protected]
#:package [email protected]
#: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);
}
}
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.
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.
Add this to .mcp.json in your project root, and commit it. Everyone who clones the project then gets the server.
Note: This example is for Claude Code, 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"]
}
}
}
The path is relative to the project, so it works for everyone who clones it, on every operating system. -v q is required: standard output carries the JSON-RPC stream, and without it the build output can reach that stream and break the connection.
With Claude Code, claude mcp add generate-password --scope project -- dotnet run ./.mcp-servers/generate-password.cs -v q writes the same entry for you. Add --scope user instead, with an absolute path, to install it for every project.
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.
XAKPC Dev Labs
Maintained by the AnyMCP community