feat(mcp,editor): Option A+B storage + 10 agent deliverables (Phase 7)

Ships the combined filesystem/Supabase storage adapter + MCP scene
lifecycle tools + Next.js API routes + editor /scene/[id] route, so
an MCP save is directly openable at /scene/<id> without any
injection hack. End-to-end verified: 10/10 e2e steps pass.

Storage (A1/A2/A3):
- SceneStore interface + error classes + slug helpers
- FilesystemSceneStore at $PASCAL_DATA_DIR (defaults XDG/~/.pascal)
  with atomic writes, .index sidecar, optimistic locking
- SupabaseSceneStore with scenes + scene_revisions tables, RLS
  migration SQL, mock-backed unit tests
- createSceneStore(env) auto-selects based on SUPABASE_URL +
  SUPABASE_SERVICE_ROLE_KEY

MCP tools (A4, A8, A9, A10):
- save_scene / load_scene / list_scenes / delete_scene / rename_scene
- list_templates / create_from_template (3 seed templates:
  empty-studio, two-bedroom, garden-house)
- generate_variants (7 mutation kinds, seeded RNG, save=true|false)
- photo_to_scene (vision sampling → scene graph → save)

Editor (A5, A6):
- /api/scenes + /api/scenes/[id] with RFC 7232 If-Match locking
- /scene/[id] and /scenes route pages with save button, SceneLoader
- Removed the window.__pascalScene dev injection hack

Security + UX edges (A7, A8):
- AssetUrl Zod validator: asset:// blob: data:image/ /path https:
  (http://localhost for dev) + PASCAL_ALLOWED_ASSET_ORIGINS env
  allowlist. Hardens scan.url, guide.url, item.asset.src,
  material.texture.url, MaterialMaps.*Map
- Auto-frame camera on empty→non-empty scene transition
  (camera-controls:fit-scene emitter event)

Shared utilities:
- rehydrateSiteChildren() extracted to packages/mcp/src/lib/ and
  used by both create-from-template and generate-variants to work
  around the SiteNode.children-as-objects vs. ids inconsistency
  (CROSS_CUTTING §2)
- Storage + MCP subpath exports added to packages/mcp/package.json
  (CROSS_CUTTING §4)

Tests: 293 pass / 0 fail across 40 files (was 142 pre-Phase-7).
Biome: clean.

Phase-7 e2e script at packages/mcp/test-reports/phase7-e2e.ts:
MCP HTTP + editor Next.js both point at $PASCAL_DATA_DIR =
/tmp/pascal-e2e, save_scene from MCP, GET /api/scenes/<id> from
editor server, /scenes list page renders all saved scenes, scene
page renders SceneLoader, delete_scene works.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Adrian Perez
2026-04-18 19:29:28 +02:00
co-authored by Claude Opus 4.7
parent 42bd05db9c
commit e8d0b13ff5
81 changed files with 8933 additions and 1213 deletions
@@ -0,0 +1,169 @@
import { beforeEach, describe, expect, test } from 'bun:test'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { SceneBridge } from '../../bridge/scene-bridge'
import {
InMemorySceneStore,
parseToolText,
type StoredTextContent,
} from '../scene-lifecycle/test-utils'
import { registerCreateFromTemplate } from './create-from-template'
import { registerListTemplates } from './list-templates'
describe('list_templates', () => {
let client: Client
beforeEach(async () => {
const server = new McpServer({ name: 'test', version: '0.0.0' })
registerListTemplates(server)
const [srvT, cliT] = InMemoryTransport.createLinkedPair()
client = new Client({ name: 'test-client', version: '0.0.0' })
await Promise.all([server.connect(srvT), client.connect(cliT)])
})
test('enumerates all three seed templates', async () => {
const result = await client.callTool({ name: 'list_templates', arguments: {} })
expect(result.isError).toBeFalsy()
const parsed = parseToolText(result.content as StoredTextContent[])
const list = parsed.templates as Array<{
id: string
name: string
description: string
nodeCount: number
}>
const ids = list.map((t) => t.id).sort()
expect(ids).toEqual(['empty-studio', 'garden-house', 'two-bedroom'])
for (const t of list) {
expect(typeof t.name).toBe('string')
expect(t.name.length).toBeGreaterThan(0)
expect(typeof t.description).toBe('string')
expect(t.nodeCount).toBeGreaterThan(0)
}
})
test('returns structuredContent matching the text payload', async () => {
const result = await client.callTool({ name: 'list_templates', arguments: {} })
expect(result.structuredContent).toBeDefined()
const structured = result.structuredContent as { templates: Array<{ id: string }> }
expect(structured.templates.length).toBe(3)
})
})
describe('create_from_template', () => {
let client: Client
let bridge: SceneBridge
let store: InMemorySceneStore
beforeEach(async () => {
bridge = new SceneBridge()
bridge.setScene({}, [])
store = new InMemorySceneStore()
const server = new McpServer({ name: 'test', version: '0.0.0' })
registerCreateFromTemplate(server, bridge, store)
const [srvT, cliT] = InMemoryTransport.createLinkedPair()
client = new Client({ name: 'test-client', version: '0.0.0' })
await Promise.all([server.connect(srvT), client.connect(cliT)])
})
test('applies a template to the bridge with fresh ids', async () => {
const result = await client.callTool({
name: 'create_from_template',
arguments: { id: 'empty-studio' },
})
expect(result.isError).toBeFalsy()
const parsed = parseToolText(result.content as StoredTextContent[])
expect(parsed.templateId).toBe('empty-studio')
expect((parsed.rootNodeIds as string[]).length).toBeGreaterThan(0)
expect(parsed.nodeCount as number).toBeGreaterThan(0)
// Fresh ids — placeholder "site_empty" should not appear.
const bridgeNodes = Object.keys(bridge.getNodes())
expect(bridgeNodes).not.toContain('site_empty')
expect(bridgeNodes.length).toBeGreaterThan(0)
// Root id from the tool response should exist in the bridge.
for (const rid of parsed.rootNodeIds as string[]) {
expect(bridge.getNode(rid as any)).not.toBeNull()
}
})
test('rejects unknown template ids', async () => {
const result = await client.callTool({
name: 'create_from_template',
arguments: { id: 'not-a-template' },
})
expect(result.isError).toBe(true)
})
test('saves to the store when save: true', async () => {
const result = await client.callTool({
name: 'create_from_template',
arguments: { id: 'two-bedroom', save: true, name: 'My flat' },
})
expect(result.isError).toBeFalsy()
const parsed = parseToolText(result.content as StoredTextContent[])
expect(parsed.scene).toBeDefined()
const scene = parsed.scene as { id: string; name: string; url: string; nodeCount: number }
expect(scene.name).toBe('My flat')
expect(scene.url).toBe(`/scene/${scene.id}`)
expect(scene.nodeCount).toBeGreaterThan(0)
// Confirm the store actually holds it.
const loaded = await store.load(scene.id)
expect(loaded).not.toBeNull()
})
test('two invocations produce disjoint id sets', async () => {
const a = await client.callTool({
name: 'create_from_template',
arguments: { id: 'empty-studio' },
})
const idsA = (parseToolText(a.content as StoredTextContent[]).rootNodeIds as string[]).sort()
const b = await client.callTool({
name: 'create_from_template',
arguments: { id: 'empty-studio' },
})
const idsB = (parseToolText(b.content as StoredTextContent[]).rootNodeIds as string[]).sort()
for (const id of idsA) {
expect(idsB).not.toContain(id)
}
})
})
describe('create_from_template without a store', () => {
let client: Client
let bridge: SceneBridge
beforeEach(async () => {
bridge = new SceneBridge()
bridge.setScene({}, [])
const server = new McpServer({ name: 'test', version: '0.0.0' })
// No store passed → save should be gracefully skipped.
registerCreateFromTemplate(server, bridge)
const [srvT, cliT] = InMemoryTransport.createLinkedPair()
client = new Client({ name: 'test-client', version: '0.0.0' })
await Promise.all([server.connect(srvT), client.connect(cliT)])
})
test('applies a template without erroring when no store is wired', async () => {
const result = await client.callTool({
name: 'create_from_template',
arguments: { id: 'garden-house' },
})
expect(result.isError).toBeFalsy()
const parsed = parseToolText(result.content as StoredTextContent[])
expect(parsed.templateId).toBe('garden-house')
})
test('save:true is a no-op but still succeeds without a store', async () => {
const result = await client.callTool({
name: 'create_from_template',
arguments: { id: 'garden-house', save: true },
})
// Does not error; no `scene` field is returned because there is no store.
expect(result.isError).toBeFalsy()
const parsed = parseToolText(result.content as StoredTextContent[])
expect(parsed.scene).toBeUndefined()
})
})