-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathKeyValueSetCommand.cs
More file actions
88 lines (78 loc) · 3.31 KB
/
Copy pathKeyValueSetCommand.cs
File metadata and controls
88 lines (78 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.Mcp.Tools.AppConfig.Options;
using Azure.Mcp.Tools.AppConfig.Options.KeyValue;
using Azure.Mcp.Tools.AppConfig.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Mcp.Core.Commands;
using Microsoft.Mcp.Core.Extensions;
using Microsoft.Mcp.Core.Models.Command;
namespace Azure.Mcp.Tools.AppConfig.Commands.KeyValue;
[CommandMetadata(
Id = "a89086eb-acf4-4168-9d32-de5cd7384030",
Name = "set",
Title = "Set App Configuration Key-Value Setting",
Description = """
Set a key-value setting in an App Configuration store. This command creates or updates a key-value setting
with the specified value. If no label is specified, the default label will be used.
""",
Destructive = true,
Idempotent = true,
OpenWorld = false,
ReadOnly = false,
Secret = false,
LocalRequired = false)]
public sealed class KeyValueSetCommand(ILogger<KeyValueSetCommand> logger, IAppConfigService appConfigService)
: BaseKeyValueCommand<KeyValueSetOptions>()
{
private const string CommandTitle = "Set App Configuration Key-Value Setting";
private readonly ILogger<KeyValueSetCommand> _logger = logger;
private readonly IAppConfigService _appConfigService = appConfigService;
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
command.Options.Add(AppConfigOptionDefinitions.Value);
command.Options.Add(AppConfigOptionDefinitions.Tags);
}
protected override KeyValueSetOptions BindOptions(ParseResult parseResult)
{
var options = base.BindOptions(parseResult);
options.Value = parseResult.GetValueOrDefault<string>(AppConfigOptionDefinitions.Value.Name);
options.Tags = parseResult.GetValueOrDefault<string[]>(AppConfigOptionDefinitions.Tags.Name);
return options;
}
[McpServerTool(Destructive = true, ReadOnly = false, Title = CommandTitle)]
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult, CancellationToken cancellationToken)
{
if (!Validate(parseResult.CommandResult, context.Response).IsValid)
{
return context.Response;
}
var options = BindOptions(parseResult);
try
{
await _appConfigService.SetKeyValue(
options.Account!,
options.Key!,
options.Value!,
options.Subscription!,
options.Tenant,
options.RetryPolicy,
options.Label,
options.ContentType,
options.Tags,
cancellationToken);
context.Response.Results = ResponseResult.Create(
new(options.Key, options.Value, options.Label, options.ContentType, options.Tags),
AppConfigJsonContext.Default.KeyValueSetCommandResult
);
}
catch (Exception ex)
{
_logger.LogError(ex, "An exception occurred setting value. Key: {Key}.", options.Key);
HandleException(context, ex);
}
return context.Response;
}
internal record KeyValueSetCommandResult(string? Key, string? Value, string? Label, string? ContentType = null, string[]? Tags = null);
}