using LazyBear.Confluence; using ModelContextProtocol.Server; namespace LazyBear.Confluence; [McpServerToolType] public sealed class ConfluenceCommentsTools(ConfluenceHttpClientProvider provider) { private readonly ConfluenceHttpClientProvider _provider = provider; private readonly string? _initializationError = provider.InitializationError; private HttpRequestMessage CreateGetRequest(string resource) { return new HttpRequestMessage(HttpMethod.Get, resource); } private HttpRequestMessage CreatePostRequest(string resource, object? body = null) { var request = new HttpRequestMessage(HttpMethod.Post, resource); request.Content = new StringContent(JsonSerializer.Serialize(body), System.Text.Encoding.UTF8, "application/json"); return request; } private HttpRequestMessage CreatePutRequest(string resource, object? body = null) { var request = new HttpRequestMessage(HttpMethod.Put, resource); request.Content = new StringContent(JsonSerializer.Serialize(body), System.Text.Encoding.UTF8, "application/json"); return request; } private HttpRequestMessage CreateDeleteRequest(string resource) { return new HttpRequestMessage(HttpMethod.Delete, resource); } [McpServerTool, Description("Список комментариев Confluence страницы")] public async Task ListCommentsAsync( [Description("ID страницы")] string pageId, [Description("Параметры")] string? expand = null, [Description("Максимум комментариев")] int? limit = 20, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(pageId)) { return "ID страницы Confluence не задан."; } var resource = $"rest/api/cloud/content/{pageId}/comment"; var request = CreateGetRequest(resource); return "Comments: [" + LimitToString(limit) + "]"; } [McpServerTool, Description("Добавить комментарий Confluence")] public async Task AddCommentAsync( [Description("ID страницы")] string pageId, [Description("Комментарий")] string body, [Description("Имя пользователя")] string? avatar = null, [Description("Тип комментария")] string? type = null, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(pageId) || string.IsNullOrWhiteSpace(body)) { return "pageId или body не задан."; } var requestBody = new { body = body, avatarUrl = avatar, type = type ?? "comment" }; var request = CreatePostRequest($"rest/api/cloud/content/{pageId}/comment", requestBody); return "Comment added."; } [McpServerTool, Description("Удалить комментарий Confluence")] public async Task DeleteCommentAsync( [Description("ID страницы")] string pageId, [Description("ID комментария")] string commentId, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(pageId) || string.IsNullOrWhiteSpace(commentId)) { return "pageId или commentId не задан."; } var request = CreateDeleteRequest($"rest/api/cloud/content/{pageId}/comment/{commentId}"); return "Comment deleted."; } private string LimitToString(int? limit) { return limit.HasValue && limit <= 0 ? "0" : limit.ToString(); } }