Compare commits

...
4 Commits
Author SHA1 Message Date
James Wampler e1df79b31e Update docs and solution file for the stdio MCP server's removal
CI / deploy (push) Successful in 10s
CI / build-and-push (push) Successful in 47s
The prior commit's git add silently dropped these five files because
one path in the same invocation didn't exist. Docs, solution file, and
example config still needed the paired update: Novelly.slnx drops the
Novelly.Mcp project entry, README and CLAUDE.md describe MCP as an
in-API HTTP endpoint instead of a stdio binary, .mcp.json.example uses
the type: http shape, and outline-importer.md's tool references are
fixed to real tool names.
2026-08-21 11:00:25 -07:00
James Wampler 2d6bb9fc57 Delete the stdio MCP server now that the API serves /mcp directly
src/Novelly.Mcp was a separate stdio process, unbuilt by CI, that
looped back over HTTP to the same REST API the previous commit's /mcp
endpoint now calls in-process. Nothing else referenced it (not CI, not
Docker, not the AppHost), so removal is just the project, its solution
entry, and scripts/publish-mcp.sh.

Updates .mcp.json / .mcp.json.example to the type: http form, fixes
.claude/agents/outline-importer.md's already-stale tool references
(list_projects/create_project/etc. never existed; the real names are
list_novels/create_novel/etc.), and rewrites README + CLAUDE.md's
description of the MCP surface and how to verify it at runtime.
2026-08-21 10:59:52 -07:00
James Wampler ab773615f8 Serve the unified tool registry over MCP Streamable HTTP at /mcp
Adds ModelContextProtocol.AspNetCore and registers AddMcpServer with
WithListToolsHandler/WithCallToolHandler rather than 45 attribute
methods, so both handlers resolve the scoped NovelAgentToolset per
request and reuse its hand-built schemas directly instead of fighting
the SDK's delegate-based schema inference.

NovelMcpTools (src/Novelly.Api/Mcp/) is the adapter: it injects a
required novelId property into the advertised schema for tools that
need one and extracts it back out at call time, since the web agent
gets novelId ambiently from its route but an MCP client has no route
to supply it from.

/mcp inherits auth from the existing fallback policy (cookie or
X-Novelly-Api-Key) by adding no authorization metadata of its own —
chaining .RequireAuthorization() would apply the default,
cookie-only policy instead and break the API key. Verified end to end
against a running instance: initialize advertises capabilities.tools,
tools/list returns all 45 with novelId injected only where needed,
tool errors map to result.isError rather than a JSON-RPC error, and a
write (create_tag) round-trips correctly with the service user's
identity intact.
2026-08-21 10:56:24 -07:00
James Wampler 897fb442a1 Unify MCP and agent tool surfaces onto one registry in NovelAgentToolset
Fixes NovelAgentService continuing a conversation under the wrong
novel's route, since FindConversationAsync matched by id alone. Then
extends NovelAgentToolset to all 45 tools the stdio MCP server offered
(tag/location CRUD, character relationships, arc-stage beat pinning,
question editing, cross-novel novel listing/creation), tagging each
with whether it needs an explicit novel scope so a later MCP adapter
can inject it. Renames the toolset's 33 existing schemas from
snake_case to camelCase to match .NET/REST convention, since nothing
external consumes them.

Lays the groundwork to serve this same registry over MCP at /mcp and
retire the separate stdio Novelly.Mcp project (docs/plans/api/mcp_http_merge_plan.md).
2026-08-21 10:52:50 -07:00
28 changed files with 1076 additions and 972 deletions
+5 -5
View File
@@ -1,7 +1,7 @@
---
name: outline-importer
description: Imports an author's existing novel outline (chapters + character dossiers, in the Kingdom Sleeps folder format) into a Novelly project over the MCP server. Invoke explicitly with a source folder path — this agent does not run proactively.
tools: Read, Glob, Grep, Write, mcp__novelly__list_projects, mcp__novelly__get_project_brief, mcp__novelly__create_project, mcp__novelly__update_project_brief, mcp__novelly__list_chapters, mcp__novelly__get_chapter, mcp__novelly__create_chapter, mcp__novelly__update_chapter, mcp__novelly__get_chapter_outline, mcp__novelly__create_beat, mcp__novelly__update_beat, mcp__novelly__list_characters, mcp__novelly__get_character, mcp__novelly__create_character, mcp__novelly__update_character, mcp__novelly__get_character_arc, mcp__novelly__add_arc_stage, mcp__novelly__list_tags
tools: Read, Glob, Grep, Write, mcp__novelly__list_novels, mcp__novelly__get_novel_brief, mcp__novelly__create_novel, mcp__novelly__update_novel_brief, mcp__novelly__list_chapters, mcp__novelly__get_chapter, mcp__novelly__create_chapter, mcp__novelly__update_chapter, mcp__novelly__get_chapter_outline, mcp__novelly__create_beat, mcp__novelly__update_beat, mcp__novelly__list_characters, mcp__novelly__get_character, mcp__novelly__create_character, mcp__novelly__update_character, mcp__novelly__get_character_arc, mcp__novelly__add_arc_stage, mcp__novelly__list_tags
model: inherit
---
@@ -101,9 +101,9 @@ in your final report.
Do not skip ahead — each pass depends on ids the previous one minted. If you're picking up a
resumed run, jump straight to the first incomplete pass.
**0. Preflight.** Call `list_projects` to confirm the API is reachable at all — if this fails, stop
and tell the user to start the API (`ASPNETCORE_URLS=http://localhost:5080 dotnet run --project src/Novelly.Api`)
and that `.mcp.json` must point at a published `Novelly.Mcp` binary. Glob the source root for
**0. Preflight.** Call `list_novels` to confirm the API is reachable at all — if this fails, stop
and tell the user to start the API (`ASPNETCORE_URLS=http://localhost:5080 dotnet run --project src/Novelly.Api`).
Glob the source root for
`outline.md`, `outlines/*.md` or `chapters/*.md`, and `characters/*.md`. If `outline.md` is
missing, stop — that's the one file every pass depends on. Report the file counts found before
proceeding.
@@ -111,7 +111,7 @@ proceeding.
**1. Project.** Skip if `completedPasses` already has `"project"`. Read `outline.md`. Its heading is
`# Outline — <Title> (<Author>)` or similar — parse title and author out of it; if there's no
author, leave it null. The paragraph(s) before the chapter table are the blurb — pass as `notes`
argument to `create_project` (there's no dedicated blurb field; `synopsis` may be filled in later
argument to `create_novel` (there's no dedicated blurb field; `synopsis` may be filled in later
by the author). Record `projectId` in the ledger, mark `"project"` complete.
**2. Characters — dossier fields only, not arcs yet.** Skip files whose name (matched
+4 -4
View File
@@ -1,10 +1,10 @@
{
"mcpServers": {
"novelly": {
"command": "./mcp-server/Novelly.Mcp",
"env": {
"NOVELLY_API_URL": "http://localhost:5080",
"NOVELLY_API_KEY": "<matches the API's Auth:ServiceApiKey user secret>"
"type": "http",
"url": "http://localhost:5080/mcp",
"headers": {
"X-Novelly-Api-Key": "<matches the API's Auth:ServiceApiKey user secret>"
}
}
}
+5 -6
View File
@@ -8,13 +8,12 @@ Novelly: software plan + write novel. ASP.NET Core 10, C#, TypeScript, React, .N
## Structure
- `src/Novelly.Api/` — whole back end, organised by feature. One folder per feature holds
entity, DTOs, service, endpoints together: `Novels/`, `Characters/`, `Chapters/`, `Beats/`,
`Scenes/`, `Tags/`, `Agent/`. `Common/` holds what crosses features; `Data/` holds
`DbContext` + EF migrations.
- `src/Novelly.Api/` — whole back end, organised by feature, plus the MCP endpoint. One folder
per feature holds entity, DTOs, service, endpoints together: `Novels/`, `Characters/`,
`Chapters/`, `Beats/`, `Scenes/`, `Tags/`, `Agent/`, `Mcp/`. `Common/` holds what crosses
features; `Data/` holds `DbContext` + EF migrations.
- `src/Novelly.AppHost/` — .NET Aspire orchestration; run this to bring up API + web client
- `src/Novelly.ServiceDefaults/` — shared Aspire wiring: OpenTelemetry, health checks, service discovery
- `src/Novelly.Mcp/` — MCP stdio server
- `src/Novelly.Web/` — React + Vite client
- `tests/` — test suite
- `docs/` — documentation
@@ -86,7 +85,7 @@ Build + tests passing ≠ working. Anything touching endpoint, agent loop, or MC
Vite dev server on :5173, dashboard for logs + traces
- API alone: `ASPNETCORE_URLS=http://localhost:5080 dotnet run --project src/Novelly.Api`, then exercise route with curl
- Web alone: `cd src/Novelly.Web && npm run dev` — proxies `/api` to :5080
- MCP: build it, then drive over stdio JSON-RPC (`initialize``notifications/initialized``tools/list``tools/call`)
- MCP: with the API running, drive `/mcp` over HTTP (`initialize``notifications/initialized``tools/list``tools/call`) — Streamable HTTP, so responses are SSE-framed and requests need `Accept: application/json, text/event-stream`
Several real bugs here — SQLite refusing ORDER BY DateTimeOffset, agent's model client throwing at construction + taking read-only endpoints down with it — passed build + test suite, only showed up when app actually ran.
-1
View File
@@ -8,7 +8,6 @@
<Folder Name="/src/">
<Project Path="src/Novelly.Api/Novelly.Api.csproj" />
<Project Path="src/Novelly.AppHost/Novelly.AppHost.csproj" />
<Project Path="src/Novelly.Mcp/Novelly.Mcp.csproj" />
<Project Path="src/Novelly.ServiceDefaults/Novelly.ServiceDefaults.csproj" />
<Project Path="src/Novelly.Web/Novelly.Web.esproj">
<Build />
+20 -26
View File
@@ -6,22 +6,21 @@
Software for planning and writing a novel. You outline the book, keep character
dossiers, break chapters into scenes, and draft prose — with a Claude-powered agent
embedded in the app that can read and edit the same data you can, and an MCP server that
exposes that data to Claude Code, Claude Desktop, or any other MCP client.
embedded in the app that can read and edit the same data you can, and an MCP endpoint that
exposes that same data to Claude Code, Claude Desktop, or any other MCP client.
The point of the three-way arrangement is that there is exactly one source of truth. The
React UI, the embedded agent, and the MCP server all go through the same REST API, so an
edit made from a chat in Claude Code and an edit made by typing in the browser are the
same edit.
React UI's REST calls, the embedded agent, and MCP clients all resolve to the same
application services in-process, so an edit made from a chat in Claude Code and an edit
made by typing in the browser are the same edit.
## Stack
| Piece | Built with |
|---|---|
| `Novelly.Api` | ASP.NET Core 10 minimal APIs, EF Core 10 + SQLite, Anthropic SDK, OpenAPI |
| `Novelly.Api` | ASP.NET Core 10 minimal APIs, EF Core 10 + SQLite, Anthropic SDK, OpenAPI, MCP over Streamable HTTP (`ModelContextProtocol.AspNetCore`) |
| `Novelly.AppHost` | .NET Aspire orchestration for the API and the web client |
| `Novelly.ServiceDefaults` | Shared OpenTelemetry, health checks and service discovery |
| `Novelly.Mcp` | MCP stdio server (`ModelContextProtocol`) |
| `Novelly.Web` | React 19, TypeScript, Vite, TanStack Query, Tailwind v4 |
The back end is one project organised by feature, not by layer. Each feature folder —
@@ -192,19 +191,10 @@ A few deliberate choices worth knowing about:
## The MCP server
A stdio MCP server exposing 45 tools over the same REST API. It holds no domain logic of
its own — it is a second front end, not a second implementation.
Build it, then point your MCP client at the produced binary:
```bash
./scripts/publish-mcp.sh
```
Aspire does not run or manage this process — it's a separate stdio subprocess your MCP
client spawns directly, so nothing rebuilds it automatically. Re-run the script (and
reconnect your MCP client) after pulling changes that touch `src/Novelly.Mcp`, or it keeps
serving whatever was published last, including against a stale auth contract.
The API itself serves MCP over Streamable HTTP at `POST /mcp`, exposing 45 tools that call
the same application services the REST endpoints and the embedded web agent call — it holds
no domain logic of its own, and there's nothing to build or publish separately. The API
process just needs to be running; there's no separate subprocess to keep in sync with it.
Copy `.mcp.json.example` to `.mcp.json` (gitignored, since it carries your API key) and
fill in the key:
@@ -213,10 +203,10 @@ fill in the key:
{
"mcpServers": {
"novelly": {
"command": "/absolute/path/to/mcp-server/Novelly.Mcp",
"env": {
"NOVELLY_API_URL": "http://localhost:5080",
"NOVELLY_API_KEY": "<matches the API's Auth:ServiceApiKey user secret>"
"type": "http",
"url": "http://localhost:5080/mcp",
"headers": {
"X-Novelly-Api-Key": "<matches the API's Auth:ServiceApiKey user secret>"
}
}
}
@@ -225,8 +215,12 @@ fill in the key:
The API must be running, with `Auth:ServiceApiKey` set (e.g. via
`dotnet user-secrets set Auth:ServiceApiKey <key> -p src/Novelly.Api`) to the same value
as `NOVELLY_API_KEY` above. If the API is not running, or the key is missing or mismatched,
the tools say so in a message the model can act on rather than failing opaquely.
as the `X-Novelly-Api-Key` header above. If the API is not running, or the key is missing or
mismatched, the request 401s.
Tool argument names are camelCase, matching the REST API and every other MCP argument name
this project has ever used. `create_novel` called over MCP is owned by the seeded service
user (an Admin), not whichever person is signed into the web app.
### Importing an existing outline
+180
View File
@@ -0,0 +1,180 @@
# Merge the MCP server into Novelly.Api as Streamable HTTP `/mcp`
## Context
Novelly has two duplicate tool surfaces that must be kept in sync by hand:
- **`src/Novelly.Mcp/`** — 45 tools as `[McpServerTool]` static methods, stdio-only, each calling the REST API back over HTTP via `NovelApiClient`. Built by no CI job, in no container, unknown to the AppHost. It only works if someone remembers to run `scripts/publish-mcp.sh` and re-point `.mcp.json` at the published binary. Zero tests.
- **`src/Novelly.Api/Agent/NovelAgentToolset.cs`** — 33 tools for the embedded web agent, calling application services directly in-process.
The 33 are a **strict subset** of the 45, matching name-for-name. The gap is pure capability loss for the web agent, not a design distinction.
Two problems follow. The MCP server is unreachable from anything but a local stdio subprocess, so the QA deploy can't serve it without shipping a binary around. And every new capability has to be written twice, in two idioms, with nothing enforcing that they agree.
**Outcome:** one tool registry, called directly by both surfaces. The API serves MCP over Streamable HTTP at `/mcp`, so any MCP client reaches it over the network with an API key and no binary to distribute. The stdio project is deleted. The web agent gains all 12 tools it was missing.
## Decisions taken
- **Delete `src/Novelly.Mcp` entirely.** No stdio proxy is kept.
- **The web agent gets all 45 tools**, including cross-novel `list_novels` and `create_novel`. Full parity, one list, no filtering.
- **camelCase argument names** everywhere. MCP specifies nothing about argument naming — `inputSchema` is plain JSON Schema — so this is a free choice, and camelCase matches .NET convention, the REST API's `JsonSerializerDefaults.Web` output, and the current MCP surface. Existing MCP clients keep working unchanged. The cost lands on `NovelAgentToolset`'s 33 hand-built schemas, which are today the only snake_case thing in the repo and must be rewritten.
- **Tool names stay snake_case** (`list_novels`, `get_novel_brief`) — that part *is* genuine MCP convention, and both surfaces already agree on it.
## Design
A single registry — the existing `AgentTool` shape in `NovelAgentToolset`, extended to all 45 tools — with two thin adapters over it.
**MCP adapter.** Register the SDK's dynamic-tools handlers rather than 45 attribute-decorated methods:
```csharp
services.AddMcpServer(o => o.ServerInfo = new Implementation { Name = "novelly", Version = "1.0.0" })
.WithHttpTransport()
.WithListToolsHandler((request, ct) => ...)
.WithCallToolHandler((request, ct) => ...);
```
Both handlers resolve `request.Services!.GetRequiredService<NovelAgentToolset>()` per request and delegate to a pure adapter class. This avoids fighting the SDK's schema inference (`McpServerToolCreateOptions` has no `InputSchema` property) and avoids hoisting the scoped, nine-dependency toolset into a static catalog, which `WithTools(IEnumerable<McpServerTool>)` would force.
**Agent adapter.** Unchanged — `NovelAgentService.SendMessageAsync``toolset.ExecuteAsync(name, novelId, input, ct)`, with `novelId` still ambient from the route and never shown to the model.
**Novel scoping, one entry / two shapes.** `AgentTool` and `AgentToolDefinition` each gain a trailing `bool RequiresNovelId = false`, so the 33 existing construction sites keep compiling. The MCP adapter injects a required `novelId` property into the advertised schema for those tools and extracts it at call time; the agent adapter supplies it from the route. `list_novels` / `create_novel` need no scope at all.
The 11 currently novel-scoped tools are identifiable mechanically — `grep -n 'async (novelId' src/Novelly.Api/Agent/NovelAgentToolset.cs`: `get_novel_brief`, `update_novel_brief`, `list_characters`, `create_character`, `list_tags`, `list_locations`, `list_chapters`, `create_chapter`, `get_character_beats`, `list_open_questions`, `raise_open_question`. Of the 12 new tools, `create_tag` and `create_location` are novel-scoped; the other ten are child-id-scoped or unscoped.
### Verified before planning
The design rests on SDK behaviour that build-and-test would not catch, so it was checked against a running probe app rather than inferred from docs:
- `WithListToolsHandler` / `WithCallToolHandler` exist in `ModelContextProtocol` 2.1.0; `Tool.InputSchema` is a settable `JsonElement` whose setter validates exactly what `JsonSchemaBuilder.Build()` already emits.
- **`capabilities.tools` *is* advertised** on `initialize` with handlers and no `ToolCollection` — this was the main open risk and it is closed.
- **`request.Services` is non-null and yields a fresh DI scope per request** (three calls returned three distinct scope ids). This is what makes the scoped `NovelDbContext` and `NovelAgentToolset` correct here. `HttpServerTransportOptions.Stateless` defaults to `true` and `PerSessionExecutionContext` to `false` in 2.1.0, so no options need restating.
- A hand-built `JsonElement` schema survives verbatim onto `tools/list` output, and `IsError` maps to `result.isError` rather than a JSON-RPC error object.
- `GET /mcp` returns 405 in stateless mode. Harmless; clients only POST.
## Chunks
Each builds, tests and commits independently.
### Chunk 0 — Fix the cross-novel conversation leak
Independent; do it first to keep it out of the main diff.
`NovelAgentService.FindConversationAsync(Guid conversationId, ...)` matches on id alone, so a conversation belonging to novel X can be continued under novel Y's route, after which every tool call runs against Y with X's transcript. Add an optional novel filter, passed from `SendMessageAsync` only — `GetConversationAsync`/`DeleteConversationAsync` are reached via `/api/conversations/{id}`, which has no novel in the route, and keep passing `null`. Log the miss at Warning with `{ConversationId}`/`{NovelId}`.
**Files:** `src/Novelly.Api/Agent/NovelAgentService.cs`, `tests/Novelly.Api.Tests/NovelAgentServiceTests.cs`
**Tests:** `Continuing_a_conversation_under_a_different_novel_is_rejected`, plus `Continuing_a_conversation_under_its_own_novel_still_works` as the guard against over-tightening.
### Chunk 1 — One registry, all 45 tools (no MCP wiring yet)
Delivers the parity decision on its own, verifiable through the existing web agent.
- `src/Novelly.Api/Agent/AgentContracts.cs` — add `RequiresNovelId` to `AgentToolDefinition`. Safe: `AnthropicAgentModelClient.ToSdkTool` maps `Name`/`Description`/`InputSchema` explicitly, so the flag never reaches the model.
- `src/Novelly.Api/Agent/NovelAgentToolset.cs` — add `RequiresNovelId` to `AgentTool`, set it on the 11 tools above, flow it into `Definitions`, and add the 12 new tools reusing the existing `OrNotFound` / `DeletedOrNotFound` / `ToolNotFound` idioms and the descriptions from the corresponding `src/Novelly.Mcp/Tools/*.cs` methods.
- **Rename the 33 existing schemas to camelCase** in the same file — both the `JsonSchemaBuilder` property keys and the matching `JsonInput` lookup strings, which must stay in lockstep (`.Str("character_id", …)` / `JsonInput.RequiredGuid(input, "character_id")``"characterId"`). Mechanical and contained to this one file, but it is the bulk of the chunk's diff and a mismatched pair fails silently as a missing argument rather than a compile error — so the per-tool tests below are what actually catch it. The 12 new tools are written camelCase from the start, matching the names their `src/Novelly.Mcp/Tools/*.cs` equivalents already used.
The 12 new tools and their existing service calls — no service-layer work is needed:
| tool | scope | service call |
|---|---|---|
| `list_novels` | none | `novels.ListAsync` |
| `create_novel` | none | `novels.CreateAsync` |
| `get_character` | child | `characters.GetAsync` |
| `relate_characters` | child | `characters.AddRelationshipAsync` — note `CreateRelationshipRequest`'s parameter order differs from the old MCP method's |
| `set_arc_stage_beats` | child | `arcs.SetBeatsAsync` |
| `create_tag` | **novel** | `tags.CreateAsync` |
| `update_tag` / `delete_tag` | child | `tags.UpdateAsync` / `DeleteAsync` |
| `create_location` | **novel** | `locations.CreateAsync` |
| `update_location` / `delete_location` | child | `locations.UpdateAsync` / `DeleteAsync` |
| `update_open_question` | child | `questions.UpdateAsync` |
Guid-list arguments follow the existing `JsonInput.Guids` idiom used by `reorder_beats`.
**Tests:** new `tests/Novelly.Api.Tests/NovelAgentToolsetTests.cs`, driving `ExecuteAsync` directly in the style of `ImportAgentToolsetTests.cs` (`ServiceTestFixture` already wires every service the toolset needs). BDD names, one per new capability — e.g. `Relating_two_characters_shows_the_pair_on_both_dossiers`, `Deleting_a_tag_leaves_the_characters_that_carried_it_alone`, `Updating_an_open_question_can_detach_it_from_its_chapter`. Two structural tests carry the most weight:
- `The_toolset_offers_every_tool_the_stdio_server_offered` — assert the 45 names against a hard-coded array. This is the anti-drift test.
- Assert no `RequiresNovelId` tool's schema already declares `novelId`, since the MCP adapter injects it and a duplicate would be silent.
**Runtime verify:** AppHost up, open a novel's agent panel, ask it to create a tag and list tags; confirm in the Aspire trace.
### Chunk 2 — Serve the registry at `/mcp`
- `src/Novelly.Api/Novelly.Api.csproj` — add `ModelContextProtocol.AspNetCore` 2.1.0 (brings Core transitively; don't reference it directly). **Not in the local NuGet cache — first restore needs network.** Pin 2.1.0 to match the verified surface.
- New feature folder `src/Novelly.Api/Mcp/`:
- `NovelMcpTools.cs` — the adapter, as pure static methods: `Describe(definitions)` maps to `Tool` records, injecting `novelId` where `RequiresNovelId`; `CallAsync(toolset, parameters, ct)` serializes arguments to a `JsonElement`, extracts `novelId` when required (`Guid.Empty` otherwise), calls `ExecuteAsync`, and maps `AgentToolResult``CallToolResult`. Catch the `ArgumentException` from a missing/malformed `novelId` and return it as `IsError` rather than letting it escape as a JSON-RPC error. **Log `{Tool}` and `{NovelId}` only — never the arguments, which carry prose (`what_happened`, `synopsis`, `notes`).**
- `McpEndpoints.cs``MapNovelMcp()` calling `app.MapMcp("/mcp")`, matching the repo's `Map*Endpoints` convention.
- `src/Novelly.Api/Common/NovellyServiceRegistration.cs` — the `AddMcpServer(...)` registration shown above.
- `src/Novelly.Api/Program.cs``.MapNovelMcp()` after `UseAuthentication()`/`UseAuthorization()`.
**Auth — the trap.** Do **not** chain `.RequireAuthorization()` onto `MapMcp`. The parameterless overload applies the *default* policy, which authenticates `IdentityConstants.ApplicationScheme` only and would reject the API key. The fallback policy already registered in `NovellyServiceRegistration` lists both that scheme *and* `ServiceApiKeyAuthenticationHandler.SchemeName`, and applies to any endpoint carrying no authorization metadata — which `MapMcp` adds none of. `/mcp` inherits the right protection by doing nothing. If explicitness is wanted, register a named policy listing both schemes; never the parameterless call.
External clients send `X-Novelly-Api-Key: <Auth:ServiceApiKey>`, resolving to `ServiceUser` (Admin), which sees every novel. If `Auth:ServiceApiKey` is unset the service user is never seeded and every call 401s.
CORS needs no change — the origin-restricted default policy is irrelevant to non-browser clients, and stateless mode exposes no `Mcp-Session-Id` header to read.
**Tests:** new `tests/Novelly.Api.Tests/NovelMcpToolsTests.cs`, against the pure adapter methods — no live session, no `WebApplicationFactory`. Cover: all 45 advertised with unique names; novel-scoped tools declare a required `novelId` and child-id tools don't; `list_novels` needs none; every advertised schema is a valid `type: object` (what `Tool.InputSchema`'s setter enforces, worth asserting before the SDK throws at startup); a missing `novelId` and a not-found id both come back as `IsError` results.
**Deliberately not adding a `WebApplicationFactory` harness.** None exists in the repo; adding one means an MVC.Testing reference, overriding the connection string, working around `Program.cs`'s boot-time `MigrateAsync` + `Environment.Exit(1)`, seeding the key, and parsing SSE. Its main payoff — proving the SDK wires up — is delivered more honestly by the curl walkthrough below, which exercises real Kestrel including auth, Serilog and the exception handler. Worth a separate chunk later if a regression harness is wanted.
### Chunk 3 — Delete the stdio server
Only after Chunk 2 is verified, so there's never a window with no MCP surface.
Remove `src/Novelly.Mcp/`, `scripts/publish-mcp.sh`, the `mcp-server/` publish output (gitignored; working-tree cleanup only), and the project line in `Novelly.slnx`. Verified as *not* referencing it: `scripts/ci/build.sh` (publishes the API by path), `prepush.sh`, `test.sh`, both Dockerfiles, the AppHost.
Docs to update:
- `README.md` — fold the `Novelly.Mcp` stack-table row into the API's; rewrite "The MCP server" section (same 45 tools, now in-process at `POST /mcp`, no build step, `X-Novelly-Api-Key` auth). Note that MCP argument names are unchanged (camelCase), so existing clients need no edits, and that `create_novel` is owned by the signed-in user over the web agent but by the service user over MCP.
- `.mcp.json` / `.mcp.json.example` — switch to `type: "http"`, `url: http://localhost:5080/mcp`, with the key in `headers`.
- `.claude/agents/outline-importer.md` — drop the published-binary requirement. Its `tools:` frontmatter is **already stale** (`mcp__novelly__list_projects`, `get_project_brief`, `create_project` exist in neither surface); fix to real names while here.
- `CLAUDE.md` — the Structure list still calls `src/Novelly.Mcp/` the "MCP stdio server" and Verifying still says "drive over stdio JSON-RPC". Propose these edits rather than slipping them in; CLAUDE.md is user-owned.
Leave `docs/plans/api/users_and_roles_plan.md` alone — historical.
**Verify:** `dotnet build Novelly.slnx` and `./scripts/ci/prepush.sh` both pass.
## Verification
Streamable HTTP needs `Accept: application/json, text/event-stream` and replies SSE-framed, so pipe through `sed -n 's/^data: //p'`.
```bash
dotnet user-secrets set Auth:ServiceApiKey devkey -p src/Novelly.Api
ASPNETCORE_URLS=http://localhost:5080 dotnet run --project src/Novelly.Api
MCP=http://localhost:5080/mcp
H=(-H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" -H "X-Novelly-Api-Key: devkey")
# handshake — expect capabilities.tools present
curl -sS "${H[@]}" "$MCP" -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"curl","version":"1"}}}' | sed -n 's/^data: //p' | jq .
curl -sS "${H[@]}" -o /dev/null -w '%{http_code}\n' "$MCP" -d '{"jsonrpc":"2.0","method":"notifications/initialized"}'
# expect exactly 45
curl -sS "${H[@]}" "$MCP" -d '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' | sed -n 's/^data: //p' | jq '.result.tools | length'
# novelId injection: present on list_tags, absent on delete_tag and list_novels
curl -sS "${H[@]}" "$MCP" -d '{"jsonrpc":"2.0","id":3,"method":"tools/list","params":{}}' | sed -n 's/^data: //p' \
| jq '.result.tools[] | select(.name=="list_tags" or .name=="delete_tag" or .name=="list_novels") | {name, props:(.inputSchema.properties|keys), required:.inputSchema.required}'
# unscoped call, then a novel-scoped one (proves NovelUserContext resolved the service user in-handler)
curl -sS "${H[@]}" "$MCP" -d '{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"list_novels","arguments":{}}}' | sed -n 's/^data: //p' | jq .
curl -sS "${H[@]}" "$MCP" -d '{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"get_novel_brief","arguments":{"novelId":"<id from above>"}}}' | sed -n 's/^data: //p' | jq .
# error mapping — expect result.isError true, not a JSON-RPC error
curl -sS "${H[@]}" "$MCP" -d '{"jsonrpc":"2.0","id":6,"method":"tools/call","params":{"name":"list_tags","arguments":{}}}' | sed -n 's/^data: //p' | jq .
# auth — expect 401 with no key
curl -sS -o /dev/null -w '%{http_code}\n' -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" \
"$MCP" -d '{"jsonrpc":"2.0","id":7,"method":"tools/list","params":{}}'
# one write, end to end
curl -sS "${H[@]}" "$MCP" -d '{"jsonrpc":"2.0","id":8,"method":"tools/call","params":{"name":"create_tag","arguments":{"novelId":"<id>","name":"Salt","color":"#9a4a2f"}}}' | sed -n 's/^data: //p' | jq .
```
Then reconnect a real client: rewrite `.mcp.json` to the `type: "http"` form and run `/mcp` in Claude Code to confirm 45 tools. Finally confirm the web agent still drives the same registry (Aspire up, agent panel, exercise one of the 12 new tools) and that the dashboard shows `/mcp` requests with no prose in the log lines.
## Risks
- **`ModelContextProtocol.AspNetCore` is not cached locally.** First restore needs network.
- **The web agent's argument names change** (snake_case → camelCase) across all 33 existing tools. Nothing external consumes those schemas — they are built fresh per request and handed to the model each turn — so there is no compatibility surface, but a `JsonSchemaBuilder` key left out of step with its `JsonInput` lookup fails silently as a missing argument rather than a compile error. External MCP clients are unaffected: their argument names were already camelCase.
- **`create_novel` ownership differs by surface** — signed-in user vs service user. Not a bug, but surprising; document it.
- **Not fixed here:** child-id-scoped tools carry no novel context in *either* surface, so a caller holding a foreign beat/tag/question id can reach across novels, subject only to `NovelAccessService`. Pre-existing for 20+ tools and unchanged by this work. Worth a follow-up.
- **QA deploy:** once merged, `/mcp` rides the existing API container and its already-exposed port. `MCP_API_KEY` must be set in Gitea for `Auth__ServiceApiKey`, or every MCP call 401s.
-15
View File
@@ -1,15 +0,0 @@
#!/usr/bin/env bash
# Rebuilds the standalone Novelly.Mcp binary that Claude Code (or Claude Desktop) spawns
# per .mcp.json. Aspire does not run or manage this process, so nothing else rebuilds it —
# run this after pulling changes that touch src/Novelly.Mcp, or the MCP server silently
# keeps serving whatever was published last.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.." && source ./scripts/ci/lib.sh
cd "$CI_ROOT"
ensure_dotnet
log "Publishing Novelly.Mcp to ./mcp-server"
dotnet publish src/Novelly.Mcp -c Release -o ./mcp-server
log "Done. Reconnect the MCP server (e.g. /mcp in Claude Code) to pick up the new build."
+1 -1
View File
@@ -2,7 +2,7 @@ using System.Text.Json;
namespace Novelly.Api.Agent;
public record AgentToolDefinition(string Name, string Description, JsonElement InputSchema);
public record AgentToolDefinition(string Name, string Description, JsonElement InputSchema, bool RequiresNovelId = false);
public abstract record AgentContentBlock;
+12 -4
View File
@@ -43,7 +43,7 @@ public class NovelAgentService(
logger.LogInformation("Getting agent conversation {ConversationId}", conversationId);
return await FindConversationAsync(conversationId, ct);
return await FindConversationAsync(conversationId, null, ct);
}
public async Task<bool> DeleteConversationAsync(Guid conversationId, CancellationToken ct = default)
@@ -52,7 +52,7 @@ public class NovelAgentService(
logger.LogInformation("Deleting agent conversation {ConversationId}", conversationId);
var conversation = await FindConversationAsync(conversationId, ct);
var conversation = await FindConversationAsync(conversationId, null, ct);
if (conversation is null)
{
return false;
@@ -81,7 +81,7 @@ public class NovelAgentService(
}
var conversation = request.ConversationId is { } id
? await FindConversationAsync(id, ct)
? await FindConversationAsync(id, novelId, ct)
: StartConversation(novelId, request.Message);
if (conversation is null) return null;
@@ -181,7 +181,7 @@ public class NovelAgentService(
return conversation;
}
private async Task<AgentConversation?> FindConversationAsync(Guid conversationId, CancellationToken ct)
private async Task<AgentConversation?> FindConversationAsync(Guid conversationId, Guid? novelId, CancellationToken ct)
{
logger.LogDebug("Finding agent conversation {ConversationId}", conversationId);
@@ -195,6 +195,14 @@ public class NovelAgentService(
return conversation;
}
if (novelId is { } expectedNovelId && conversation.NovelId != expectedNovelId)
{
logger.LogWarning(
"AgentConversation {ConversationId} belongs to novel {ActualNovelId}, not requested novel {NovelId}",
conversationId, conversation.NovelId, expectedNovelId);
return null;
}
logger.LogDebug("Found agent conversation {ConversationId}", conversationId);
return conversation;
}
+328 -111
View File
@@ -21,7 +21,8 @@ public record AgentTool(
string Name,
string Description,
JsonElement InputSchema,
Func<Guid, JsonElement, CancellationToken, Task<object?>> Handler);
Func<Guid, JsonElement, CancellationToken, Task<object?>> Handler,
bool RequiresNovelId = false);
public class NovelAgentToolset(
NovelService novels,
@@ -46,7 +47,7 @@ public class NovelAgentToolset(
private IReadOnlyList<AgentTool> Tools => [.. ByName.Values];
public IReadOnlyList<AgentToolDefinition> Definitions =>
[.. Tools.Select(t => new AgentToolDefinition(t.Name, t.Description, t.InputSchema))];
[.. Tools.Select(t => new AgentToolDefinition(t.Name, t.Description, t.InputSchema, t.RequiresNovelId))];
public async Task<AgentToolResult> ExecuteAsync(string name, Guid novelId, JsonElement input, CancellationToken ct = default)
{
@@ -97,12 +98,45 @@ public class NovelAgentToolset(
private IEnumerable<AgentTool> Build()
{
yield return new AgentTool(
"list_novels",
"List every novel, with counts of characters, chapters and drafted words. "
+ "Start here to find the novel id everything else needs.",
new JsonSchemaBuilder().Build(),
async (_, _, ct) => await novels.ListAsync(ct));
yield return new AgentTool(
"create_novel",
"Create a new novel.",
new JsonSchemaBuilder()
.Str("title", "Working title.", required: true)
.Str("author", "Author name.")
.Str("genre", "Genre or category.")
.Str("logline", "One-sentence pitch.")
.Str("synopsis", "Paragraph-length summary of the whole book.")
.Str("notes", "Free-form notes on theme, tone, comparable titles.")
.Int("targetWordCount", "Target manuscript length in words.")
.Build(),
async (_, input, ct) =>
{
var novel = await novels.CreateAsync(new CreateNovelRequest(
JsonInput.RequiredString(input, "title"),
JsonInput.String(input, "author"),
JsonInput.String(input, "genre"),
JsonInput.String(input, "logline"),
JsonInput.String(input, "synopsis"),
JsonInput.String(input, "notes"),
JsonInput.Int(input, "targetWordCount")), ct);
return novel.ToResponse(null);
});
yield return new AgentTool(
"get_novel_brief",
"Read the novel's title, logline, synopsis, genre, notes and word-count target. "
+ "Call this first in a conversation to ground yourself in what the book is.",
new JsonSchemaBuilder().Build(),
async (novelId, _, ct) => await OrNotFound(novels.GetAsync(novelId, ct), p => p.ToResponse(null), "Novel", novelId));
async (novelId, _, ct) => await OrNotFound(novels.GetAsync(novelId, ct), p => p.ToResponse(null), "Novel", novelId),
RequiresNovelId: true);
yield return new AgentTool(
"update_novel_brief",
@@ -115,7 +149,7 @@ public class NovelAgentToolset(
.Str("logline", "One-sentence pitch.")
.Str("synopsis", "Paragraph-length summary of the whole book.")
.Str("notes", "Free-form notes on theme, tone, comparable titles.")
.Int("target_word_count", "Target manuscript length in words.")
.Int("targetWordCount", "Target manuscript length in words.")
.Build(),
async (novelId, input, ct) => await OrNotFound(novels.UpdateAsync(novelId, new UpdateNovelRequest(
JsonInput.String(input, "title"),
@@ -124,13 +158,27 @@ public class NovelAgentToolset(
JsonInput.String(input, "logline"),
JsonInput.String(input, "synopsis"),
JsonInput.String(input, "notes"),
JsonInput.Int(input, "target_word_count")), ct), p => p.ToResponse(null), "Novel", novelId));
JsonInput.Int(input, "targetWordCount")), ct), p => p.ToResponse(null), "Novel", novelId),
RequiresNovelId: true);
yield return new AgentTool(
"list_characters",
"List every character in the novel with their full dossiers.",
new JsonSchemaBuilder().Build(),
async (novelId, _, ct) => (await characters.ListAsync(novelId, ct)).Select(c => c.ToResponse()));
async (novelId, _, ct) => (await characters.ListAsync(novelId, ct)).Select(c => c.ToResponse()),
RequiresNovelId: true);
yield return new AgentTool(
"get_character",
"Read one character's dossier.",
new JsonSchemaBuilder()
.Str("characterId", "Id of the character to read.", required: true)
.Build(),
async (_, input, ct) =>
{
var characterId = JsonInput.RequiredGuid(input, "characterId");
return await OrNotFound(characters.GetAsync(characterId, ct), c => c.ToResponse(), "Character", characterId);
});
yield return new AgentTool(
"create_character",
@@ -152,17 +200,18 @@ public class NovelAgentToolset(
JsonInput.String(input, "voice"),
JsonInput.String(input, "notes"),
JsonInput.Strings(input, "tags"),
JsonInput.Strings(input, "aliases")), ct), c => c.ToResponse(), "Novel", novelId));
JsonInput.Strings(input, "aliases")), ct), c => c.ToResponse(), "Novel", novelId),
RequiresNovelId: true);
yield return new AgentTool(
"update_character",
"Revise an existing character dossier. Only the fields you supply change.",
CharacterSchema(includeName: true, nameRequired: false)
.Str("character_id", "Id of the character to update.", required: true)
.Str("characterId", "Id of the character to update.", required: true)
.Build(),
async (_, input, ct) =>
{
var characterId = JsonInput.RequiredGuid(input, "character_id");
var characterId = JsonInput.RequiredGuid(input, "characterId");
return await OrNotFound(characters.UpdateAsync(
characterId,
new UpdateCharacterRequest(
@@ -183,25 +232,49 @@ public class NovelAgentToolset(
JsonInput.Strings(input, "aliases")), ct), c => c.ToResponse(), "Character", characterId);
});
yield return new AgentTool(
"relate_characters",
"Record a relationship between two characters in the same novel. Creates both directions "
+ "at once — characterId's side and relatedCharacterId's side — so the pair always shows up "
+ "on both dossiers.",
new JsonSchemaBuilder()
.Str("characterId", "Id of the character the relationship belongs to.", required: true)
.Str("relatedCharacterId", "Id of the character they are related to.", required: true)
.Str("relationshipType", "How characterId is related to relatedCharacterId, e.g. 'sister', 'rival', 'former mentor'.", required: true)
.Str("reciprocalRelationshipType", "How relatedCharacterId is related back to characterId, if different. Defaults to relationshipType when the relation is symmetric, like 'rival'.")
.Str("description", "What the relationship is like, and where it is headed.")
.Build(),
async (_, input, ct) =>
{
var characterId = JsonInput.RequiredGuid(input, "characterId");
return await OrNotFound(characters.AddRelationshipAsync(
characterId,
new CreateRelationshipRequest(
JsonInput.RequiredGuid(input, "relatedCharacterId"),
JsonInput.RequiredString(input, "relationshipType"),
JsonInput.String(input, "description"),
JsonInput.String(input, "reciprocalRelationshipType")), ct), c => c.ToResponse(), "Character", characterId);
});
yield return new AgentTool(
"link_character_identity",
"Record that a character is really another character — e.g. one introduced under one name "
+ "who is later revealed to be a character already in the novel under another name. Both "
+ "keep their own dossier and beats; the canonical identity is whichever character you link to.",
new JsonSchemaBuilder()
.Str("character_id", "Id of the character being revealed as someone else.", required: true)
.Str("same_character_as_id", "Id of the character this one really is.", required: true)
.Str("revealed_in_chapter_id", "Id of the chapter where the reveal happens, if any.")
.Str("characterId", "Id of the character being revealed as someone else.", required: true)
.Str("sameCharacterAsId", "Id of the character this one really is.", required: true)
.Str("revealedInChapterId", "Id of the chapter where the reveal happens, if any.")
.Str("note", "Context on the reveal, e.g. how and why the disguise held.")
.Build(),
async (_, input, ct) =>
{
var characterId = JsonInput.RequiredGuid(input, "character_id");
var characterId = JsonInput.RequiredGuid(input, "characterId");
return await OrNotFound(characters.LinkIdentityAsync(
characterId,
new LinkCharacterIdentityRequest(
JsonInput.RequiredGuid(input, "same_character_as_id"),
JsonInput.Guid(input, "revealed_in_chapter_id"),
JsonInput.RequiredGuid(input, "sameCharacterAsId"),
JsonInput.Guid(input, "revealedInChapterId"),
JsonInput.String(input, "note")), ct), c => c.ToResponse(), "Character", characterId);
});
@@ -209,11 +282,11 @@ public class NovelAgentToolset(
"unlink_character_identity",
"Remove a character's identity link, restoring it to its own separate identity.",
new JsonSchemaBuilder()
.Str("character_id", "Id of the character to unlink.", required: true)
.Str("characterId", "Id of the character to unlink.", required: true)
.Build(),
async (_, input, ct) =>
{
var characterId = JsonInput.RequiredGuid(input, "character_id");
var characterId = JsonInput.RequiredGuid(input, "characterId");
return await DeletedOrNotFound(characters.UnlinkIdentityAsync(characterId, ct), "Character", characterId);
});
@@ -222,29 +295,29 @@ public class NovelAgentToolset(
"Read a chapter's outline: its summary paragraph and its beat table, in order. "
+ "A beat is one row — a short title, whose beat it is, what happened, and what it sets up.",
new JsonSchemaBuilder()
.Str("chapter_id", "Id of the chapter whose outline to read.", required: true)
.Str("chapterId", "Id of the chapter whose outline to read.", required: true)
.Build(),
async (_, input, ct) => (await beats.ListAsync(JsonInput.RequiredGuid(input, "chapter_id"), ct)).Select(b => b.ToResponse()));
async (_, input, ct) => (await beats.ListAsync(JsonInput.RequiredGuid(input, "chapterId"), ct)).Select(b => b.ToResponse()));
yield return new AgentTool(
"create_beat",
"Add a beat to a chapter's outline. Keep the title to three to five words — it is a "
+ "handle, not a sentence; the detail belongs in what_happened and whats_next.",
+ "handle, not a sentence; the detail belongs in whatHappened and whatsNext.",
BeatSchema()
.Str("chapter_id", "Id of the chapter the beat belongs to.", required: true)
.Str("chapterId", "Id of the chapter the beat belongs to.", required: true)
.Str("title", "Three to five words naming the beat.", required: true)
.Build(),
async (_, input, ct) =>
{
var chapterId = JsonInput.RequiredGuid(input, "chapter_id");
var chapterId = JsonInput.RequiredGuid(input, "chapterId");
return await OrNotFound(beats.CreateAsync(
chapterId,
new CreateBeatRequest(
JsonInput.RequiredString(input, "title"),
JsonInput.Int(input, "sort_order"),
JsonInput.Guids(input, "character_ids"),
JsonInput.String(input, "what_happened"),
JsonInput.String(input, "whats_next"),
JsonInput.Int(input, "sortOrder"),
JsonInput.Guids(input, "characterIds"),
JsonInput.String(input, "whatHappened"),
JsonInput.String(input, "whatsNext"),
JsonInput.Strings(input, "tags")), ct), b => b.ToResponse(), "Chapter", chapterId);
});
@@ -253,20 +326,20 @@ public class NovelAgentToolset(
"Revise a beat. Only the fields you supply change. Supplying a tag list replaces "
+ "the beat's tags outright, so include the ones you want to keep.",
BeatSchema()
.Str("beat_id", "Id of the beat to update.", required: true)
.Str("beatId", "Id of the beat to update.", required: true)
.Str("title", "Three to five words naming the beat.")
.Build(),
async (_, input, ct) =>
{
var beatId = JsonInput.RequiredGuid(input, "beat_id");
var beatId = JsonInput.RequiredGuid(input, "beatId");
return await OrNotFound(beats.UpdateAsync(
beatId,
new UpdateBeatRequest(
JsonInput.String(input, "title"),
JsonInput.Int(input, "sort_order"),
JsonInput.Guids(input, "character_ids"),
JsonInput.String(input, "what_happened"),
JsonInput.String(input, "whats_next"),
JsonInput.Int(input, "sortOrder"),
JsonInput.Guids(input, "characterIds"),
JsonInput.String(input, "whatHappened"),
JsonInput.String(input, "whatsNext"),
JsonInput.Strings(input, "tags")), ct), b => b.ToResponse(), "Beat", beatId);
});
@@ -274,11 +347,11 @@ public class NovelAgentToolset(
"delete_beat",
"Remove a beat from a chapter's outline. Confirm with the writer before calling it.",
new JsonSchemaBuilder()
.Str("beat_id", "Id of the beat to delete.", required: true)
.Str("beatId", "Id of the beat to delete.", required: true)
.Build(),
async (_, input, ct) =>
{
var beatId = JsonInput.RequiredGuid(input, "beat_id");
var beatId = JsonInput.RequiredGuid(input, "beatId");
return await DeletedOrNotFound(beats.DeleteAsync(beatId, ct), "Beat", beatId);
});
@@ -287,16 +360,16 @@ public class NovelAgentToolset(
"Renumber a chapter's beats to match the order given. List every beat id in the "
+ "order you want; any you leave out keep their relative position at the end.",
new JsonSchemaBuilder()
.Str("chapter_id", "Id of the chapter whose beats to reorder.", required: true)
.StringArray("beat_ids", "Beat ids in their new order.", required: true)
.Str("chapterId", "Id of the chapter whose beats to reorder.", required: true)
.StringArray("beatIds", "Beat ids in their new order.", required: true)
.Build(),
async (_, input, ct) =>
{
var chapterId = JsonInput.RequiredGuid(input, "chapter_id");
var chapterId = JsonInput.RequiredGuid(input, "chapterId");
return await OrNotFound(beats.ReorderAsync(
chapterId,
new ReorderBeatsRequest(
[.. (JsonInput.Strings(input, "beat_ids") ?? [])
[.. (JsonInput.Strings(input, "beatIds") ?? [])
.Select(id => Guid.TryParse(id, out var g) ? g : Guid.Empty)
.Where(g => g != Guid.Empty)]), ct), list => list.Select(b => b.ToResponse()), "Chapter", chapterId);
});
@@ -306,18 +379,18 @@ public class NovelAgentToolset(
"Add a character to several beats at once. Leaves each beat's existing characters and "
+ "other fields alone — this only adds, it never removes.",
new JsonSchemaBuilder()
.Str("chapter_id", "Id of the chapter the beats belong to.", required: true)
.Str("character_id", "Id of the character to add.", required: true)
.StringArray("beat_ids", "Ids of the beats to add the character to.", required: true)
.Str("chapterId", "Id of the chapter the beats belong to.", required: true)
.Str("characterId", "Id of the character to add.", required: true)
.StringArray("beatIds", "Ids of the beats to add the character to.", required: true)
.Build(),
async (_, input, ct) =>
{
var chapterId = JsonInput.RequiredGuid(input, "chapter_id");
var chapterId = JsonInput.RequiredGuid(input, "chapterId");
return await OrNotFound(beats.AssignCharacterAsync(
chapterId,
new AssignCharacterToBeatsRequest(
JsonInput.RequiredGuid(input, "character_id"),
[.. (JsonInput.Strings(input, "beat_ids") ?? [])
JsonInput.RequiredGuid(input, "characterId"),
[.. (JsonInput.Strings(input, "beatIds") ?? [])
.Select(id => Guid.TryParse(id, out var g) ? g : Guid.Empty)
.Where(g => g != Guid.Empty)]), ct), list => list.Select(b => b.ToResponse()), "Chapter", chapterId);
});
@@ -327,18 +400,18 @@ public class NovelAgentToolset(
"Move one or more beats from one chapter to another, appending them to the target "
+ "chapter's end in the order given.",
new JsonSchemaBuilder()
.Str("chapter_id", "Id of the beats' current chapter.", required: true)
.Str("target_chapter_id", "Id of the chapter to move the beats into.", required: true)
.StringArray("beat_ids", "Ids of the beats to move.", required: true)
.Str("chapterId", "Id of the beats' current chapter.", required: true)
.Str("targetChapterId", "Id of the chapter to move the beats into.", required: true)
.StringArray("beatIds", "Ids of the beats to move.", required: true)
.Build(),
async (_, input, ct) =>
{
var chapterId = JsonInput.RequiredGuid(input, "chapter_id");
var chapterId = JsonInput.RequiredGuid(input, "chapterId");
return await OrNotFound(beats.MoveAsync(
chapterId,
new MoveBeatsRequest(
JsonInput.RequiredGuid(input, "target_chapter_id"),
[.. (JsonInput.Strings(input, "beat_ids") ?? [])
JsonInput.RequiredGuid(input, "targetChapterId"),
[.. (JsonInput.Strings(input, "beatIds") ?? [])
.Select(id => Guid.TryParse(id, out var g) ? g : Guid.Empty)
.Where(g => g != Guid.Empty)]), ct), list => list.Select(b => b.ToResponse()), "Chapter", chapterId);
});
@@ -348,18 +421,19 @@ public class NovelAgentToolset(
"List the novel's tags with how many characters, chapters and beats carry each. "
+ "Read this before inventing a new tag so you reuse the writer's vocabulary.",
new JsonSchemaBuilder().Build(),
async (novelId, _, ct) => await tags.ListAsync(novelId, ct));
async (novelId, _, ct) => await tags.ListAsync(novelId, ct),
RequiresNovelId: true);
yield return new AgentTool(
"get_tag_references",
"Cross-reference a tag: every character, chapter and beat carrying it. Use this to "
+ "trace a motif or a thread through the book.",
new JsonSchemaBuilder()
.Str("tag_id", "Id of the tag to trace.", required: true)
.Str("tagId", "Id of the tag to trace.", required: true)
.Build(),
async (_, input, ct) =>
{
var tagId = JsonInput.RequiredGuid(input, "tag_id");
var tagId = JsonInput.RequiredGuid(input, "tagId");
var tag = await tags.GetReferencesAsync(tagId, ct);
if (tag is null)
{
@@ -370,22 +444,68 @@ public class NovelAgentToolset(
return tag.ToReferencesResponse(displayNumbers);
});
yield return new AgentTool(
"create_tag",
"Create a tag explicitly. Applying an unknown tag by name to a character, chapter or "
+ "beat also creates it, so this is only needed to set a colour up front.",
new JsonSchemaBuilder()
.Str("name", "The tag's name. Unique within the novel, matched case-insensitively.", required: true)
.Str("color", "Optional hex colour for the UI, e.g. \"#9a4a2f\".")
.Build(),
async (novelId, input, ct) => await OrNotFound(tags.CreateAsync(
novelId,
new CreateTagRequest(
JsonInput.RequiredString(input, "name"),
JsonInput.String(input, "color")), ct), t => t.ToResponse(), "Novel", novelId),
RequiresNovelId: true);
yield return new AgentTool(
"update_tag",
"Rename or recolour a tag. Renaming updates it everywhere it is applied.",
new JsonSchemaBuilder()
.Str("tagId", "Id of the tag to update.", required: true)
.Str("name", "New name.")
.Str("color", "Hex colour, e.g. \"#9a4a2f\".")
.Build(),
async (_, input, ct) =>
{
var tagId = JsonInput.RequiredGuid(input, "tagId");
return await OrNotFound(tags.UpdateAsync(
tagId,
new UpdateTagRequest(
JsonInput.String(input, "name"),
JsonInput.String(input, "color")), ct), t => t.ToResponse(), "Tag", tagId);
});
yield return new AgentTool(
"delete_tag",
"Delete a tag. Whatever carried it is left alone — only the label goes.",
new JsonSchemaBuilder()
.Str("tagId", "Id of the tag to delete.", required: true)
.Build(),
async (_, input, ct) =>
{
var tagId = JsonInput.RequiredGuid(input, "tagId");
return await DeletedOrNotFound(tags.DeleteAsync(tagId, ct), "Tag", tagId);
});
yield return new AgentTool(
"list_locations",
"List the novel's locations with how many chapters are set there. "
+ "Read this before inventing a new location so you reuse the writer's vocabulary.",
new JsonSchemaBuilder().Build(),
async (novelId, _, ct) => await locations.ListAsync(novelId, ct));
async (novelId, _, ct) => await locations.ListAsync(novelId, ct),
RequiresNovelId: true);
yield return new AgentTool(
"get_location_references",
"Cross-reference a location: every chapter set there.",
new JsonSchemaBuilder()
.Str("location_id", "Id of the location to trace.", required: true)
.Str("locationId", "Id of the location to trace.", required: true)
.Build(),
async (_, input, ct) =>
{
var locationId = JsonInput.RequiredGuid(input, "location_id");
var locationId = JsonInput.RequiredGuid(input, "locationId");
var location = await locations.GetReferencesAsync(locationId, ct);
if (location is null)
{
@@ -396,6 +516,45 @@ public class NovelAgentToolset(
return location.ToReferencesResponse(displayNumbers);
});
yield return new AgentTool(
"create_location",
"Create a location explicitly. Applying an unknown location by name to a chapter also "
+ "creates it, so this is only needed to set one up ahead of time.",
new JsonSchemaBuilder()
.Str("name", "The location's name. Unique within the novel, matched case-insensitively.", required: true)
.Build(),
async (novelId, input, ct) => await OrNotFound(locations.CreateAsync(
novelId,
new CreateLocationRequest(JsonInput.RequiredString(input, "name")), ct), l => l.ToResponse(), "Novel", novelId),
RequiresNovelId: true);
yield return new AgentTool(
"update_location",
"Rename a location. Renaming updates it everywhere it is applied.",
new JsonSchemaBuilder()
.Str("locationId", "Id of the location to update.", required: true)
.Str("name", "New name.", required: true)
.Build(),
async (_, input, ct) =>
{
var locationId = JsonInput.RequiredGuid(input, "locationId");
return await OrNotFound(locations.UpdateAsync(
locationId,
new UpdateLocationRequest(JsonInput.RequiredString(input, "name")), ct), l => l.ToResponse(), "Location", locationId);
});
yield return new AgentTool(
"delete_location",
"Delete a location. Whatever carried it is left alone — only the label goes.",
new JsonSchemaBuilder()
.Str("locationId", "Id of the location to delete.", required: true)
.Build(),
async (_, input, ct) =>
{
var locationId = JsonInput.RequiredGuid(input, "locationId");
return await DeletedOrNotFound(locations.DeleteAsync(locationId, ct), "Location", locationId);
});
yield return new AgentTool(
"list_chapters",
"List the novel's chapters in manuscript order with beat and word counts.",
@@ -405,17 +564,18 @@ public class NovelAgentToolset(
var list = await chapters.ListAsync(novelId, ct);
var displayNumbers = ChapterNumbering.DisplayNumbers(list);
return list.Select(c => c.ToSummaryResponse(displayNumbers.TryGetValue(c.Id, out var n) ? n : null));
});
},
RequiresNovelId: true);
yield return new AgentTool(
"get_chapter",
"Read one chapter in full: its outline (beats) and its drafted prose.",
new JsonSchemaBuilder()
.Str("chapter_id", "Id of the chapter to read.", required: true)
.Str("chapterId", "Id of the chapter to read.", required: true)
.Build(),
async (_, input, ct) =>
{
var chapterId = JsonInput.RequiredGuid(input, "chapter_id");
var chapterId = JsonInput.RequiredGuid(input, "chapterId");
var chapter = await chapters.GetAsync(chapterId, ct);
if (chapter is null)
{
@@ -439,7 +599,7 @@ public class NovelAgentToolset(
.StringArray("locations", "Where and when the chapter takes place. Unknown locations are created.")
.Str("notes", "Anything else worth recording.")
.Enum("status", "Drafting status.", System.Enum.GetNames<DraftStatus>())
.Int("target_word_count", "Target length in words.")
.Int("targetWordCount", "Target length in words.")
.Str("prose", "The chapter's drafted text, in markdown, if you are writing it now.")
.StringArray("tags", "Tags for cross-referencing. Replaces the existing tags.")
.Build(),
@@ -453,7 +613,7 @@ public class NovelAgentToolset(
JsonInput.Strings(input, "locations"),
JsonInput.String(input, "notes"),
JsonInput.Enum<DraftStatus>(input, "status") ?? DraftStatus.Planned,
JsonInput.Int(input, "target_word_count"),
JsonInput.Int(input, "targetWordCount"),
JsonInput.String(input, "prose"),
JsonInput.Strings(input, "tags")), ct);
@@ -464,7 +624,8 @@ public class NovelAgentToolset(
var displayNumber = await chapters.DisplayNumberAsync(chapter, ct);
return chapter.ToResponse(displayNumber);
});
},
RequiresNovelId: true);
yield return new AgentTool(
"update_chapter",
@@ -472,7 +633,7 @@ public class NovelAgentToolset(
+ "prose. Use 'prose' to write or replace the chapter's draft text in markdown; the "
+ "word count is recomputed automatically.",
new JsonSchemaBuilder()
.Str("chapter_id", "Id of the chapter to update.", required: true)
.Str("chapterId", "Id of the chapter to update.", required: true)
.Str("title", "New title.")
.Int("number", "Manuscript position, 1-based, counting front and back matter.")
.Enum("kind", "Front matter, a numbered body chapter, or back matter.", System.Enum.GetNames<ChapterKind>())
@@ -480,13 +641,13 @@ public class NovelAgentToolset(
.StringArray("locations", "Where and when the chapter takes place. Replaces the existing locations. Unknown locations are created.")
.Str("notes", "Anything else worth recording.")
.Enum("status", "Drafting status.", System.Enum.GetNames<DraftStatus>())
.Int("target_word_count", "Target length in words.")
.Int("targetWordCount", "Target length in words.")
.Str("prose", "The chapter's drafted text, in markdown.")
.StringArray("tags", "Tags for cross-referencing. Replaces the existing tags.")
.Build(),
async (_, input, ct) =>
{
var chapterId = JsonInput.RequiredGuid(input, "chapter_id");
var chapterId = JsonInput.RequiredGuid(input, "chapterId");
var chapter = await chapters.UpdateAsync(
chapterId,
new UpdateChapterRequest(
@@ -497,7 +658,7 @@ public class NovelAgentToolset(
JsonInput.Strings(input, "locations"),
JsonInput.String(input, "notes"),
JsonInput.Enum<DraftStatus>(input, "status"),
JsonInput.Int(input, "target_word_count"),
JsonInput.Int(input, "targetWordCount"),
JsonInput.String(input, "prose"),
JsonInput.Strings(input, "tags")), ct);
@@ -516,11 +677,11 @@ public class NovelAgentToolset(
+ "Read this before revising a character — it is what they actually do on the page, "
+ "as opposed to what the dossier claims about them.",
new JsonSchemaBuilder()
.Str("character_id", "Id of the character.", required: true)
.Str("characterId", "Id of the character.", required: true)
.Build(),
async (novelId, input, ct) =>
{
var characterId = JsonInput.RequiredGuid(input, "character_id");
var characterId = JsonInput.RequiredGuid(input, "characterId");
var characterBeats = await beats.ListForCharacterAsync(characterId, ct);
if (characterBeats is null)
{
@@ -530,66 +691,67 @@ public class NovelAgentToolset(
var displayNumbers = await chapterLabels.ForNovelAsync(novelId, ct);
return characterBeats.Select(b =>
b.ToCharacterBeatResponse(characterId, b.Chapter is null ? null : chapterLabels.LabelFor(b.Chapter, displayNumbers)));
});
},
RequiresNovelId: true);
yield return new AgentTool(
"get_character_arc",
"Read a main character's arc: the ordered stages of how they change. Each stage may "
+ "be pinned to the chapter where it lands.",
new JsonSchemaBuilder()
.Str("character_id", "Id of the character.", required: true)
.Str("characterId", "Id of the character.", required: true)
.Build(),
async (_, input, ct) => (await arcs.ListAsync(
JsonInput.RequiredGuid(input, "character_id"), ct)).Select(s => s.ToResponse()));
JsonInput.RequiredGuid(input, "characterId"), ct)).Select(s => s.ToResponse()));
yield return new AgentTool(
"add_arc_stage",
"Add a stage to a character's arc. Arcs are for main characters — promote the "
+ "character first with update_character if they are still Supporting.",
ArcStageSchema()
.Str("character_id", "Id of the character whose arc to add to.", required: true)
.Str("characterId", "Id of the character whose arc to add to.", required: true)
.Str("title", "A short handle for the change, three to five words.", required: true)
.Build(),
async (_, input, ct) =>
{
var characterId = JsonInput.RequiredGuid(input, "character_id");
var characterId = JsonInput.RequiredGuid(input, "characterId");
return await OrNotFound(arcs.CreateAsync(
characterId,
new CreateArcStageRequest(
JsonInput.RequiredString(input, "title"),
JsonInput.Int(input, "sort_order"),
JsonInput.Int(input, "sortOrder"),
JsonInput.String(input, "description"),
JsonInput.Guid(input, "chapter_id")), ct), s => s.ToResponse(), "Character", characterId);
JsonInput.Guid(input, "chapterId")), ct), s => s.ToResponse(), "Character", characterId);
});
yield return new AgentTool(
"update_arc_stage",
"Revise a stage of a character's arc. Only the fields you supply change.",
ArcStageSchema()
.Str("arc_stage_id", "Id of the arc stage to update.", required: true)
.Str("arcStageId", "Id of the arc stage to update.", required: true)
.Str("title", "New title for the stage.")
.Build(),
async (_, input, ct) =>
{
var arcStageId = JsonInput.RequiredGuid(input, "arc_stage_id");
var arcStageId = JsonInput.RequiredGuid(input, "arcStageId");
return await OrNotFound(arcs.UpdateAsync(
arcStageId,
new UpdateArcStageRequest(
JsonInput.String(input, "title"),
JsonInput.Int(input, "sort_order"),
JsonInput.Int(input, "sortOrder"),
JsonInput.String(input, "description"),
JsonInput.Guid(input, "chapter_id")), ct), s => s.ToResponse(), "CharacterArcStage", arcStageId);
JsonInput.Guid(input, "chapterId")), ct), s => s.ToResponse(), "CharacterArcStage", arcStageId);
});
yield return new AgentTool(
"delete_arc_stage",
"Remove a stage from a character's arc.",
new JsonSchemaBuilder()
.Str("arc_stage_id", "Id of the arc stage to delete.", required: true)
.Str("arcStageId", "Id of the arc stage to delete.", required: true)
.Build(),
async (_, input, ct) =>
{
var arcStageId = JsonInput.RequiredGuid(input, "arc_stage_id");
var arcStageId = JsonInput.RequiredGuid(input, "arcStageId");
return await DeletedOrNotFound(arcs.DeleteAsync(arcStageId, ct), "CharacterArcStage", arcStageId);
});
@@ -598,16 +760,35 @@ public class NovelAgentToolset(
"Renumber a character's arc to match the order given. Stages left out keep their "
+ "relative position after the ones listed.",
new JsonSchemaBuilder()
.Str("character_id", "Id of the character whose arc to reorder.", required: true)
.StringArray("stage_ids", "Arc stage ids in the order wanted.", required: true)
.Str("characterId", "Id of the character whose arc to reorder.", required: true)
.StringArray("stageIds", "Arc stage ids in the order wanted.", required: true)
.Build(),
async (_, input, ct) =>
{
var characterId = JsonInput.RequiredGuid(input, "character_id");
var characterId = JsonInput.RequiredGuid(input, "characterId");
return await OrNotFound(arcs.ReorderAsync(
characterId,
new ReorderArcStagesRequest(
[.. JsonInput.Strings(input, "stage_ids")?.Select(Guid.Parse) ?? []]), ct), list => list.Select(s => s.ToResponse()), "Character", characterId);
[.. JsonInput.Strings(input, "stageIds")?.Select(Guid.Parse) ?? []]), ct), list => list.Select(s => s.ToResponse()), "Character", characterId);
});
yield return new AgentTool(
"set_arc_stage_beats",
"Set which beats belong to an arc stage, replacing its current set. This groups the "
+ "chapter-level beats that establish or pay off this stage of the character's arc. A "
+ "beat moved into this stage leaves any other stage of the same character it was in. "
+ "Each beat must already include this character.",
new JsonSchemaBuilder()
.Str("arcStageId", "Id of the arc stage.", required: true)
.StringArray("beatIds", "Beat ids that belong to this stage, replacing whatever was there before.", required: true)
.Build(),
async (_, input, ct) =>
{
var arcStageId = JsonInput.RequiredGuid(input, "arcStageId");
return await OrNotFound(arcs.SetBeatsAsync(
arcStageId,
new SetArcStageBeatsRequest(
[.. JsonInput.Strings(input, "beatIds")?.Select(Guid.Parse) ?? []]), ct), s => s.ToResponse(), "CharacterArcStage", arcStageId);
});
yield return new AgentTool(
@@ -615,22 +796,23 @@ public class NovelAgentToolset(
"The decisions the writer has not made yet. Read this before proposing changes — an "
+ "open question is a place the writer is still thinking, not a gap to fill in for them.",
new JsonSchemaBuilder()
.Str("chapter_id", "Narrow to questions about one chapter outline.")
.Str("character_id", "Narrow to questions about one character.")
.Bool("include_resolved", "Include questions already settled. Defaults to false.")
.Str("chapterId", "Narrow to questions about one chapter outline.")
.Str("characterId", "Narrow to questions about one character.")
.Bool("includeResolved", "Include questions already settled. Defaults to false.")
.Build(),
async (novelId, input, ct) =>
{
var list = await questions.ListAsync(
novelId,
JsonInput.Guid(input, "chapter_id"),
JsonInput.Guid(input, "character_id"),
JsonInput.Bool(input, "include_resolved") ?? false,
JsonInput.Guid(input, "chapterId"),
JsonInput.Guid(input, "characterId"),
JsonInput.Bool(input, "includeResolved") ?? false,
ct);
var displayNumbers = await chapterLabels.ForNovelAsync(novelId, ct);
return list.Select(q => q.ToResponse(displayNumbers));
});
},
RequiresNovelId: true);
yield return new AgentTool(
"raise_open_question",
@@ -640,8 +822,8 @@ public class NovelAgentToolset(
new JsonSchemaBuilder()
.Str("question", "The question, in one line.", required: true)
.Str("detail", "The thinking around it — options, and what each costs.")
.Str("chapter_id", "The chapter outline this is about, if any.")
.Str("character_id", "The character this is about, if any.")
.Str("chapterId", "The chapter outline this is about, if any.")
.Str("characterId", "The character this is about, if any.")
.Build(),
async (novelId, input, ct) =>
{
@@ -650,8 +832,8 @@ public class NovelAgentToolset(
new CreateOpenQuestionRequest(
JsonInput.RequiredString(input, "question"),
JsonInput.String(input, "detail"),
JsonInput.Guid(input, "chapter_id"),
JsonInput.Guid(input, "character_id")), ct);
JsonInput.Guid(input, "chapterId"),
JsonInput.Guid(input, "characterId")), ct);
if (question is null)
{
@@ -660,25 +842,60 @@ public class NovelAgentToolset(
var displayNumbers = await chapterLabels.ForNovelAsync(novelId, ct);
return question.ToResponse(displayNumbers);
},
RequiresNovelId: true);
yield return new AgentTool(
"update_open_question",
"Revise a question or change what it is attached to. Only the fields you supply change.",
new JsonSchemaBuilder()
.Str("questionId", "Id of the question to update.", required: true)
.Str("question", "New wording for the question.")
.Str("detail", "New detail. Pass an empty string to clear it.")
.Str("chapterId", "Attach to this chapter outline.")
.Str("characterId", "Attach to this character.")
.Bool("clearChapter", "Detach from its chapter.")
.Bool("clearCharacter", "Detach from its character.")
.Build(),
async (_, input, ct) =>
{
var questionId = JsonInput.RequiredGuid(input, "questionId");
var question = await questions.UpdateAsync(
questionId,
new UpdateOpenQuestionRequest(
JsonInput.String(input, "question"),
JsonInput.String(input, "detail"),
JsonInput.Guid(input, "chapterId"),
JsonInput.Guid(input, "characterId"),
JsonInput.Bool(input, "clearChapter") ?? false,
JsonInput.Bool(input, "clearCharacter") ?? false), ct);
if (question is null)
{
return new ToolNotFound("OpenQuestion", questionId);
}
var displayNumbers = await chapterLabels.ForNovelAsync(question.NovelId, ct);
return question.ToResponse(displayNumbers);
});
yield return new AgentTool(
"resolve_open_question",
"Settle a question with what the writer decided. Set append_to_notes to also write "
"Settle a question with what the writer decided. Set appendToNotes to also write "
+ "the resolution into the notes of the chapter and character it hangs off.",
new JsonSchemaBuilder()
.Str("question_id", "Id of the question to resolve.", required: true)
.Str("questionId", "Id of the question to resolve.", required: true)
.Str("resolution", "What was decided.", required: true)
.Bool("append_to_notes", "Also append the resolution to the associated notes.")
.Bool("appendToNotes", "Also append the resolution to the associated notes.")
.Build(),
async (_, input, ct) =>
{
var questionId = JsonInput.RequiredGuid(input, "question_id");
var questionId = JsonInput.RequiredGuid(input, "questionId");
var question = await questions.ResolveAsync(
questionId,
new ResolveOpenQuestionRequest(
JsonInput.RequiredString(input, "resolution"),
JsonInput.Bool(input, "append_to_notes") ?? false), ct);
JsonInput.Bool(input, "appendToNotes") ?? false), ct);
if (question is null)
{
@@ -693,11 +910,11 @@ public class NovelAgentToolset(
"reopen_question",
"Put a resolved question back on the list. Anything already appended to notes stays.",
new JsonSchemaBuilder()
.Str("question_id", "Id of the question to reopen.", required: true)
.Str("questionId", "Id of the question to reopen.", required: true)
.Build(),
async (_, input, ct) =>
{
var questionId = JsonInput.RequiredGuid(input, "question_id");
var questionId = JsonInput.RequiredGuid(input, "questionId");
var question = await questions.ReopenAsync(questionId, ct);
if (question is null)
{
@@ -712,20 +929,20 @@ public class NovelAgentToolset(
"delete_open_question",
"Delete a question outright. Resolving is usually better — it keeps the decision.",
new JsonSchemaBuilder()
.Str("question_id", "Id of the question to delete.", required: true)
.Str("questionId", "Id of the question to delete.", required: true)
.Build(),
async (_, input, ct) =>
{
var questionId = JsonInput.RequiredGuid(input, "question_id");
var questionId = JsonInput.RequiredGuid(input, "questionId");
return await DeletedOrNotFound(questions.DeleteAsync(questionId, ct), "OpenQuestion", questionId);
});
}
private static JsonSchemaBuilder ArcStageSchema() =>
new JsonSchemaBuilder()
.Int("sort_order", "Position in the arc. Appended to the end when omitted.")
.Int("sortOrder", "Position in the arc. Appended to the end when omitted.")
.Str("description", "What shifts in the character here, and what it costs them.")
.Str("chapter_id", "The chapter where this stage lands, if it is pinned to one.");
.Str("chapterId", "The chapter where this stage lands, if it is pinned to one.");
private static JsonSchemaBuilder CharacterSchema(bool includeName, bool nameRequired)
{
@@ -759,9 +976,9 @@ public class NovelAgentToolset(
private static JsonSchemaBuilder BeatSchema() =>
new JsonSchemaBuilder()
.Int("sort_order", "Position in the chapter. Appended to the end when omitted.")
.StringArray("character_ids", "Ids of the characters whose beat this is. Replaces the existing list.")
.Str("what_happened", "The event itself.")
.Str("whats_next", "What it sets in motion — the hook into the next beat.")
.Int("sortOrder", "Position in the chapter. Appended to the end when omitted.")
.StringArray("characterIds", "Ids of the characters whose beat this is. Replaces the existing list.")
.Str("whatHappened", "The event itself.")
.Str("whatsNext", "What it sets in motion — the hook into the next beat.")
.StringArray("tags", "Tags for cross-referencing. Replaces the existing tags.");
}
@@ -15,11 +15,13 @@ using Novelly.Api.Data;
using Novelly.Api.Genres;
using Novelly.Api.Imports;
using Novelly.Api.Locations;
using Novelly.Api.Mcp;
using Novelly.Api.Novels;
using Novelly.Api.Questions;
using Novelly.Api.Tags;
using Novelly.Api.Trash;
using Novelly.Api.Users;
using ModelContextProtocol.Protocol;
namespace Novelly.Api.Common;
@@ -111,6 +113,19 @@ public static class NovellyServiceRegistration
services.AddModelValidatorsFromAssemblyContaining<Program>();
services.AddMcpServer(options => options.ServerInfo = new Implementation { Name = "novelly", Version = "1.0.0" })
.WithHttpTransport()
.WithListToolsHandler((request, ct) =>
{
var toolset = request.Services!.GetRequiredService<NovelAgentToolset>();
return ValueTask.FromResult(new ListToolsResult { Tools = [.. NovelMcpTools.Describe(toolset.Definitions)] });
})
.WithCallToolHandler((request, ct) =>
{
var toolset = request.Services!.GetRequiredService<NovelAgentToolset>();
return new ValueTask<CallToolResult>(NovelMcpTools.CallAsync(toolset, toolset.Definitions, request.Params!, ct));
});
return services;
}
}
+10
View File
@@ -0,0 +1,10 @@
namespace Novelly.Api.Mcp;
public static class McpEndpoints
{
public static IEndpointRouteBuilder MapNovelMcp(this IEndpointRouteBuilder app)
{
app.MapMcp("/mcp");
return app;
}
}
+109
View File
@@ -0,0 +1,109 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using ModelContextProtocol.Protocol;
using Novelly.Api.Agent;
namespace Novelly.Api.Mcp;
public static class NovelMcpTools
{
private const string NovelIdProperty = "novelId";
public static IReadOnlyList<Tool> Describe(IReadOnlyList<AgentToolDefinition> definitions) =>
[.. definitions.Select(definition => new Tool
{
Name = definition.Name,
Description = definition.Description,
InputSchema = definition.RequiresNovelId ? WithNovelId(definition.InputSchema) : definition.InputSchema
})];
public static async Task<CallToolResult> CallAsync(
NovelAgentToolset toolset,
IReadOnlyList<AgentToolDefinition> definitions,
CallToolRequestParams parameters,
CancellationToken ct)
{
var definition = definitions.FirstOrDefault(d => d.Name == parameters.Name);
if (definition is null)
{
return new CallToolResult
{
IsError = true,
Content = [new TextContentBlock { Text = $"No such tool: '{parameters.Name}'." }]
};
}
var arguments = ToJsonElement(parameters.Arguments);
Guid novelId;
if (definition.RequiresNovelId)
{
try
{
novelId = JsonInput.RequiredGuid(arguments, NovelIdProperty);
}
catch (ArgumentException ex)
{
return new CallToolResult { IsError = true, Content = [new TextContentBlock { Text = ex.Message }] };
}
}
else
{
novelId = Guid.Empty;
}
var result = await toolset.ExecuteAsync(parameters.Name, novelId, arguments, ct);
return new CallToolResult
{
IsError = result.IsError,
Content = [new TextContentBlock { Text = result.Content }]
};
}
private static JsonElement ToJsonElement(IDictionary<string, JsonElement>? arguments)
{
if (arguments is null)
{
return JsonSerializer.Deserialize<JsonElement>("{}");
}
var obj = new JsonObject();
foreach (var (key, value) in arguments)
{
obj[key] = JsonNode.Parse(value.GetRawText());
}
return JsonSerializer.Deserialize<JsonElement>(obj.ToJsonString());
}
private static JsonElement WithNovelId(JsonElement schema)
{
var node = JsonNode.Parse(schema.GetRawText())!.AsObject();
var properties = new JsonObject
{
[NovelIdProperty] = new JsonObject
{
["type"] = "string",
["description"] = "The novel's id."
}
};
if (node["properties"] is JsonObject existingProperties)
{
foreach (var (key, value) in existingProperties.ToList())
{
existingProperties.Remove(key);
properties[key] = value;
}
}
node["properties"] = properties;
var required = node["required"] as JsonArray ?? [];
required.Insert(0, NovelIdProperty);
node["required"] = required;
return JsonSerializer.Deserialize<JsonElement>(node.ToJsonString());
}
}
+1
View File
@@ -14,6 +14,7 @@
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.10" />
<PackageReference Include="Microsoft.OpenApi" Version="2.11.0" />
<PackageReference Include="ModelContextProtocol.AspNetCore" Version="2.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="10.0.1" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="3.0.5" />
+2
View File
@@ -12,6 +12,7 @@ using Novelly.Api.Data;
using Novelly.Api.Genres;
using Novelly.Api.Imports;
using Novelly.Api.Locations;
using Novelly.Api.Mcp;
using Novelly.Api.Novels;
using Novelly.Api.Questions;
using Novelly.Api.Tags;
@@ -112,6 +113,7 @@ if (app.Environment.IsDevelopment())
}
app.MapDefaultEndpoints();
app.MapNovelMcp();
app.MapGet("/api/health", () => Results.Ok(new { status = "ok" })).WithTags("Health").AllowAnonymous();
app.MapUiSettingsEndpoints();
-101
View File
@@ -1,101 +0,0 @@
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;
}
}
}
-17
View File
@@ -1,17 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.10" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.10" />
<PackageReference Include="ModelContextProtocol" Version="2.1.0" />
</ItemGroup>
</Project>
-31
View File
@@ -1,31 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Novelly.Mcp;
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddConsole(options => options.LogToStandardErrorThreshold = LogLevel.Trace);
builder.Logging.SetMinimumLevel(LogLevel.Warning);
var apiBaseUrl = builder.Configuration["NOVELLY_API_URL"] ?? "http://localhost:5080";
var apiKey = builder.Configuration["NOVELLY_API_KEY"];
builder.Services.AddHttpClient<NovelApiClient>(client =>
{
client.BaseAddress = new Uri(apiBaseUrl);
client.Timeout = TimeSpan.FromSeconds(30);
if (!string.IsNullOrWhiteSpace(apiKey))
{
client.DefaultRequestHeaders.Add("X-Novelly-Api-Key", apiKey);
}
});
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
await builder.Build().RunAsync();
-92
View File
@@ -1,92 +0,0 @@
using System.ComponentModel;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
namespace Novelly.Mcp.Tools;
[McpServerToolType]
public static class BeatTools
{
[McpServerTool(Name = "get_chapter_outline")]
[Description("Read a chapter's outline: its beats in order. Each beat is one row — a short "
+ "title, who it belongs to, what happened, and what it sets up. The chapter's "
+ "summary paragraph and drafted prose sit on the chapter itself, via get_chapter.")]
public static Task<CallToolResult> GetChapterOutline(
NovelApiClient api,
[Description("The chapter's id.")] Guid chapterId,
CancellationToken ct) =>
api.GetAsync($"/api/chapters/{chapterId}/beats", ct);
[McpServerTool(Name = "create_beat")]
[Description("Add a beat to a chapter's outline. Keep the title to three to five words — it "
+ "is a handle, not a sentence; detail belongs in whatHappened and whatsNext.")]
public static Task<CallToolResult> CreateBeat(
NovelApiClient api,
[Description("The chapter's id.")] Guid chapterId,
[Description("Three to five words naming the beat.")] string title,
CancellationToken ct,
[Description("Position in the chapter. Appended to the end when omitted.")] int? sortOrder = null,
[Description("Ids of the characters whose beat this is.")] Guid[]? characterIds = null,
[Description("The event itself.")] string? whatHappened = null,
[Description("What it sets in motion — the hook into the next beat.")] string? whatsNext = null,
[Description("Tags for cross-referencing. Unknown tags are created.")] string[]? tags = null) =>
api.PostAsync($"/api/chapters/{chapterId}/beats",
new { title, sortOrder, characterIds, whatHappened, whatsNext, tags }, ct);
[McpServerTool(Name = "update_beat")]
[Description("Revise a beat. Only the fields you supply change. Supplying a characterIds or "
+ "tag list replaces the beat's characters or tags outright — pass an empty list "
+ "to clear one, and include everything you want to keep.")]
public static Task<CallToolResult> UpdateBeat(
NovelApiClient api,
[Description("The beat's id.")] Guid beatId,
CancellationToken ct,
[Description("Three to five words naming the beat.")] string? title = null,
[Description("Position in the chapter.")] int? sortOrder = null,
[Description("Ids of the characters whose beat this is. Replaces the existing list.")] Guid[]? characterIds = null,
[Description("The event itself.")] string? whatHappened = null,
[Description("What it sets in motion.")] string? whatsNext = null,
[Description("Tags for cross-referencing. Replaces the existing tags.")] string[]? tags = null) =>
api.PatchAsync($"/api/beats/{beatId}",
new { title, sortOrder, characterIds, whatHappened, whatsNext, tags }, ct);
[McpServerTool(Name = "delete_beat")]
[Description("Remove a beat from a chapter's outline. Confirm with the writer first.")]
public static Task<CallToolResult> DeleteBeat(
NovelApiClient api,
[Description("The beat's id.")] Guid beatId,
CancellationToken ct) =>
api.DeleteAsync($"/api/beats/{beatId}", ct);
[McpServerTool(Name = "assign_character_to_beats")]
[Description("Add a character to several beats at once. Leaves each beat's existing characters "
+ "and other fields alone — this only adds, it never removes.")]
public static Task<CallToolResult> AssignCharacterToBeats(
NovelApiClient api,
[Description("The chapter's id.")] Guid chapterId,
[Description("Id of the character to add.")] Guid characterId,
[Description("Ids of the beats to add the character to.")] Guid[] beatIds,
CancellationToken ct) =>
api.PostAsync($"/api/chapters/{chapterId}/beats/assign-character", new { characterId, beatIds }, ct);
[McpServerTool(Name = "reorder_beats")]
[Description("Renumber a chapter's beats to match the order given. List every beat id in the "
+ "order wanted; any left out keep their relative position at the end.")]
public static Task<CallToolResult> ReorderBeats(
NovelApiClient api,
[Description("The chapter's id.")] Guid chapterId,
[Description("Beat ids in their new order.")] Guid[] beatIds,
CancellationToken ct) =>
api.PostAsync($"/api/chapters/{chapterId}/beats/reorder", new { beatIds }, ct);
[McpServerTool(Name = "move_beats")]
[Description("Move one or more beats from one chapter to another, appending them to the "
+ "target chapter's end in the order given.")]
public static Task<CallToolResult> MoveBeats(
NovelApiClient api,
[Description("The beats' current chapter id.")] Guid chapterId,
[Description("Id of the chapter to move the beats into.")] Guid targetChapterId,
[Description("Ids of the beats to move.")] Guid[] beatIds,
CancellationToken ct) =>
api.PostAsync($"/api/chapters/{chapterId}/beats/move", new { targetChapterId, beatIds }, ct);
}
-226
View File
@@ -1,226 +0,0 @@
using System.ComponentModel;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
namespace Novelly.Mcp.Tools;
[McpServerToolType]
public static class CharacterTools
{
[McpServerTool(Name = "list_characters")]
[Description("List a novel's character dossiers in full, including their relationships.")]
public static Task<CallToolResult> ListCharacters(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
CancellationToken ct) =>
api.GetAsync($"/api/novels/{novelId}/characters", ct);
[McpServerTool(Name = "get_character")]
[Description("Read one character's dossier.")]
public static Task<CallToolResult> GetCharacter(
NovelApiClient api,
[Description("The character's id.")] Guid characterId,
CancellationToken ct) =>
api.GetAsync($"/api/characters/{characterId}", ct);
[McpServerTool(Name = "create_character")]
[Description("Add a character dossier to a novel. Name is the only requirement — leave a field "
+ "blank when the writer has not decided it yet rather than inventing detail.")]
public static Task<CallToolResult> CreateCharacter(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
[Description("The character's name.")] string name,
CancellationToken ct,
[Description("Protagonist, Antagonist, Deuteragonist, Supporting, Minor, Mentor, LoveInterest or Foil.")]
string? role = null,
[Description("Main or Supporting. Main characters are the few the story is about and are worth tracking an arc for.")]
string? importance = null,
[Description("Age, exact or approximate.")] string? age = null,
[Description("The pronouns this character uses.")] string? pronouns = null,
[Description("What they do.")] string? occupation = null,
[Description("How they look.")] string? appearance = null,
[Description("Temperament, habits, how they treat people.")] string? personality = null,
[Description("History that shapes who they are now.")] string? backstory = null,
[Description("What they consciously pursue, weighed against what they actually need.")] string? motivation = null,
[Description("The war inside them and what in the world opposes them.")] string? conflict = null,
[Description("Speech patterns and register that make their dialogue theirs.")] string? voice = null,
[Description("Anything else worth recording.")] string? notes = null,
[Description("Tags for cross-referencing. Unknown tags are created.")] string[]? tags = null,
[Description("Other names this character is known by.")] string[]? aliases = null) =>
api.PostAsync($"/api/novels/{novelId}/characters", new
{
name,
role = role ?? "Supporting",
importance = importance ?? "Supporting",
age,
pronouns,
occupation,
appearance,
personality,
backstory,
motivation,
conflict,
voice,
notes,
tags,
aliases
}, ct);
[McpServerTool(Name = "update_character")]
[Description("Revise an existing character dossier. Only the fields you supply change.")]
public static Task<CallToolResult> UpdateCharacter(
NovelApiClient api,
[Description("The character's id.")] Guid characterId,
CancellationToken ct,
[Description("New name.")] string? name = null,
[Description("Protagonist, Antagonist, Deuteragonist, Supporting, Minor, Mentor, LoveInterest or Foil.")]
string? role = null,
[Description("Main or Supporting. Main characters are the few the story is about and are worth tracking an arc for.")]
string? importance = null,
[Description("Age, exact or approximate.")] string? age = null,
[Description("The pronouns this character uses.")] string? pronouns = null,
[Description("What they do.")] string? occupation = null,
[Description("How they look.")] string? appearance = null,
[Description("Temperament, habits, how they treat people.")] string? personality = null,
[Description("History that shapes who they are now.")] string? backstory = null,
[Description("What they consciously pursue, weighed against what they actually need.")] string? motivation = null,
[Description("The war inside them and what in the world opposes them.")] string? conflict = null,
[Description("Speech patterns and register.")] string? voice = null,
[Description("Anything else worth recording.")] string? notes = null,
[Description("Tags for cross-referencing. Replaces the existing tags.")] string[]? tags = null,
[Description("Other names this character is known by. Replaces the existing aliases.")] string[]? aliases = null) =>
api.PatchAsync($"/api/characters/{characterId}", new
{
name,
role,
importance,
age,
pronouns,
occupation,
appearance,
personality,
backstory,
motivation,
conflict,
voice,
notes,
tags,
aliases
}, ct);
[McpServerTool(Name = "get_character_beats")]
[Description("Every beat this character appears in, across the whole book, in manuscript order. "
+ "This is what the character actually does on the page, as opposed to what the "
+ "dossier claims about them — read it before revising a character.")]
public static Task<CallToolResult> GetCharacterBeats(
NovelApiClient api,
[Description("The character's id.")] Guid characterId,
CancellationToken ct) =>
api.GetAsync($"/api/characters/{characterId}/beats", ct);
[McpServerTool(Name = "get_character_arc")]
[Description("Read a main character's arc: the ordered stages of how they change, each "
+ "optionally pinned to the chapter where it lands.")]
public static Task<CallToolResult> GetCharacterArc(
NovelApiClient api,
[Description("The character's id.")] Guid characterId,
CancellationToken ct) =>
api.GetAsync($"/api/characters/{characterId}/arc", ct);
[McpServerTool(Name = "add_arc_stage")]
[Description("Add a stage to a character's arc. Arcs are kept for main characters — promote "
+ "the character with update_character first if they are still Supporting.")]
public static Task<CallToolResult> AddArcStage(
NovelApiClient api,
[Description("Id of the character whose arc to add to.")] Guid characterId,
[Description("A short handle for the change, three to five words.")] string title,
CancellationToken ct,
[Description("What this stage of the arc results in for the character — what shifts, and what it costs them.")] string? result = null,
[Description("Id of the chapter where this stage lands, if it is pinned to one.")] Guid? chapterId = null,
[Description("Position in the arc. Appended to the end when omitted.")] int? sortOrder = null) =>
api.PostAsync($"/api/characters/{characterId}/arc",
new { title, sortOrder, result, chapterId }, ct);
[McpServerTool(Name = "update_arc_stage")]
[Description("Revise a stage of a character's arc. Only the fields you supply change.")]
public static Task<CallToolResult> UpdateArcStage(
NovelApiClient api,
[Description("The arc stage's id.")] Guid arcStageId,
CancellationToken ct,
[Description("New title for the stage.")] string? title = null,
[Description("What this stage of the arc results in for the character.")] string? result = null,
[Description("Id of the chapter where this stage lands.")] Guid? chapterId = null,
[Description("Position in the arc.")] int? sortOrder = null) =>
api.PatchAsync($"/api/arc-stages/{arcStageId}",
new { title, sortOrder, result, chapterId }, ct);
[McpServerTool(Name = "delete_arc_stage")]
[Description("Remove a stage from a character's arc.")]
public static Task<CallToolResult> DeleteArcStage(
NovelApiClient api,
[Description("The arc stage's id.")] Guid arcStageId,
CancellationToken ct) =>
api.DeleteAsync($"/api/arc-stages/{arcStageId}", ct);
[McpServerTool(Name = "reorder_arc_stages")]
[Description("Renumber a character's arc to match the order given. Stages left out keep their "
+ "relative position after the ones listed.")]
public static Task<CallToolResult> ReorderArcStages(
NovelApiClient api,
[Description("Id of the character whose arc to reorder.")] Guid characterId,
[Description("Arc stage ids in the order wanted.")] string[] stageIds,
CancellationToken ct) =>
api.PostAsync($"/api/characters/{characterId}/arc/reorder", new { stageIds }, ct);
[McpServerTool(Name = "set_arc_stage_beats")]
[Description("Set which beats belong to an arc stage, replacing its current set. This groups the "
+ "chapter-level beats that establish or pay off this stage of the character's arc. A "
+ "beat moved into this stage leaves any other stage of the same character it was in. "
+ "Each beat must already include this character.")]
public static Task<CallToolResult> SetArcStageBeats(
NovelApiClient api,
[Description("The arc stage's id.")] Guid arcStageId,
[Description("Beat ids that belong to this stage, replacing whatever was there before.")] string[] beatIds,
CancellationToken ct) =>
api.PostAsync($"/api/arc-stages/{arcStageId}/beats", new { beatIds }, ct);
[McpServerTool(Name = "relate_characters")]
[Description("Record a relationship between two characters in the same novel. Creates both directions "
+ "at once — characterId's side and relatedCharacterId's side — so the pair always shows up "
+ "on both dossiers.")]
public static Task<CallToolResult> RelateCharacters(
NovelApiClient api,
[Description("Id of the character the relationship belongs to.")] Guid characterId,
[Description("Id of the character they are related to.")] Guid relatedCharacterId,
[Description("How characterId is related to relatedCharacterId, e.g. 'sister', 'rival', 'former mentor'.")] string relationshipType,
CancellationToken ct,
[Description("How relatedCharacterId is related back to characterId, if different — e.g. 'brother' for "
+ "'sister'. Defaults to relationshipType when the relation is symmetric, like 'rival'.")]
string? reciprocalRelationshipType = null,
[Description("What the relationship is like, and where it is headed.")] string? description = null) =>
api.PostAsync($"/api/characters/{characterId}/relationships",
new { relatedCharacterId, relationshipType, reciprocalRelationshipType, description }, ct);
[McpServerTool(Name = "link_character_identity")]
[Description("Record that this character is really another character — e.g. a character introduced "
+ "under one name who is later revealed to be a character already in the novel under "
+ "another name. Both characters keep their own dossier and beats; the canonical identity "
+ "is whichever character you link to.")]
public static Task<CallToolResult> LinkCharacterIdentity(
NovelApiClient api,
[Description("Id of the character being revealed as someone else.")] Guid characterId,
[Description("Id of the character this one really is.")] Guid sameCharacterAsId,
CancellationToken ct,
[Description("Id of the chapter where the reveal happens, if any.")] Guid? revealedInChapterId = null,
[Description("Context on the reveal, e.g. how and why the disguise held.")] string? note = null) =>
api.PutAsync($"/api/characters/{characterId}/identity",
new { sameCharacterAsId, revealedInChapterId, note }, ct);
[McpServerTool(Name = "unlink_character_identity")]
[Description("Remove a character's identity link, restoring it to its own separate identity.")]
public static Task<CallToolResult> UnlinkCharacterIdentity(
NovelApiClient api,
[Description("The character's id.")] Guid characterId,
CancellationToken ct) =>
api.DeleteAsync($"/api/characters/{characterId}/identity", ct);
}
-54
View File
@@ -1,54 +0,0 @@
using System.ComponentModel;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
namespace Novelly.Mcp.Tools;
[McpServerToolType]
public static class LocationTools
{
[McpServerTool(Name = "list_locations")]
[Description("List a novel's locations with how many chapters are set there. "
+ "Read this before inventing a new location so you reuse the writer's vocabulary.")]
public static Task<CallToolResult> ListLocations(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
CancellationToken ct) =>
api.GetAsync($"/api/novels/{novelId}/locations", ct);
[McpServerTool(Name = "get_location_references")]
[Description("Cross-reference a location: every chapter set there.")]
public static Task<CallToolResult> GetLocationReferences(
NovelApiClient api,
[Description("The location's id.")] Guid locationId,
CancellationToken ct) =>
api.GetAsync($"/api/locations/{locationId}/references", ct);
[McpServerTool(Name = "create_location")]
[Description("Create a location explicitly. Applying an unknown location by name to a chapter "
+ "also creates it, so this is only needed to set one up ahead of time.")]
public static Task<CallToolResult> CreateLocation(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
[Description("The location's name. Unique within the novel, matched case-insensitively.")] string name,
CancellationToken ct) =>
api.PostAsync($"/api/novels/{novelId}/locations", new { name }, ct);
[McpServerTool(Name = "update_location")]
[Description("Rename a location. Renaming updates it everywhere it is applied.")]
public static Task<CallToolResult> UpdateLocation(
NovelApiClient api,
[Description("The location's id.")] Guid locationId,
[Description("New name.")] string name,
CancellationToken ct) =>
api.PatchAsync($"/api/locations/{locationId}", new { name }, ct);
[McpServerTool(Name = "delete_location")]
[Description("Move a location to the trash. Whatever carried it is left alone — only the label goes. "
+ "It can be restored from the Trash page within the retention window.")]
public static Task<CallToolResult> DeleteLocation(
NovelApiClient api,
[Description("The location's id.")] Guid locationId,
CancellationToken ct) =>
api.DeleteAsync($"/api/locations/{locationId}", ct);
}
-75
View File
@@ -1,75 +0,0 @@
using System.ComponentModel;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
namespace Novelly.Mcp.Tools;
[McpServerToolType]
public static class ManuscriptTools
{
[McpServerTool(Name = "list_chapters")]
[Description("List a novel's chapters in manuscript order, with beat and word counts.")]
public static Task<CallToolResult> ListChapters(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
CancellationToken ct) =>
api.GetAsync($"/api/novels/{novelId}/chapters", ct);
[McpServerTool(Name = "get_chapter")]
[Description("Read one chapter in full: its outline (beats) and its drafted prose.")]
public static Task<CallToolResult> GetChapter(
NovelApiClient api,
[Description("The chapter's id.")] Guid chapterId,
CancellationToken ct) =>
api.GetAsync($"/api/chapters/{chapterId}", ct);
[McpServerTool(Name = "create_chapter")]
[Description("Add a chapter to a novel. It goes at the end of the manuscript unless you supply a number. "
+ "Use 'kind' for a foreword, prologue, afterword, or other unnumbered front/back matter.")]
public static Task<CallToolResult> CreateChapter(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
[Description("Chapter title.")] string title,
CancellationToken ct,
[Description("Manuscript position, 1-based, counting front and back matter.")] int? number = null,
[Description("FrontMatter, Body, or BackMatter. Defaults to Body.")] string? kind = null,
[Description("The chapter's outline summary paragraph.")] string? summary = null,
[Description("Where and when the chapter takes place. Unknown locations are created.")] string[]? locations = null,
[Description("Planned, Outlined, Drafted, Revised or Final.")] string? status = null,
[Description("Target length in words.")] int? targetWordCount = null,
[Description("The chapter's drafted text, in markdown, if you are writing it now.")] string? prose = null,
[Description("Tags for cross-referencing. Unknown tags are created.")] string[]? tags = null) =>
api.PostAsync($"/api/novels/{novelId}/chapters", new
{
title,
number,
kind = kind ?? "Body",
summary,
locations,
status = status ?? "Planned",
targetWordCount,
prose,
tags
}, ct);
[McpServerTool(Name = "update_chapter")]
[Description("Revise a chapter's title, number, kind, summary, locations, notes, status "
+ "or drafted prose. Use 'prose' to write or replace the chapter's draft text in "
+ "markdown; the word count is recomputed automatically.")]
public static Task<CallToolResult> UpdateChapter(
NovelApiClient api,
[Description("The chapter's id.")] Guid chapterId,
CancellationToken ct,
[Description("New title.")] string? title = null,
[Description("Manuscript position, 1-based, counting front and back matter.")] int? number = null,
[Description("FrontMatter, Body, or BackMatter.")] string? kind = null,
[Description("The chapter's outline summary paragraph.")] string? summary = null,
[Description("Where and when the chapter takes place. Replaces the existing locations. Unknown locations are created.")] string[]? locations = null,
[Description("Anything else worth recording.")] string? notes = null,
[Description("Planned, Outlined, Drafted, Revised or Final.")] string? status = null,
[Description("Target length in words.")] int? targetWordCount = null,
[Description("The chapter's drafted text, in markdown.")] string? prose = null,
[Description("Tags for cross-referencing. Replaces the existing tags.")] string[]? tags = null) =>
api.PatchAsync($"/api/chapters/{chapterId}",
new { title, number, kind, summary, locations, notes, status, targetWordCount, prose, tags }, ct);
}
-54
View File
@@ -1,54 +0,0 @@
using System.ComponentModel;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
namespace Novelly.Mcp.Tools;
[McpServerToolType]
public static class NovelTools
{
[McpServerTool(Name = "list_novels")]
[Description("List every novel, with counts of characters, chapters and drafted words. "
+ "Start here to find the novel id everything else needs.")]
public static Task<CallToolResult> ListNovels(NovelApiClient api, CancellationToken ct) =>
api.GetAsync("/api/novels", ct);
[McpServerTool(Name = "get_novel_brief")]
[Description("Read a novel's title, author, genre, logline, synopsis, notes and word-count target.")]
public static Task<CallToolResult> GetNovel(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
CancellationToken ct) =>
api.GetAsync($"/api/novels/{novelId}", ct);
[McpServerTool(Name = "create_novel")]
[Description("Create a new novel.")]
public static Task<CallToolResult> CreateNovel(
NovelApiClient api,
[Description("Working title.")] string title,
CancellationToken ct,
[Description("Author name.")] string? author = null,
[Description("Genre or category.")] string? genre = null,
[Description("One-sentence pitch.")] string? logline = null,
[Description("Paragraph-length summary of the whole book.")] string? synopsis = null,
[Description("Free-form notes on theme, tone, comparable titles.")] string? notes = null,
[Description("Target manuscript length in words.")] int? targetWordCount = null) =>
api.PostAsync("/api/novels", new { title, author, genre, logline, synopsis, notes, targetWordCount }, ct);
[McpServerTool(Name = "update_novel_brief")]
[Description("Revise a novel's top-level fields. Only the fields you supply change; "
+ "pass an empty string to clear one.")]
public static Task<CallToolResult> UpdateNovel(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
CancellationToken ct,
[Description("New title.")] string? title = null,
[Description("Author name.")] string? author = null,
[Description("Genre or category.")] string? genre = null,
[Description("One-sentence pitch.")] string? logline = null,
[Description("Paragraph-length summary of the whole book.")] string? synopsis = null,
[Description("Free-form notes on theme, tone, comparable titles.")] string? notes = null,
[Description("Target manuscript length in words.")] int? targetWordCount = null) =>
api.PatchAsync($"/api/novels/{novelId}",
new { title, author, genre, logline, synopsis, notes, targetWordCount }, ct);
}
-92
View File
@@ -1,92 +0,0 @@
using System.ComponentModel;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
namespace Novelly.Mcp.Tools;
[McpServerToolType]
public static class QuestionTools
{
[McpServerTool(Name = "list_open_questions")]
[Description("The decisions the writer has not made yet, newest first. Read this before "
+ "proposing changes — an open question marks somewhere the writer is still "
+ "thinking, not a gap to fill in for them.")]
public static Task<CallToolResult> ListOpenQuestions(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
CancellationToken ct,
[Description("Narrow to questions about one chapter outline.")] Guid? chapterId = null,
[Description("Narrow to questions about one character.")] Guid? characterId = null,
[Description("Include questions already settled. Defaults to false.")] bool includeResolved = false)
{
var query = new List<string> { $"includeResolved={includeResolved.ToString().ToLowerInvariant()}" };
if (chapterId is { } chapter)
{
query.Add($"chapterId={chapter}");
}
if (characterId is { } character)
{
query.Add($"characterId={character}");
}
return api.GetAsync($"/api/novels/{novelId}/questions?{string.Join('&', query)}", ct);
}
[McpServerTool(Name = "raise_open_question")]
[Description("Record a question the writer has not settled, attached to the chapter outline "
+ "and/or the character it is about. Prefer raising a question over guessing.")]
public static Task<CallToolResult> RaiseOpenQuestion(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
[Description("The question, in one line.")] string question,
CancellationToken ct,
[Description("The thinking around it — options considered, and what each costs.")] string? detail = null,
[Description("Id of the chapter outline this is about, if any.")] Guid? chapterId = null,
[Description("Id of the character this is about, if any.")] Guid? characterId = null) =>
api.PostAsync($"/api/novels/{novelId}/questions",
new { question, detail, chapterId, characterId }, ct);
[McpServerTool(Name = "update_open_question")]
[Description("Revise a question or change what it is attached to. Only the fields you supply change.")]
public static Task<CallToolResult> UpdateOpenQuestion(
NovelApiClient api,
[Description("The question's id.")] Guid questionId,
CancellationToken ct,
[Description("New wording for the question.")] string? question = null,
[Description("New detail. Pass an empty string to clear it.")] string? detail = null,
[Description("Attach to this chapter outline.")] Guid? chapterId = null,
[Description("Attach to this character.")] Guid? characterId = null,
[Description("Detach from its chapter.")] bool clearChapter = false,
[Description("Detach from its character.")] bool clearCharacter = false) =>
api.PatchAsync($"/api/questions/{questionId}",
new { question, detail, chapterId, characterId, clearChapter, clearCharacter }, ct);
[McpServerTool(Name = "resolve_open_question")]
[Description("Settle a question with what the writer decided. Set appendToNotes to also write "
+ "the resolution into the notes of the chapter and character it hangs off.")]
public static Task<CallToolResult> ResolveOpenQuestion(
NovelApiClient api,
[Description("The question's id.")] Guid questionId,
[Description("What was decided.")] string resolution,
CancellationToken ct,
[Description("Also append the resolution to the associated notes.")] bool appendToNotes = false) =>
api.PostAsync($"/api/questions/{questionId}/resolve", new { resolution, appendToNotes }, ct);
[McpServerTool(Name = "reopen_question")]
[Description("Put a resolved question back on the list. Anything already appended to notes stays.")]
public static Task<CallToolResult> ReopenQuestion(
NovelApiClient api,
[Description("The question's id.")] Guid questionId,
CancellationToken ct) =>
api.PostAsync($"/api/questions/{questionId}/reopen", new { }, ct);
[McpServerTool(Name = "delete_open_question")]
[Description("Delete a question outright. Resolving is usually better — it keeps the decision.")]
public static Task<CallToolResult> DeleteOpenQuestion(
NovelApiClient api,
[Description("The question's id.")] Guid questionId,
CancellationToken ct) =>
api.DeleteAsync($"/api/questions/{questionId}", ct);
}
-56
View File
@@ -1,56 +0,0 @@
using System.ComponentModel;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
namespace Novelly.Mcp.Tools;
[McpServerToolType]
public static class TagTools
{
[McpServerTool(Name = "list_tags")]
[Description("List a novel's tags with how many characters, chapters and beats carry each. "
+ "Read this before inventing a new tag so you reuse the writer's vocabulary.")]
public static Task<CallToolResult> ListTags(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
CancellationToken ct) =>
api.GetAsync($"/api/novels/{novelId}/tags", ct);
[McpServerTool(Name = "get_tag_references")]
[Description("Cross-reference a tag: every character, chapter and beat carrying it. Use this "
+ "to trace a motif, a thread, or a piece of setup through the book.")]
public static Task<CallToolResult> GetTagReferences(
NovelApiClient api,
[Description("The tag's id.")] Guid tagId,
CancellationToken ct) =>
api.GetAsync($"/api/tags/{tagId}/references", ct);
[McpServerTool(Name = "create_tag")]
[Description("Create a tag explicitly. Applying an unknown tag by name to a character, "
+ "chapter or beat also creates it, so this is only needed to set a colour up front.")]
public static Task<CallToolResult> CreateTag(
NovelApiClient api,
[Description("The novel's id.")] Guid novelId,
[Description("The tag's name. Unique within the novel, matched case-insensitively.")] string name,
CancellationToken ct,
[Description("Optional hex colour for the UI, e.g. \"#9a4a2f\".")] string? color = null) =>
api.PostAsync($"/api/novels/{novelId}/tags", new { name, color }, ct);
[McpServerTool(Name = "update_tag")]
[Description("Rename or recolour a tag. Renaming updates it everywhere it is applied.")]
public static Task<CallToolResult> UpdateTag(
NovelApiClient api,
[Description("The tag's id.")] Guid tagId,
CancellationToken ct,
[Description("New name.")] string? name = null,
[Description("Hex colour, e.g. \"#9a4a2f\".")] string? color = null) =>
api.PatchAsync($"/api/tags/{tagId}", new { name, color }, ct);
[McpServerTool(Name = "delete_tag")]
[Description("Delete a tag. Whatever carried it is left alone — only the label goes.")]
public static Task<CallToolResult> DeleteTag(
NovelApiClient api,
[Description("The tag's id.")] Guid tagId,
CancellationToken ct) =>
api.DeleteAsync($"/api/tags/{tagId}", ct);
}
@@ -100,7 +100,7 @@ public class NovelAgentServiceTests : ServiceTestFixture
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var model = new ScriptedModelClient([
[ToolUse("t1", "update_character", new { character_id = Guid.NewGuid().ToString(), name = "Ines" })],
[ToolUse("t1", "update_character", new { characterId = Guid.NewGuid().ToString(), name = "Ines" })],
[new AgentTextBlock("That character does not exist yet — shall I create her?")]
]);
@@ -185,6 +185,42 @@ public class NovelAgentServiceTests : ServiceTestFixture
});
}
[Test]
public async Task Continuing_a_conversation_under_a_different_novel_is_rejected()
{
var novelAId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var novelBId = (await Novels.CreateAsync(new CreateNovelRequest("A Different Book"))).Id;
var model = new ScriptedModelClient([
[new AgentTextBlock("First answer.")]
]);
var agent = BuildAgent(model);
var first = await agent.SendMessageAsync(novelAId, new SendAgentMessageRequest("Question one."));
var second = await agent.SendMessageAsync(
novelBId, new SendAgentMessageRequest("Question two.", first.ConversationId));
Assert.That(second, Is.Null);
}
[Test]
public async Task Continuing_a_conversation_under_its_own_novel_still_works()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var model = new ScriptedModelClient([
[new AgentTextBlock("First answer.")],
[new AgentTextBlock("Second answer.")]
]);
var agent = BuildAgent(model);
var first = await agent.SendMessageAsync(novelId, new SendAgentMessageRequest("Question one."));
var second = await agent.SendMessageAsync(
novelId, new SendAgentMessageRequest("Question two.", first.ConversationId));
Assert.That(second, Is.Not.Null);
Assert.That(second.ConversationId, Is.EqualTo(first.ConversationId));
}
[Test]
public void Every_tool_declares_an_object_schema_and_a_description()
{
@@ -0,0 +1,226 @@
using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions;
using Novelly.Api.Agent;
using Novelly.Api.Beats;
using Novelly.Api.Characters;
using Novelly.Api.Novels;
namespace Novelly.Api.Tests;
[TestFixture]
public class NovelAgentToolsetTests : ServiceTestFixture
{
private NovelAgentToolset _toolset = null!;
protected override void OnSetUp() =>
_toolset = new NovelAgentToolset(Novels, Characters, Arcs, Chapters, ChapterLabels, Beats, Tags, Locations, Questions, NullLogger<NovelAgentToolset>.Instance);
private static JsonElement Input(object value) => JsonSerializer.SerializeToElement(value);
[Test]
public void The_toolset_offers_every_tool_the_stdio_server_offered()
{
var expected = new[]
{
"list_novels", "create_novel", "get_novel_brief", "update_novel_brief",
"list_characters", "get_character", "create_character", "update_character",
"get_character_beats", "get_character_arc", "add_arc_stage", "update_arc_stage",
"delete_arc_stage", "reorder_arc_stages", "set_arc_stage_beats", "relate_characters",
"link_character_identity", "unlink_character_identity",
"get_chapter_outline", "create_beat", "update_beat", "delete_beat",
"assign_character_to_beats", "reorder_beats", "move_beats",
"list_chapters", "get_chapter", "create_chapter", "update_chapter",
"list_tags", "get_tag_references", "create_tag", "update_tag", "delete_tag",
"list_locations", "get_location_references", "create_location", "update_location", "delete_location",
"list_open_questions", "raise_open_question", "update_open_question",
"resolve_open_question", "reopen_question", "delete_open_question"
};
Assert.That(_toolset.Definitions.Select(t => t.Name), Is.EquivalentTo(expected));
}
[Test]
public void No_novel_scoped_tools_schema_already_declares_a_novel_id()
{
Assert.Multiple(() =>
{
foreach (var tool in _toolset.Definitions.Where(t => t.RequiresNovelId))
{
Assert.That(tool.InputSchema.TryGetProperty("novelId", out _), Is.False, tool.Name);
}
});
}
[Test]
public async Task Creating_a_novel_through_a_tool_makes_it_appear_in_the_novel_list()
{
var result = await _toolset.ExecuteAsync(
"create_novel", Guid.Empty, Input(new { title = "The Ash Ledger" }));
var novels = await Novels.ListAsync();
Assert.Multiple(() =>
{
Assert.That(result.IsError, Is.False);
Assert.That(novels.Select(n => n.Title), Has.Member("The Ash Ledger"));
});
}
[Test]
public async Task Reading_a_character_by_id_returns_its_dossier()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var characterId = (await Characters.CreateAsync(novelId, new CreateCharacterRequest("Ines"))).Id;
var result = await _toolset.ExecuteAsync(
"get_character", novelId, Input(new { characterId }));
Assert.Multiple(() =>
{
Assert.That(result.IsError, Is.False);
Assert.That(result.Content, Does.Contain("Ines"));
});
}
[Test]
public async Task Relating_two_characters_shows_the_pair_on_both_dossiers()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var inesId = (await Characters.CreateAsync(novelId, new CreateCharacterRequest("Ines"))).Id;
var marcoId = (await Characters.CreateAsync(novelId, new CreateCharacterRequest("Marco"))).Id;
var result = await _toolset.ExecuteAsync(
"relate_characters", novelId,
Input(new { characterId = inesId, relatedCharacterId = marcoId, relationshipType = "sister" }));
var ines = await Characters.GetAsync(inesId);
var marco = await Characters.GetAsync(marcoId);
Assert.Multiple(() =>
{
Assert.That(result.IsError, Is.False);
Assert.That(ines!.Relationships.Select(r => r.RelatedCharacterId), Has.Member(marcoId));
Assert.That(marco!.Relationships.Select(r => r.RelatedCharacterId), Has.Member(inesId));
});
}
[Test]
public async Task Setting_an_arc_stages_beats_replaces_whatever_was_there()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var characterId = (await Characters.CreateAsync(novelId, new CreateCharacterRequest("Ines", Importance: CharacterImportance.Main))).Id;
var chapterId = (await Chapters.CreateAsync(novelId, new Novelly.Api.Chapters.CreateChapterRequest("Chapter One"))).Id;
var beatId = (await Beats.CreateAsync(chapterId, new CreateBeatRequest("Arrival", CharacterIds: [characterId]))).Id;
var stageId = (await Arcs.CreateAsync(characterId, new CreateArcStageRequest("Doubt"))).Id;
var result = await _toolset.ExecuteAsync(
"set_arc_stage_beats", novelId, Input(new { arcStageId = stageId, beatIds = new[] { beatId.ToString() } }));
Assert.That(result.IsError, Is.False);
}
[Test]
public async Task Creating_a_tag_with_a_colour_then_renaming_it_keeps_it_on_what_carried_it()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var created = await _toolset.ExecuteAsync(
"create_tag", novelId, Input(new { name = "Foreshadowing", color = "#9a4a2f" }));
var tagsAfterCreate = await Tags.ListAsync(novelId);
var tagId = tagsAfterCreate.Single(t => t.Name == "Foreshadowing").Id;
var renamed = await _toolset.ExecuteAsync(
"update_tag", novelId, Input(new { tagId, name = "Setup" }));
var tagsAfterRename = await Tags.ListAsync(novelId);
Assert.Multiple(() =>
{
Assert.That(created.IsError, Is.False);
Assert.That(renamed.IsError, Is.False);
Assert.That(tagsAfterRename.Select(t => t.Name), Has.Member("Setup"));
Assert.That(tagsAfterRename.Select(t => t.Name), Has.No.Member("Foreshadowing"));
});
}
[Test]
public async Task Deleting_a_tag_leaves_the_characters_that_carried_it_alone()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var characterId = (await Characters.CreateAsync(
novelId, new CreateCharacterRequest("Ines", Tags: ["Foreshadowing"]))).Id;
var tagId = (await Tags.ListAsync(novelId)).Single(t => t.Name == "Foreshadowing").Id;
var result = await _toolset.ExecuteAsync("delete_tag", novelId, Input(new { tagId }));
var character = await Characters.GetAsync(characterId);
var tagsAfter = await Tags.ListAsync(novelId);
Assert.Multiple(() =>
{
Assert.That(result.IsError, Is.False);
Assert.That(tagsAfter, Is.Empty);
Assert.That(character, Is.Not.Null);
});
}
[Test]
public async Task Creating_and_renaming_a_location_updates_it_everywhere()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
await _toolset.ExecuteAsync("create_location", novelId, Input(new { name = "The Salt Flats" }));
var locationId = (await Locations.ListAsync(novelId)).Single(l => l.Name == "The Salt Flats").Id;
var renamed = await _toolset.ExecuteAsync(
"update_location", novelId, Input(new { locationId, name = "The Great Salt Flats" }));
var locationsAfter = await Locations.ListAsync(novelId);
Assert.Multiple(() =>
{
Assert.That(renamed.IsError, Is.False);
Assert.That(locationsAfter.Select(l => l.Name), Has.Member("The Great Salt Flats"));
});
}
[Test]
public async Task Updating_an_open_question_can_detach_it_from_its_chapter()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var chapterId = (await Chapters.CreateAsync(novelId, new Novelly.Api.Chapters.CreateChapterRequest("Chapter One"))).Id;
var questionId = (await Questions.CreateAsync(
novelId, new Novelly.Api.Questions.CreateOpenQuestionRequest("Who sent the letter?", ChapterId: chapterId)))!.Id;
var result = await _toolset.ExecuteAsync(
"update_open_question", novelId, Input(new { questionId, clearChapter = true }));
var question = await Questions.GetAsync(questionId);
Assert.Multiple(() =>
{
Assert.That(result.IsError, Is.False);
Assert.That(question!.ChapterId, Is.Null);
});
}
[Test]
public async Task Asking_for_an_unknown_id_comes_back_as_an_error_result()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var missingId = Guid.NewGuid();
Assert.Multiple(async () =>
{
var character = await _toolset.ExecuteAsync("get_character", novelId, Input(new { characterId = missingId }));
Assert.That(character.IsError, Is.True);
var tag = await _toolset.ExecuteAsync("update_tag", novelId, Input(new { tagId = missingId, name = "x" }));
Assert.That(tag.IsError, Is.True);
var location = await _toolset.ExecuteAsync("delete_location", novelId, Input(new { locationId = missingId }));
Assert.That(location.IsError, Is.True);
var question = await _toolset.ExecuteAsync("update_open_question", novelId, Input(new { questionId = missingId }));
Assert.That(question.IsError, Is.True);
});
}
}
@@ -0,0 +1,121 @@
using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions;
using ModelContextProtocol.Protocol;
using Novelly.Api.Agent;
using Novelly.Api.Mcp;
using Novelly.Api.Novels;
namespace Novelly.Api.Tests;
[TestFixture]
public class NovelMcpToolsTests : ServiceTestFixture
{
private NovelAgentToolset _toolset = null!;
protected override void OnSetUp() =>
_toolset = new NovelAgentToolset(Novels, Characters, Arcs, Chapters, ChapterLabels, Beats, Tags, Locations, Questions, NullLogger<NovelAgentToolset>.Instance);
private static CallToolRequestParams Params(string name, object? arguments = null) => new()
{
Name = name,
Arguments = arguments is null
? null
: JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(JsonSerializer.Serialize(arguments))
};
[Test]
public void Every_agent_tool_is_advertised_over_mcp()
{
var tools = NovelMcpTools.Describe(_toolset.Definitions);
Assert.Multiple(() =>
{
Assert.That(tools, Has.Count.EqualTo(45));
Assert.That(tools.Select(t => t.Name), Is.Unique);
Assert.That(tools, Has.All.Matches<Tool>(t => !string.IsNullOrWhiteSpace(t.Description)));
});
}
[Test]
public void Tools_that_work_on_a_whole_novel_advertise_a_required_novel_id()
{
var tool = NovelMcpTools.Describe(_toolset.Definitions).Single(t => t.Name == "list_tags");
Assert.Multiple(() =>
{
Assert.That(tool.InputSchema.GetProperty("properties").TryGetProperty("novelId", out _), Is.True);
Assert.That(
tool.InputSchema.GetProperty("required").EnumerateArray().Select(e => e.GetString()),
Has.Member("novelId"));
});
}
[Test]
public void Tools_addressed_by_child_id_do_not_advertise_a_novel_id()
{
var tool = NovelMcpTools.Describe(_toolset.Definitions).Single(t => t.Name == "delete_tag");
Assert.That(tool.InputSchema.GetProperty("properties").TryGetProperty("novelId", out _), Is.False);
}
[Test]
public void Listing_novels_over_mcp_needs_no_novel_id()
{
var tool = NovelMcpTools.Describe(_toolset.Definitions).Single(t => t.Name == "list_novels");
Assert.That(tool.InputSchema.GetProperty("properties").TryGetProperty("novelId", out _), Is.False);
}
[Test]
public void Advertised_schemas_stay_valid_json_schema_objects()
{
Assert.Multiple(() =>
{
foreach (var tool in NovelMcpTools.Describe(_toolset.Definitions))
{
Assert.That(tool.InputSchema.GetProperty("type").GetString(), Is.EqualTo("object"), tool.Name);
Assert.That(tool.InputSchema.TryGetProperty("properties", out _), Is.True, tool.Name);
}
});
}
[Test]
public async Task Calling_a_novel_scoped_tool_without_a_novel_id_comes_back_as_an_error_result()
{
var result = await NovelMcpTools.CallAsync(_toolset, _toolset.Definitions, Params("list_tags"), CancellationToken.None);
Assert.That(result.IsError, Is.True);
}
[Test]
public async Task Calling_a_tool_over_mcp_runs_it_against_the_novel_in_the_arguments()
{
var novelId = (await Novels.CreateAsync(new CreateNovelRequest("The Salt Road"))).Id;
var result = await NovelMcpTools.CallAsync(
_toolset, _toolset.Definitions, Params("get_novel_brief", new { novelId }), CancellationToken.None);
var text = ((TextContentBlock)result.Content![0]).Text;
Assert.Multiple(() =>
{
Assert.That(result.IsError, Is.False);
Assert.That(text, Does.Contain("The Salt Road"));
});
}
[Test]
public async Task A_tool_that_finds_nothing_comes_back_as_an_error_result()
{
var result = await NovelMcpTools.CallAsync(
_toolset, _toolset.Definitions, Params("get_character", new { characterId = Guid.NewGuid() }), CancellationToken.None);
var text = ((TextContentBlock)result.Content![0]).Text;
Assert.Multiple(() =>
{
Assert.That(result.IsError, Is.True);
Assert.That(text, Does.Contain("was not found"));
});
}
}