- Characters can carry aliases and be linked as the same underlying person (canonical SameCharacterAsId, optional reveal chapter/note), surfaced through the API, MCP tools, agent toolset, and web UI. - Characters page redesigned as a filterable/sortable table (name+ aliases, role, importance, occupation, tags) instead of a sidebar list, to stay usable as the cast grows. - Beats can be moved between chapters (BeatService.MoveAsync + MCP/ agent tool + endpoint). - Add a keyboard-shortcuts help overlay (HelpButton/HelpOverlayContext) wired into the project layout. - CLAUDE.md: require every frontend component to carry a unique id attribute; apply it to CharacterMultiSelect and MarkdownEditor.
102 lines
3.8 KiB
C#
102 lines
3.8 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.Logging;
|
|
using ModelContextProtocol.Protocol;
|
|
|
|
namespace Novelly.Mcp;
|
|
|
|
public class NovelApiClient(HttpClient http, ILogger<NovelApiClient> logger)
|
|
{
|
|
private static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web)
|
|
{
|
|
WriteIndented = true
|
|
};
|
|
|
|
public Task<CallToolResult> GetAsync(string path, CancellationToken ct = default) => SendAsync(new HttpRequestMessage(HttpMethod.Get, path), ct);
|
|
|
|
public Task<CallToolResult> PostAsync(string path, object body, CancellationToken ct = default) =>
|
|
SendAsync(new HttpRequestMessage(HttpMethod.Post, path)
|
|
{
|
|
Content = JsonContent.Create(body, options: Options)
|
|
}, ct);
|
|
|
|
public Task<CallToolResult> PatchAsync(string path, object body, CancellationToken ct = default) =>
|
|
SendAsync(new HttpRequestMessage(HttpMethod.Patch, path)
|
|
{
|
|
Content = JsonContent.Create(body, options: Options)
|
|
}, ct);
|
|
|
|
public Task<CallToolResult> PutAsync(string path, object body, CancellationToken ct = default) =>
|
|
SendAsync(new HttpRequestMessage(HttpMethod.Put, path)
|
|
{
|
|
Content = JsonContent.Create(body, options: Options)
|
|
}, ct);
|
|
|
|
public Task<CallToolResult> DeleteAsync(string path, CancellationToken ct = default) => SendAsync(new HttpRequestMessage(HttpMethod.Delete, path), ct);
|
|
|
|
private async Task<CallToolResult> SendAsync(HttpRequestMessage request, CancellationToken ct)
|
|
{
|
|
HttpResponseMessage response;
|
|
try
|
|
{
|
|
response = await http.SendAsync(request, ct);
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
logger.LogError(ex, "Could not reach the Novelly API at {BaseAddress}", http.BaseAddress);
|
|
return Error($"Could not reach the Novelly API at {http.BaseAddress}. Is it running? ({ex.Message})");
|
|
}
|
|
|
|
var body = await response.Content.ReadAsStringAsync(ct);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return Ok(string.IsNullOrWhiteSpace(body) ? "{\"ok\":true}" : Prettify(body));
|
|
}
|
|
|
|
var detail = TryReadProblemDetail(body) ?? body;
|
|
return Error(response.StatusCode switch
|
|
{
|
|
HttpStatusCode.Unauthorized => $"Not permitted: the Novelly API rejected the service api key. Set NOVELLY_API_KEY to match the API's Auth:ServiceApiKey. ({detail})",
|
|
HttpStatusCode.Forbidden => $"Not permitted: {detail}",
|
|
HttpStatusCode.NotFound => $"Not found: {detail}",
|
|
HttpStatusCode.BadRequest => $"Rejected: {detail}",
|
|
_ => $"API returned {(int)response.StatusCode}: {detail}"
|
|
});
|
|
}
|
|
|
|
private static CallToolResult Ok(string text) =>
|
|
new() { Content = [new TextContentBlock { Text = text }] };
|
|
|
|
private static CallToolResult Error(string message) =>
|
|
new() { Content = [new TextContentBlock { Text = message }], IsError = true };
|
|
|
|
private string Prettify(string json)
|
|
{
|
|
try
|
|
{
|
|
return JsonSerializer.Serialize(JsonSerializer.Deserialize<JsonElement>(json), Options);
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
logger.LogWarning(ex, "Response body was not valid JSON; returning it unformatted");
|
|
return json;
|
|
}
|
|
}
|
|
|
|
private string? TryReadProblemDetail(string body)
|
|
{
|
|
try
|
|
{
|
|
var problem = JsonSerializer.Deserialize<JsonElement>(body);
|
|
return problem.TryGetProperty("detail", out var detail) ? detail.GetString() : null;
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
logger.LogWarning(ex, "Error response body was not valid JSON problem details");
|
|
return null;
|
|
}
|
|
}
|
|
}
|