sozsoft-platform/api/modules/Sozsoft.Mcp/Sozsoft.Mcp.Domain.Shared/McpToolPolicy.cs

52 lines
1.7 KiB
C#
Raw Permalink Normal View History

2026-09-17 09:01:14 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace Sozsoft.Mcp;
/// <summary>
/// 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.
/// </summary>
public static class McpToolPolicy
{
/// <param name="hasServerRecord">Istegin kiracisina dusen aktif bir sunucu kaydi var mi.</param>
2026-09-18 13:47:23 +00:00
/// <param name="isTenant">Istek bir kiraci kullanicisindan mi geliyor; o zaman yalnizca okuma araclari acilir.</param>
/// <param name="allowedTools">Acik arac adlari; bos liste "tumu acik" demektir.</param>
2026-09-17 09:01:14 +00:00
public static bool IsAllowed(
bool hasServerRecord,
2026-09-18 13:47:23 +00:00
bool isTenant,
2026-09-17 09:01:14 +00:00
IReadOnlyCollection<string> allowedTools,
string toolName)
{
if (McpToolNames.AlwaysAvailableTools.Contains(toolName, StringComparer.Ordinal))
{
return true;
}
2026-09-18 13:47:23 +00:00
if (isTenant && !McpToolNames.TenantTools.Contains(toolName, StringComparer.Ordinal))
2026-09-17 09:01:14 +00:00
{
return false;
}
2026-09-18 13:47:23 +00:00
if (!hasServerRecord)
2026-09-17 09:01:14 +00:00
{
return false;
}
if (allowedTools.Count == 0)
{
return true;
}
return allowedTools.Contains(toolName, StringComparer.OrdinalIgnoreCase);
}
public static IReadOnlyList<string> VisibleTools(
bool hasServerRecord,
2026-09-18 13:47:23 +00:00
bool isTenant,
2026-09-17 09:01:14 +00:00
IReadOnlyCollection<string> allowedTools)
2026-09-18 13:47:23 +00:00
=> [.. McpToolNames.All.Where(name => IsAllowed(hasServerRecord, isTenant, allowedTools, name))];
2026-09-17 09:01:14 +00:00
}