in src/Commands/Postgres/Database/DatabaseListCommand.cs [13:47]
public sealed class DatabaseListCommand(ILogger<DatabaseListCommand> logger) : BaseServerCommand<DatabaseListArguments>(logger)
{
protected override string GetCommandName() => "list";
protected override string GetCommandDescription() => "Lists all databases in the PostgreSQL server.";
[McpServerTool(Destructive = false, ReadOnly = true)]
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult)
{
try
{
var args = BindArguments(parseResult);
if (!await ProcessArguments(context, args))
{
return context.Response;
}
IPostgresService pgService = context.GetService<IPostgresService>() ?? throw new InvalidOperationException("PostgreSQL service is not available.");
List<string> databases = await pgService.ListDatabasesAsync(args.Subscription!, args.ResourceGroup!, args.User!, args.Server!);
context.Response.Results = databases?.Count > 0 ?
ResponseResult.Create(
new DatabaseListCommandResult(databases),
PostgresJsonContext.Default.DatabaseListCommandResult) :
null;
}
catch (Exception ex)
{
_logger.LogError(ex, "An exception occurred listing databases.");
HandleException(context.Response, ex);
}
return context.Response;
}
internal record DatabaseListCommandResult(List<string> Databases);
}