csharp5e452b8e
| 1 | using System.Text.Json; |
| 2 | |
| 3 | namespace DotnetDebugMcp; |
| 4 | |
| 5 | internal static class McpArgumentHelpers |
| 6 | { |
| 7 | internal static bool TryGetString(IReadOnlyDictionary<string, JsonElement> args, string key, out string? value) |
| 8 | { |
| 9 | value = null; |
| 10 | if (!args.TryGetValue(key, out var el) || el.ValueKind != JsonValueKind.String) |
| 11 | return false; |
| 12 | value = el.GetString(); |
| 13 | return true; |
| 14 | } |
| 15 | |
| 16 | internal static bool TryGetPropString(JsonElement el, string key, out string? value) |
| 17 | { |
| 18 | value = null; |
| 19 | if (!el.TryGetProperty(key, out var prop) || prop.ValueKind != JsonValueKind.String) |
| 20 | return false; |
| 21 | value = prop.GetString(); |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | internal static bool TryGetPropInt(JsonElement el, string key, out int value) |
| 26 | { |
| 27 | value = 0; |
| 28 | if (!el.TryGetProperty(key, out var prop) || prop.ValueKind != JsonValueKind.Number) |
| 29 | return false; |
| 30 | return prop.TryGetInt32(out value); |
| 31 | } |
| 32 | |
| 33 | /// <summary>Опциональное целое из аргументов MCP; вне [min, max] — зажать.</summary> |
| 34 | internal static int GetOptionalClampedInt32( |
| 35 | IReadOnlyDictionary<string, JsonElement> args, |
| 36 | string key, |
| 37 | int defaultValue, |
| 38 | int min, |
| 39 | int max) |
| 40 | { |
| 41 | if (!args.TryGetValue(key, out var el) || el.ValueKind != JsonValueKind.Number || !el.TryGetInt32(out var v)) |
| 42 | return defaultValue; |
| 43 | if (v < min) |
| 44 | return min; |
| 45 | if (v > max) |
| 46 | return max; |
| 47 | return v; |
| 48 | } |
| 49 | } |
| 50 | |