nutho-preset-editor
README.md
# Nutho Preset Editor
English | [ภาษาไทย](README.th.md)
An editor and Model Context Protocol (MCP) server for SillyTavern Chat Completion presets. A browser UI and AI agents (Claude Desktop, Claude Code, Cursor, Cline, and other MCP clients) work on the same in-memory document, with revision checks so neither can silently overwrite the other.
## Contents
- [Features](#features)
- [Requirements and setup](#requirements-and-setup)
- [Running](#running)
- [Connecting an MCP client](#connecting-an-mcp-client)
- [How a session works](#how-a-session-works)
- [Tool reference](#tool-reference)
- [Examples](#examples)
- [Resources and prompts](#resources-and-prompts)
- [Environment variables](#environment-variables)
- [Testing](#testing)
- [License](#license)
## Features
- One shared session. The web UI and every MCP client see the same document; changes are pushed to the browser through server-sent events.
- Optimistic concurrency. Every write carries the revision and document id it was based on; a stale write is rejected with the current values.
- Atomic batches with dry run, plus undo, redo, and revert.
- Safe saving. Writing requires explicit confirmation and makes a byte-exact backup of the previous file first.
- Fidelity to the SillyTavern format. Unknown fields, duplicate order references, marker rows, and legacy fields are preserved.
- Diagnostics: orphan references, duplicate identifiers, missing built-in prompts, empty enabled prompts, unknown macros, and an offline preview with token estimates.
- Agent-friendly MCP surface: every parameter is documented, errors carry a `hint`, replies are compact by default, and a `help` tool plus server instructions explain the workflow.
## Requirements and setup
- Node.js 22 or newer
- Windows, macOS, or Linux
```bash
git clone https://github.com/nattaphat010447/nutho-preset-editor.git
cd nutho-preset-editor
npm install
```
## Running
The server needs at least one root directory that contains your preset `.json` files, for example the SillyTavern `OpenAI Settings` folder. No directory is chosen automatically.
```bash
node server.js --root "/path/to/SillyTavern/data/default-user/OpenAI Settings" --port 4173
```
`--root` can be repeated. `PORT` selects the port (default 4173). The server listens on `127.0.0.1` only.
On Windows, `run.bat` starts the server with a default root and opens the browser; `start-server.ps1` is the PowerShell equivalent. Edit the root path and token at the top of `run.bat` for your machine.
`npm start` serves the legacy browser-only mode (`--static`), which has no shared session and no MCP.
## Connecting an MCP client
### stdio (recommended)
The stdio process checks whether the web server is running on `127.0.0.1:4173` (or `PORT`). If it is, MCP calls are forwarded to that live session, so the browser and the agent edit the same document. Otherwise it starts its own session over the roots you give it.
Roots can be passed as arguments or through `NUTHO_PRESET_ROOTS` (separate several with `;`). Add `--read-only` to block all writes.
Claude Desktop (`claude_desktop_config.json`), Cursor, Cline, and similar clients:
```json
{
"mcpServers": {
"nutho-preset-editor": {
"command": "node",
"args": ["C:/path/to/nutho-preset-editor/src/mcp/stdio.js"],
"env": {
"NUTHO_PRESET_ROOTS": "C:/path/to/SillyTavern/data/default-user/OpenAI Settings"
}
}
}
}
```
### Streamable HTTP
Start the web server with a token and connect a client to `/mcp`:
```bash
NUTHO_MCP_TOKEN="<random string of at least 32 characters>" node server.js --root "/path/to/presets"
```
- URL: `http://127.0.0.1:4173/mcp`
- Header: `Authorization: Bearer <your token>`
- POST only; the endpoint is stateless. Requests must use `Content-Type: application/json`, a loopback `Host`, and either no `Origin` or the same origin.
Choose your own token. Do not reuse the example token in `run.bat` outside a local test.
## How a session works
1. **One document at a time.** Open a file with `open`, create one with `scaffold_preset` or `create_default_st_preset`, or load text with `import_json` followed by `bind`.
2. **Every write is gated.** Pass `expectedRevision` and `expectedDocumentId`. Take them from `get_session` or from the previous write response. With no document open, use `0` and `"none"`.
3. **Changes live in memory** until `save` is called with `confirm: true`. `undo`, `redo`, and `revert` only affect memory.
4. **Indexes and identifiers.** Prompts are a flat array; order sets reference prompts by `identifier`. An index is a 0-based array position and shifts when items are added or removed, so tools accept `identifier` wherever possible.
5. **Order positions** are `{"kind":"start"}`, `{"kind":"end"}`, `{"kind":"before","identifier":"chatHistory"}`, or `{"kind":"after","entryIndex":3}`.
6. **Response size.** Write tools and `get_session` accept `responseMode`:
- `summary` (default): revision, document id, per-operation results, changed paths, counts.
- `diff`: the summary plus exact before and after values.
- `full`: the whole document (without the saved copy).
7. **Errors** have `code`, `message`, `details`, and usually a `hint` that names the next call to make.
## Tool reference
45 tools. Call `help` for a guide on the server itself; `help` with topic `operations` lists every `apply_batch` command with arguments and an example.
### Guide and session
| Tool | Purpose |
| :--- | :--- |
| `help` | Usage guide. Topics: overview, workflow, operations, read_methods, settings, errors, examples. |
| `get_session` | Current document: id, revision, dirty state, prompt list, order sets. Returns null if nothing is open. |
| `list_files` | Preset files under the configured roots, with `rootId` and `presetId`. |
| `open` | Open a file as the active document. |
| `create_default_st_preset` | Create a default SillyTavern preset and open it. |
| `scaffold_preset` | Create a preset and fill it in one atomic call (prompts, placement, settings). |
| `import_json` / `bind` | Load JSON text as an unsaved document; bind it to a file name. |
| `save` / `reload` / `close` | Write (with backup), re-read from disk, or close. |
| `undo` / `redo` / `revert` | Memory-only history. |
### Reading
| Tool | Purpose |
| :--- | :--- |
| `list_prompts` / `search_prompts` | Paginated prompt list with position, depth, order, and references. |
| `get_prompt` / `get_prompts` | One or several prompts with full content and references (by index or identifier). |
| `list_orders` / `get_order` | Order sets and the entries of one set. |
| `get_prompt_impact` | Which order entries reference a prompt. |
| `validate` / `diagnose` / `doctor` | Structure check, diagnostics, and automatic fixes. |
| `preview` | Offline synthetic render with token estimates. |
| `diff` / `compare_presets` | Compare with the saved state, the base state, or another preset. |
| `export_json` | The preset as formatted JSON text. |
| `read` | Generic read-only query (`method` plus `args`). |
### Editing
| Tool | Purpose |
| :--- | :--- |
| `create_prompt` | Create a prompt; the identifier is generated from the name when omitted. |
| `update_prompt` | Change one field by index or identifier. |
| `rename_identifier` | Change a user prompt identifier and update order references. |
| `duplicate_prompt` | Copy a prompt, optionally into an order set. |
| `delete_prompt` | Delete a user prompt; requires `referencePolicy` and `confirm`. |
| `reset_prompt` | Restore a built-in prompt (main, nsfw, jailbreak, enhanceDefinitions). |
| `add_prompt_to_order` / `move_prompt` | Reference a prompt from an order set; insert or move a reference. |
| `detach_order_entry` / `set_order_entry_enabled` | Remove or toggle an order entry. |
| `reorder_order_entry` / `replace_order` / `reset_order` | Rearrange or replace an order set. |
| `update_settings` | Generation settings (see below). |
| `apply_fix` | Apply a fix object from `diagnose`. |
| `apply_batch` | Up to 100 operations, atomic, with `dryRun`. |
### Settings allowed in `update_settings`, `apply_batch`, and `scaffold_preset`
- Numbers: `openai_max_context`, `openai_max_tokens`, `temperature`, `top_p`, `top_k`, `top_a`, `min_p`, `frequency_penalty`, `presence_penalty`, `repetition_penalty`, `seed`, `n`, `names_behavior`
- Booleans: `stream_openai`, `max_context_unlocked`, `wrap_in_quotes`, `show_thoughts`, `squash_system_messages`, `continue_prefill`, `use_sysprompt`, `function_calling`
- Strings: `new_chat_prompt`, `new_group_chat_prompt`, `new_example_chat_prompt`, `continue_nudge_prompt`, `impersonation_prompt`, `group_nudge_prompt`, `wi_format`, `scenario_format`, `personality_format`, `send_if_empty`, `assistant_prefill`, `assistant_impersonation`, `reasoning_effort`
Unknown names and wrong types are rejected with the list of allowed names.
## Examples
Build a preset in one call (no document open yet):
```json
{
"expectedRevision": 0,
"expectedDocumentId": "none",
"rootId": "root_0",
"name": "MyRoleplay",
"prompts": [
{ "identifier": "main", "content": "You are the narrator of a story with {{user}}." },
{ "identifier": "style", "name": "Style", "content": "Write vivid, concise prose.",
"placement": { "kind": "before", "identifier": "chatHistory" } },
{ "identifier": "reminder", "name": "Reminder", "content": "Never act for {{user}}.",
"placement": { "kind": "after", "identifier": "chatHistory" } }
],
"settings": { "temperature": 0.9, "openai_max_tokens": 800 }
}
```
`scaffold_preset` rehearses the request on a throwaway document first, so a bad request leaves your current session untouched. The new document is unsaved until you call `save` with `confirm: true`.
Several edits atomically, using an identifier created earlier in the same batch:
```json
{
"expectedRevision": 3,
"expectedDocumentId": "<documentId>",
"dryRun": true,
"operations": [
{ "command": "createPrompt", "args": {
"prompt": { "name": "Tone", "content": "Warm and playful." },
"addToOrder": { "orderIndex": 0, "position": { "kind": "before", "identifier": "chatHistory" } } } },
{ "command": "addPromptToOrder", "args": {
"orderIndex": 1, "identifier": "tone", "position": { "kind": "before", "identifier": "chatHistory" } } }
]
}
```
## Resources and prompts
Resources:
- `preset://guide`: usage guide.
- `preset://schema/operations`: every `apply_batch` command with arguments and an example.
- `preset://active`, `preset://active/prompts`, `preset://active/orders`: the open document.
- `preset://roots`: configured roots and the preset files under them.
Prompts: `create_roleplay_preset` (guided build with `scaffold_preset`, content rating, language), `audit_and_doctor_preset`, `tune_hyperparameters`.
## Environment variables
| Variable | Used by | Meaning |
| :--- | :--- | :--- |
| `PORT` | web server, stdio bridge | HTTP port, default 4173. |
| `NUTHO_MCP_TOKEN` | web server | Enables `/mcp` when set; bearer token of at least 32 characters. |
| `NUTHO_PRESET_ROOTS` | stdio | Preset directories, separated by `;`. Arguments take precedence. |
## Testing
```bash
npm test
```
The suite covers the core model, the headless document, the session service, the HTTP API, the MCP protocol (tools, resources, prompts, stdio), the schema quality of every tool, and browser behavior in headless Chromium.
## License
MIT. See [LICENSE](LICENSE).