54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
namespace Sozsoft.Mcp;
|
|
|
|
/// <summary>
|
|
/// Aracin modelle konusarak cozulecek reddi: eksik ya da hatali girdi, durum kurali, eksik yetki.
|
|
/// Istisna olarak atilmaz — bu durum aracin beklenen cevabidir; atilan istisna debugger'i durdurur
|
|
/// ve SDK'da hata logu uretir. Istemciye hata sonucu (<c>IsError</c>) olarak gider, model mesaji
|
|
/// okuyup girdiyi duzeltir ve tekrar dener.
|
|
/// </summary>
|
|
public sealed record McpProblem(string Message)
|
|
{
|
|
public static McpProblem From(IStringLocalizer localizer, string name, params object[] arguments)
|
|
=> new(localizer[name, arguments]);
|
|
}
|
|
|
|
/// <summary>Aracin degeri ya da <see cref="McpProblem"/>'i; ikisi birden olmaz.</summary>
|
|
public readonly struct McpOutcome<T>
|
|
{
|
|
private McpOutcome(T? value, McpProblem? problem)
|
|
{
|
|
Value = value;
|
|
Problem = problem;
|
|
}
|
|
|
|
public T? Value { get; }
|
|
|
|
public McpProblem? Problem { get; }
|
|
|
|
public bool TryGetValue([MaybeNullWhen(false)] out T value, [NotNullWhen(false)] out McpProblem? problem)
|
|
{
|
|
value = Value!;
|
|
problem = Problem;
|
|
return problem is null;
|
|
}
|
|
|
|
public static implicit operator McpOutcome<T>(T value) => new(value, null);
|
|
|
|
public static implicit operator McpOutcome<T>(McpProblem problem) => new(default, problem);
|
|
}
|
|
|
|
/// <summary>Yetki reddi mesajinda yetkinin gorunen adi ve kodu; rol ekraninda aranabilsin diye kod her zaman yazilir.</summary>
|
|
public static class McpPermissionText
|
|
{
|
|
public static string Describe(IStringLocalizer localizer, string permissionName)
|
|
{
|
|
var displayName = localizer[permissionName];
|
|
|
|
return displayName.ResourceNotFound || displayName.Value == permissionName
|
|
? permissionName
|
|
: $"{displayName.Value} ({permissionName})";
|
|
}
|
|
}
|