using System;
using System.Collections.Generic;
using System.Linq;
namespace Sozsoft.Mcp;
///
/// Bir aracin bir sunucu kaydina gore acik olup olmadigina karar veren kural. Karar iki yerde
/// kullanilir: arac listesi suzulurken ve arac cagrilirken. Listeyi suzmek tek basina bir
/// yetki siniri degildir — istemci listede olmayan bir araci da cagirabilir.
///
public static class McpToolPolicy
{
/// Istegin kiracisina dusen aktif bir sunucu kaydi var mi.
/// Istek bir kiraci kullanicisindan mi geliyor; o zaman yalnizca okuma araclari acilir.
/// Acik arac adlari; bos liste "tumu acik" demektir.
public static bool IsAllowed(
bool hasServerRecord,
bool isTenant,
IReadOnlyCollection allowedTools,
string toolName)
{
if (McpToolNames.AlwaysAvailableTools.Contains(toolName, StringComparer.Ordinal))
{
return true;
}
if (isTenant && !McpToolNames.TenantTools.Contains(toolName, StringComparer.Ordinal))
{
return false;
}
if (!hasServerRecord)
{
return false;
}
if (allowedTools.Count == 0)
{
return true;
}
return allowedTools.Contains(toolName, StringComparer.OrdinalIgnoreCase);
}
public static IReadOnlyList VisibleTools(
bool hasServerRecord,
bool isTenant,
IReadOnlyCollection allowedTools)
=> [.. McpToolNames.All.Where(name => IsAllowed(hasServerRecord, isTenant, allowedTools, name))];
}