Inspiration
Every gamer dreams of dynamic worlds where NPCs aren't bound to static, scripted dialogue trees. Modern developers, however, rarely ship production titles powered by real-time LLMs due to a harsh economic reality: server-side inference costs scale linearly with player traffic, which can bankrupt an indie studio overnight.
Furthermore, naive real-time agent architectures suffer from the polling trap in which agents repeatedly ping game state to check for changes, rapidly burning token budgets and hitting rate limits just asking "Has anything happened yet?"
When the WebMCP specification launched, it presented a chance to flip this paradigm completely. By embedding agent tooling directly into the browser client, the player brings their own compute/agent. The developer only hosts static WebGL assets achieving zero marginal inference cost while empowering an autonomous agent to act as the game's dynamic Dungeon Master.
What it does
Crown and Cipher is a 3D open-world detective game set in a low-poly city, running natively in Unity 6 WebGL and driven by a WebMCP agent:
- Procedural Mystery Generation: Each playthrough procedurally selects a new killer, establishes three essential investigation leads, and generates an indirect knowledge network among city residents.
- Living NPC Roleplay: Suspects are not rigid chatbots; the agent temporarily possesses individual characters, adhering strictly to each NPC's bounded knowledge graph.
- Zero-Polling Event Loop: Through an asynchronous, promise-based
waittool, the game halts agent execution until the player speaks or interacts, eliminating token waste during exploration and driving. - Dynamic Win Validation: The agent validates clues, enforces investigative requirements, and triggers the victory state once the player pieces together the full case.
How we built it
The project establishes a bidirectional bridge between Unity's native WebGL runtime and the browser's model context interface:
- Unity Controller (
WebMCPManager.cs): Manages procedural case assignment, orchestrates the NPC knowledge matrix, dispatches dialogue events, and calls into native JavaScript functions via[DllImport("__Internal")]. - Browser Bridge (
WebMCPBridge.jslib): Registers five core tools (get_system_context,speak,wait,get_npc_location, andshow_winning_panel) withnavigator.modelContext/document.modelContext. When the agent triggers an action, the bridge routes data to Unity viawindow.unityInstance.SendMessage. - The Suspended Promise Loop (
waittool): Rather than continuously querying game state, the agent invokeswaitafter each action. The tool holds an open JavaScriptPromisefor up to $120\text{s}$. When the player submits text through Unity's dialogue UI,EnqueuePlayerMessage()fires from C# and resolves that specific Promise immediately with the interaction payload.
Challenges we ran into
- Event Synchronization in WebGL: Coordinating an asynchronous browser LLM loop with Unity's single-threaded WebGL execution required strict queue management. If a player sends a message while the agent is still executing, messages are staged in an internal queue (
window.mcpMessageQueue) to prevent dropped events. - Controlling Hallucinations & Spoilers: An unrestrained LLM will immediately reveal the killer if pressed. We solved this mathematically by generating an indirect knowledge graph in Unity before the mystery starts, passing strict graph constraints into
get_system_contextto force NPCs to claim ignorance when asked about unlinked characters. - Browser Sandbox Limitations: Embedded browser webviews (such as ChatGPT's in-app browser) often restrict Pointer Lock APIs, making first-person mouse look difficult to control. Documenting these constraints and optimizing for standard desktop browsers ensured smooth playability.
Accomplishments that we're proud of
- Pioneering the
waitTool Architecture: Solving the token-waste problem in agentic gaming with an event-driven, suspended Promise pattern that keeps the agent responsive with minimal token usage. - Zero-Cost Generative Gaming: Proving that studios can ship rich, LLM-orchestrated open worlds without maintaining costly GPU clusters.
- Seamless Unity ↔ WebMCP Integration: Creating a clean, reusable
.jslibbridge pattern that other developers can adopt to connect WebGL games to browser models.
What we learned
- Agents Should Wait, Not Poll: Shifting from polling loops to event-driven suspended promises fundamentally reduces latency and token consumption in web-based agent architectures.
- Client-Side MCP is a Game Changer: Decentralizing AI compute to the user's browser runtime unlocks interactive, generative entertainment models that were previously cost-prohibitive.
- Structured Tools Beat Pure Prompting: Grounding the story loop in explicit tools (
get_npc_location,speak,show_winning_panel) provides the guardrails necessary to keep an open-ended narrative coherent.
What's next for Crown and Cipher
- Multimodal Clue Analysis: Allowing agents to visually analyze screenshots of crime scenes and player positioning using vision models.
- Dynamic Audio & Voice Acting: Integrating local neural TTS models via WebGPU to dynamically voice NPC lines in real time rather than displaying text subtitles alone.
- Expanded Sandbox Mechanics: Introducing inventory systems, physical evidence gathering, and forensic mini-games exposed as modular WebMCP tools.
Built With
- c#
- javascript
- unity
- webmcp
Log in or sign up for Devpost to join the conversation.