in src/Commands/Cosmos/DatabaseListCommand.cs [14:62]
public sealed class DatabaseListCommand(ILogger<DatabaseListCommand> logger) : BaseCosmosCommand<DatabaseListArguments>()
{
private readonly ILogger<DatabaseListCommand> _logger = logger;
protected override string GetCommandName() => "list";
protected override string GetCommandDescription() =>
"""
List all databases in a Cosmos DB account. This command retrieves and displays all databases available
in the specified Cosmos DB account. Results include database names and are returned as a JSON array.
""";
[McpServerTool(Destructive = false, ReadOnly = true)]
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult)
{
var args = BindArguments(parseResult);
try
{
if (!await ProcessArguments(context, args))
{
return context.Response;
}
var cosmosService = context.GetService<ICosmosService>();
var databases = await cosmosService.ListDatabases(
args.Account!,
args.Subscription!,
args.AuthMethod ?? AuthMethod.Credential,
args.Tenant,
args.RetryPolicy);
context.Response.Results = databases?.Count > 0 ?
ResponseResult.Create(
new DatabaseListCommandResult(databases),
CosmosJsonContext.Default.DatabaseListCommandResult) :
null;
}
catch (Exception ex)
{
_logger.LogError(ex, "An exception occurred listing databases. Account: {Account}.", args.Account);
HandleException(context.Response, ex);
}
return context.Response;
}
internal record DatabaseListCommandResult(List<string> Databases);
}