narrative-runtime
Goal-oriented narrative state machine for AI agents, exposing live world/scene context as MCP tools and resources.
README
narrative-runtime
Goal-oriented narrative state machine for AI agents.
Model agent work as Stories → Scenes → Beats with soft/hard constraints, parallel lanes, failure routing, checkpoints, and a template narrator. The runtime enforces invariants and exposes live world/scene context as MCP tools + resources.
Interactive fiction engine × agent orchestration.
Features
- Core domain: Story, Scene, Beat, Constraint, WorldState, Transition, Outcome, InvariantChecker
- Engine: YAML/JSON load, graph validation, step transitions, dead-end detection, checkpoint/restore
- Soft vs hard constraints — warn or block / route to failure scenes
- Parallel scene lanes with join gates (
fork: truetransitions) - Template narrator (no API key)
- Mermaid story-graph + timeline export
- MCP server (stdio) with tools, resources, and a continue prompt
- CLI + optional Vite studio for graph visualization
- Sample stories:
incident-response-agent,research-agent
Requirements
- Node.js 20+
Install
git clone https://github.com/SanjoyDat1/narrative-runtime.git
cd narrative-runtime
npm install
npm run build
npm test
CLI
# Summarize a story
npx tsx src/cli/index.ts load stories/research-agent/story.yaml
# Print Mermaid graph
npx tsx src/cli/index.ts mermaid stories/incident-response-agent/story.yaml
# Advance one beat
npx tsx src/cli/index.ts step stories/research-agent/story.yaml define-topic \
--patch '{"research":{"topic":"MCP resources"}}'
# Start MCP server (stdio)
npm run mcp
# or after build:
node dist/mcp/server.js
Cursor MCP config
Add to your Cursor MCP settings (e.g. ~/.cursor/mcp.json):
{
"mcpServers": {
"narrative-runtime": {
"command": "node",
"args": ["/ABS/PATH/to/narrative-runtime/dist/mcp/server.js"],
"env": {}
}
}
}
Or during development:
{
"mcpServers": {
"narrative-runtime": {
"command": "npx",
"args": ["tsx", "/ABS/PATH/to/narrative-runtime/src/mcp/server.ts"]
}
}
}
MCP tools
| Tool | Description |
|---|---|
nr_load_story |
Load YAML/JSON story (path sandboxed) |
nr_get_state |
Runtime snapshot |
nr_list_beats |
Beats in active lanes |
nr_advance |
Complete a beat (+ optional world patch) |
nr_check_invariants |
Soft/hard constraint check |
nr_checkpoint |
Save checkpoint JSON |
nr_restore |
Restore checkpoint |
nr_export_timeline |
Timeline JSON / Mermaid / story graph |
MCP resources
| URI | Description |
|---|---|
nr://story/current |
Loaded story metadata |
nr://scene/current |
Active scene(s) + beats |
nr://world/state |
World state JSON |
nr://timeline/latest |
Narrator + recent events (markdown) |
MCP prompt
nr_continue— guides the agent to pick the next beat
Agent workflow
nr_load_story→stories/research-agent/story.yaml- Read
nr://scene/currentandnr://world/state nr_list_beats→ choose a beatnr_advancewith required patch fieldsnr_check_invariants/ re-read resourcesnr_checkpointbefore risky branches
Studio
npm run build # also used to sync sample JSON if you run export
cd studio && npm install && npm run dev
Open http://localhost:5173 — pick a sample story and view the Mermaid graph + initial world state.
Generate studio JSON samples from the repo root:
npx tsx scripts/sync-studio-samples.ts
Story authoring (YAML sketch)
id: my-story
title: My Story
startSceneId: start
initialWorldState: { flag: false }
scenes:
- id: start
title: Start
lane: main
failureSceneId: fail
beats:
- id: do-thing
title: Do the thing
allowPatch: [flag]
apply: { flag: true }
constraints:
- id: flag-on
severity: hard
path: flag
op: eq
value: true
onViolation: block
transitions:
- id: win
afterBeats: [do-thing]
to: success
- id: fail
kind: failure
title: Fail
beats: []
transitions: []
outcomes:
- id: success
title: Success
kind: success
Constraint operators: eq, neq, gt, gte, lt, lte, exists, in, contains.
Severity: soft | hard. Hard onViolation: block | route_failure.
Architecture
See docs/ARCHITECTURE.md and docs/PLAN.md.
License
MIT
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。