roleplay-mcp.
Persistent campaign storage for long-running AI roleplay.
An MCP server on Cloudflare Workers backed by D1. It keeps world rules, campaign state and append-only history apart, and separates objective truth from what each character knows.
- JavaScript
- Cloudflare Workers
- D1
- MCP
- OAuth 2.1
The problem
A long campaign accumulates world rules, session state, history and character knowledge.
A document-style save file blurs those boundaries, and continuity depends on one writer at a time.
The engine targets campaigns where continuity, player agency and hidden information matter more than a save file.
Concurrent writers must not silently overwrite each other's newer canon.
Data model
The schema is deliberately campaign-agnostic.
Scopes are reusable information boundaries. A global scope holds a world or a shared ruleset; a campaign owns session and OOC proposal scopes.
campaign_scopes links those scopes into one campaign context.
Entities, aliases and relations hold named things and their links.
state_values holds mutable state, and resources holds long-form Markdown.
Game-specific ideas such as mana, credits or spell slots become entity kinds, state namespaces or commodities, not schema columns.
campaigns,scopesandcampaign_scopesdefine the reusable and owned boundaries.entities,entity_aliasesandrelationshold names, aliases and links.state_valuesandresourceshold mutable state and Markdown references.claims,knowledge,eventsandquantity_ledgerhold truth, belief, history and exact balances.changesetsandchange_recordshold revisions and their audit rows.
Truth and knowledge
A claim is objective canon, a hypothesis, a rumor, a proposal or a rule.
A claim points to one entity or one JSON value, and the schema rejects both at once.
knowledge records one character's stance on one claim.
The stance can be knows, suspects, believes, doubts or rejects.
Knowledge is separate from truth, so a character can believe a false claim.
Visibility defaults to GM, and campaign context keeps the label on each item.
Safe writes
Every normal mutation is one changeset, and each changeset advances the campaign revision by one.
A writer supplies expected_revision, so a stale write fails instead of overwriting newer canon.
UNIQUE(campaign_id, base_revision) makes two simultaneous commits on one revision conflict.
The guarded insert selects the current revision, and a lost race leaves no changeset row.
Events are append-only, and their order comes from the changeset revision plus ordinal.
change_records keeps an immutable audit row for every operation.
quantity_ledger stores integer deltas in each commodity's smallest unit.
commit_changesetaccepts set_state, upsert_entity, set_relation, put_resource, append_event, ledger_entry, record_claim, set_knowledge and set_active_session.- A write rejects a scope that is not linked to the campaign.
Architecture
Production runs as one OAuth-protected Worker. It serves authorization, consent, the upstream callback and the MCP endpoint.
One D1 database holds campaign data, and one OAuth KV namespace holds OAuth state.
Cloudflare Access is the upstream OIDC provider, with an exact email allowlist.
MCP access needs mcp:read; create_campaign and commit_changeset also need mcp:write.
src/worker.js holds the semantic engine. src/auth-worker.js wraps it for production, and the older resource Worker remains a migration reference.
The MCP endpoint accepts JSON-RPC over HTTP POST and returns JSON responses.
- D1 is authoritative, and no second writable store runs beside it.
- OAuth 2.1 uses the Workers OAuth Provider and a credential-free MCP URL.
Tool catalog
The server exposes eight tools at this revision. Read tools return campaign data; create_campaign and commit_changeset write.
list_campaigns: lists campaigns stored in the engine.get_campaign_context: returns linked scopes, current state, key characters, balances and recent events for the active session.search_entities: searches campaign-linked entities by name or slug, with an optional kind filter.get_resource: reads one long-form Markdown resource by scope, kind and slug.get_recent_events: reads recent immutable events for the active session.get_balance: returns an exact ledger balance for one owner and commodity across linked scopes.create_campaign: creates an empty campaign, a session scope and a runtime pointer.commit_changeset: commits a revision-guarded batch of mutations and audits each operation.
Use with your agent
The server exposes MCP tools over streamable-http from a Worker. Access needs OAuth 2.1 and a scope.
The client obtains an access token through the browser, and the MCP URL itself carries no credentials.
- A client connects to the Worker's MCP endpoint without a token and receives an OAuth 2.1 challenge.
- The browser signs in through the upstream identity provider, and consent grants
mcp:readormcp:write. - The client sends authenticated MCP requests, lists the tool catalog and reads a campaign before it writes.
Source: private repository at 5dcf8ed