Files
AX-Copilot-Codex/src/AxCopilot/Services/Agent/McpListResourcesTool.cs

62 lines
2.4 KiB
C#

using System.Text.Json;
namespace AxCopilot.Services.Agent;
/// <summary>MCP 서버의 리소스 목록을 조회합니다.</summary>
public class McpListResourcesTool : IAgentTool
{
private readonly Func<IReadOnlyCollection<McpClientService>> _getClients;
public McpListResourcesTool(Func<IReadOnlyCollection<McpClientService>> getClients)
{
_getClients = getClients;
}
public string Name => "mcp_list_resources";
public string Description => "연결된 MCP 서버들의 리소스 목록을 조회합니다. server_name을 지정하면 특정 서버만 조회합니다.";
public ToolParameterSchema Parameters => new()
{
Properties = new()
{
["server_name"] = new ToolProperty
{
Type = "string",
Description = "조회할 MCP 서버 이름. 생략하면 모든 연결된 서버를 조회합니다."
}
}
};
public async Task<ToolResult> ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct = default)
{
var serverName = args.ValueKind == JsonValueKind.Object &&
args.TryGetProperty("server_name", out var serverProp)
? serverProp.GetString() ?? ""
: "";
var clients = _getClients()
.Where(c => c.IsConnected &&
(string.IsNullOrWhiteSpace(serverName) || string.Equals(c.ServerName, serverName, StringComparison.OrdinalIgnoreCase)))
.ToList();
if (clients.Count == 0)
return ToolResult.Fail(string.IsNullOrWhiteSpace(serverName)
? "연결된 MCP 서버가 없습니다."
: $"MCP 서버 '{serverName}'를 찾지 못했습니다.");
var lines = new List<string>();
foreach (var client in clients)
{
var resources = await client.ListResourcesAsync(ct);
lines.Add($"[{client.ServerName}] {resources.Count}개 리소스");
foreach (var resource in resources.Take(20))
{
var desc = string.IsNullOrWhiteSpace(resource.Description) ? "" : $" - {resource.Description}";
var mime = string.IsNullOrWhiteSpace(resource.MimeType) ? "" : $" ({resource.MimeType})";
lines.Add($"- {resource.Name} :: {resource.Uri}{mime}{desc}");
}
}
return ToolResult.Ok(string.Join(Environment.NewLine, lines));
}
}