public sealed class GetSchemaCommand()

in src/Commands/Postgres/Table/GetSchemaCommand.cs [15:78]


public sealed class GetSchemaCommand(ILogger<GetSchemaCommand> logger) : BaseDatabaseCommand<GetSchemaArguments>(logger)
{
    private readonly Option<string> _tableOption = ArgumentDefinitions.Postgres.Table.ToOption();
    protected override string GetCommandName() => "schema";

    protected override string GetCommandDescription() =>
        "Retrieves the schema of a specified table in a PostgreSQL database.";

    protected override void RegisterOptions(Command command)
    {
        base.RegisterOptions(command);
        command.AddOption(_tableOption);
    }

    protected override void RegisterArguments()
    {
        base.RegisterArguments();
        AddArgument(CreateTableArgument());
    }

    protected override GetSchemaArguments BindArguments(ParseResult parseResult)
    {
        var args = base.BindArguments(parseResult);
        args.Table = parseResult.GetValueForOption(_tableOption);
        return args;
    }

    [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> schema = await pgService.GetTableSchemaAsync(args.Subscription!, args.ResourceGroup!, args.User!, args.Server!, args.Database!, args.Table!);
            context.Response.Results = schema?.Count > 0 ?
                ResponseResult.Create(
                    new GetSchemaCommandResult(schema),
                    PostgresJsonContext.Default.GetSchemaCommandResult) :
                null;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "An exception occurred retrieving the schema for table");
            HandleException(context.Response, ex);
        }

        return context.Response;
    }

    private static ArgumentBuilder<GetSchemaArguments> CreateTableArgument() =>
        ArgumentBuilder<GetSchemaArguments>
            .Create(ArgumentDefinitions.Postgres.Table.Name, ArgumentDefinitions.Postgres.Table.Description)
            .WithValueAccessor(args => args.Table ?? string.Empty)
            .WithIsRequired(true);

    internal record GetSchemaCommandResult(List<string> Schema);
}