Files
editor/packages/mcp/src/tools/scene-query.ts
T
bf89b5bcf2 feat: vertical building model — stored level heights, wall inversion, decks (#526)
* feat(core): stored storey heights groundwork — pure slab-support module + level height schema

Extract pointInPolygon/computeWallSlabSupport and friends into a cycle-free
packages/core/src/systems/slab/slab-support.ts (severs level-height ->
spatial-grid-manager -> use-scene), add deriveLegacyLevelHeight as the pure
mesh-free equivalent of the viewer's stacked level height, and add the
optional LevelNode.height field plus the storey service (getStoredLevelHeight,
getLevelElevations per-building prefix sums). No behavior change.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(editor): storey height badge + edit popover on level rows

Each floating-level-selector row shows its storey height; clicking opens a
popover with 2.5/3.0/3.5 presets and a free slider writing level.height.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(core): vertical-model load migration — stored heights, ordinal compaction, wall-top classification

Pass 3 in migrateNodes: derive and store each legacy level's exact stacked
height (never snapped), compact ordinals per building anchored at zero so
basements stay basements, classify wall tops against the derived plane
(|plane - top| < 0.20 strictly -> plane-bound, else explicit height
materialized), and drop the blind totalRise 2.5 stair default on legacy
scenes only. Epsilon and strictness validated by a prod census.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: pin wall tops to the storey plane; stored heights become the only vertical truth

Wall-top inversion: a wall without an explicit height now tops out at its
storey plane (resolveWallTop); slabs lift only the base. Window/door caps
resolve the real top through the same slab election instead of Infinity.
All level stacking (viewer, elevator, first-person, stair openings, MCP
scene queries) reads stored LevelNode.height; the four divergent live
derivations and level.metadata.height are deleted. Stair totalRise becomes
optional and derives from the storey height when absent. MCP create-level
stops writing its elevation param into the ordinal. Level creation sites
write explicit heights; templates carry their true derived heights.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: clamp slabs/ceilings under the storey plane; sweep wall-height fallbacks; wall Top control

Slab elevation writes clamp to plane − MIN_WALL_HEIGHT when plane-bound
walls elect the slab (pure clampSlabElevationForWalls + registry handle
bounds + shared panels for 2D/3D parity); ceiling heights clamp under the
plane and auto-ceilings derive from resolved wall tops. Every remaining
wall.height ?? 2.5 fallback resolves through resolveWallTop /
resolveWallEffectiveHeight (panels, overlays, measurements, quantities,
spatial grid, MCP reports); template walls matching their storey become
plane-bound. Wall panel gains a Top control (Follows storey / Custom
height) derived purely from height presence; the store update path now
deletes keys passed as explicit undefined so plane-binding round-trips.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(core): persisted support hosts — schema, host-preferring election, rendered-polygon unification

Floor-placed nodes and walls gain a nullable supportSlabId. Elections
surface the winning slab (getSlabSupportForItem, candidates query) and
prefer a still-valid persisted host, falling back silently when the host
is gone or reshaped away; deleting the host strips references in the same
undo commit. Item-side support now tests the rendered slab polygon (like
walls) through a per-level cache invalidated by the spatial-grid sync.
Also adds the resolveStairTotalRise unit tests from the stage-1 gates.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: persist support hosts at commit; thread wall host preference everywhere

Floor-placed commits (draft pipeline, per-kind creation tools, registry
move tool) and wall create/move/endpoint commits persist supportSlabId
via shared resolveSupportSlabPatch helpers — only when overlapping
supports disagree on elevation, clearing it otherwise or when the node
leaves the floor. WallSlabSupport surfaces electedSlabId; every wall
support read site passes wall.supportSlabId as the preferred host.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: split slab into placement + thickness; pools become explicit recess intent

slab.elevation stays the walking surface; new thickness grows downward so
the solid occupies [elevation − thickness, elevation]. Migration writes
thickness := elevation for solids (byte-identical intervals, including
degenerate zero) and recessed: true for legacy negative pools. Geometry
branches on recessed instead of the elevation sign; presets keep today's
intervals; free elevation edits move the body without coupling thickness
(the deck semantic). Dead viewer SlabSystem component deleted.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: clamp ceilings to covering-slab undersides across levels

getLevelAbove + getCoveringSlabUndersideAt give the first cross-level
query; ceiling writes clamp to min(storey plane, lowest covering
underside) − 0.01, and the space-detection reconcile now clamps manual
ceilings down (never up) when a deck above intrudes — a flush deck
reactively lowers the ceiling below it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: mezzanine and balcony build-tab tools

One-gesture composites over the kernel: draw a deck footprint and commit
deck slab + railings + stair (mezzanine) or deck + railings (balcony) in
a single undo step. Fences gain supportSlabId and lift onto their host
deck; railing runs split around the stair mouth; edges near wall
centerlines are treated as closed. Stairs target the deck via explicit
totalRise with no level-to-level opening sync.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: adaptive slab vertical editing; level vocabulary in UI copy

Dragging a grounded slab's top stretches it (elevation and thickness move
together — gaps impossible); floating decks move with thickness preserved
and land grounded at zero; pools keep the drag-through-zero gesture.
User-facing copy says level, not storey (Follows level, Level height).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: clamp plane-bound wall tops to covering-slab undersides; fix vertical reactivity

getWallPlaneTop samples the wall span against the level above's slabs, so
a thick or flush upper floor shortens the walls below it instead of
colliding (automatic attach, no dialog). Level-height edits now dirty the
level's walls, stairs, ceilings, and fences; covering-slab changes dirty
the level below. The effective-height helper triplicated across editor
overlays moved to core.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: ceilings follow the level top by default

ceiling.height becomes optional — absent means the ceiling tracks
min(level top, covering slab underside) − 0.01 live, so level-height
edits no longer require ceiling fixes. Ceiling panel gains the same
Follows level / Custom height control as walls; auto-from-walls ceilings
are created height-less and their height-derivation machinery is deleted;
migration drops stored heights within 0.20 of the bound on legacy scenes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: wall plane clamp missed max-side boundary walls

Auto slabs derive their polygon from wall centerlines, so covering-clamp
samples sat exactly on the boundary where ray-cast point-in-polygon is
side-dependent (min edges in, max edges out) — walls clamped or not by
orientation. getWallPlaneTop now clips the wall's thickness band against
the covering polygon (boundary-inclusive, arc-aware) and the ceiling
bound's point sampling gained an explicit on-boundary test. Verified
against the reported repro scene.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: stairs attach to decks; pointer-decided placement surface

Stairs gain deckSlabId — rise follows the deck's elevation live (straight
flights re-converge via a write-sync mirroring auto-openings), the panel
shows a unified To destination with Follows deck / Custom rise, and the
mezzanine tool attaches instead of baking a stale rise. Item placement
under an elevated deck no longer flickers: grid events fed a feedback
loop (the grid plane rode the ghost's elected height), so the support
election is now capped at the surface the pointer ray actually hits,
with a ground sentinel keeping under-deck commits deterministic.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: remove the mezzanine/balcony composite tools

Decks ship as catalog presets instead; the kernel the presets rely on
(fence deck-hosting, stair deck attachment, clone remaps, pointer-decided
placement) all stays. The tool code lives at e30042db for reference.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: gate wall adoption to grounded slabs; panel moves, drag stretches then unsticks

Floating decks keep their drawn polygon (and stop being seam candidates
for grounded neighbors) instead of growing into nearby walls. Panel
elevation edits are pure placement; the viewport drag stretches a
grounded slab up to 0.4 m then unsticks it into a thin deck.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: deck-attached stairs land flush with the deck surface

The rise now subtracts the stair's own elected base (same election the
visual lift uses), so base + rise always equals the deck walking surface;
the auto-sync defers a microtask so it reads a settled spatial grid and
re-converges on both deck and base-slab moves.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: draw walls and fences on elevated decks

Wall and fence draw tools now publish the pointed surface, so the draw
plane rides the deck top (no more perspective-skewed floor hits) and
previews sit on the deck. Fences gain real support election: a pure
resolveFenceSupportSlabPatch persists the deck host at draw and reshape
commits; wall commits thread the pointer cap so aiming under a deck
elects the floor.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: unit-aware height presets

Level, ceiling, and slab preset buttons show clean values per display
system (8/9/10 ft storeys, 8'-9' ceilings, whole-inch slab steps) instead
of converted metric labels; metric presets unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: stairs converge to their resolved rise; deck attachment disables the cutout

syncStairRises now converges every follows-mode straight stair (level or
deck) plus deck-attached custom rises — detaching a stair from a deck
re-derives its height, and ordinary stairs finally track level-height
changes. Attaching via the panel writes slabOpeningMode none and hides
the cutout controls; detaching restores the destination cutout and
clears the stale explicit rise.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: stacked-slab move hopping — one ray, one surface, one XZ

The hop was hysteresis: the election consumed the riding grid plane's
perspective-skewed hit, giving two self-consistent fixed points for one
pointer ray. getPointedSupportSurface now returns the ray's crossing of
the pointed surface and both the support cap and the cursor XZ derive
from that single computation, so items stay on the surface the pointer
aims at and sit exactly under the cursor across storeys.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: single stair per click; tools restore select mode on exit

The stair tool subscribed to both node clicks (synthesized on pointerup)
and the native-click grid event with none of the guards sibling tools
carry — one physical click over any node surface committed twice. A
commit gate + follow-up click swallow fix the double dispatch, and the
stair and column tools now restore select mode on exit instead of
leaving the dead build-mode-without-tool state that ignored every click.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: compact multi-selection panel with host footer slot

Selecting multiple nodes now docks the collapsed-by-default panel on the
right: N selected header, kind breakdown, and Duplicate/Delete mirroring
the floating pill. A new multiSelectionFooter slot lets the host app dock
actions below it, exactly like inspectorFooter.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-21 09:57:30 -04:00

859 lines
28 KiB
TypeScript

import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import {
DEFAULT_LEVEL_HEIGHT,
getStoredLevelHeight,
getWallPlaneTop,
resolveStairTotalRise,
resolveWallEffectiveHeight,
} from '@pascal-app/core'
import type { AnyNode, AnyNodeId } from '@pascal-app/core/schema'
import { computeWallSlabSupport } from '@pascal-app/core/spatial-grid'
import { z } from 'zod'
import type { SceneOperations } from '../operations'
import {
distance2D,
pointInPolygon,
polygonArea,
polygonContainsPolygon,
type Vec2,
wallLength,
} from './geometry'
import { NodeIdSchema } from './schemas'
export const levelScopedInput = {
levelId: NodeIdSchema.optional(),
}
const jsonObject = z.record(z.string(), z.unknown())
export const listLevelsOutput = {
activeSceneId: z.string().nullable(),
levelCount: z.number(),
occupiedStoryCount: z.number(),
supportLevelCount: z.number(),
roofLevelIds: z.array(z.string()),
levels: z.array(jsonObject),
}
export const getLevelSummaryOutput = {
levelId: z.string(),
levelName: z.string().optional(),
role: z.string(),
metadataRole: z.string().nullable(),
isOccupiedStory: z.boolean(),
isSupportLevel: z.boolean(),
referenceLevelId: z.string().nullable(),
counts: jsonObject,
walls: z.array(jsonObject),
zones: z.array(jsonObject),
items: z.array(jsonObject),
slabs: z.array(jsonObject),
ceilings: z.array(jsonObject),
}
export const getWallsOutput = {
levelId: z.string(),
walls: z.array(jsonObject),
}
export const getZonesOutput = {
levelId: z.string(),
zones: z.array(jsonObject),
}
export const verifySceneOutput = {
ok: z.boolean(),
valid: z.boolean(),
levelCount: z.number(),
occupiedStoryCount: z.number(),
supportLevelCount: z.number(),
roofLevelIds: z.array(z.string()),
activeSceneId: z.string().nullable(),
levels: z.array(jsonObject),
emptyLevelIds: z.array(z.string()),
issues: z.array(z.string()),
hasIssues: z.boolean(),
}
type ContentCounts = {
walls: number
zones: number
doors: number
windows: number
items: number
slabs: number
ceilings: number
roofs: number
stairs: number
}
type LevelRole = 'occupied' | 'roof' | 'support'
function textResult<T extends Record<string, unknown>>(payload: T) {
return {
content: [{ type: 'text' as const, text: JSON.stringify(payload) }],
structuredContent: payload,
}
}
function getLevels(bridge: SceneOperations): AnyNode[] {
return bridge.findNodes({ type: 'level' }).sort((a, b) => {
const aa = a.type === 'level' ? a.level : 0
const bb = b.type === 'level' ? b.level : 0
return aa - bb
})
}
function getDefaultLevelId(
bridge: SceneOperations,
requested?: string | undefined,
): AnyNodeId | null {
if (requested) return requested as AnyNodeId
const level = getLevels(bridge)[0]
return (level?.id as AnyNodeId | undefined) ?? null
}
function nodesOnLevel(bridge: SceneOperations, levelId: AnyNodeId): AnyNode[] {
return Object.values(bridge.getNodes()).filter(
(node) => node.id !== levelId && bridge.resolveLevelId(node.id as AnyNodeId) === levelId,
)
}
export function resolveReportedWallHeight(
bridge: SceneOperations,
wall: Extract<AnyNode, { type: 'wall' }>,
): number {
const levelId = bridge.resolveLevelId(wall.id as AnyNodeId)
// Covering-clamped plane for plane-bound walls; explicit heights pass
// through resolveWallEffectiveHeight untouched.
const planeTop = levelId
? getWallPlaneTop(wall, levelId, bridge.getNodes())
: DEFAULT_LEVEL_HEIGHT
const levelNodes = levelId ? nodesOnLevel(bridge, levelId) : []
const slabs = levelNodes.filter(
(node): node is Extract<AnyNode, { type: 'slab' }> => node.type === 'slab',
)
const walls = levelNodes.filter(
(node): node is Extract<AnyNode, { type: 'wall' }> => node.type === 'wall',
)
const support = computeWallSlabSupport(wall, slabs, walls, wall.supportSlabId)
return resolveWallEffectiveHeight(wall, planeTop, support.elevation)
}
function metadataRecord(node: AnyNode): Record<string, unknown> | null {
return typeof node.metadata === 'object' && node.metadata !== null
? (node.metadata as Record<string, unknown>)
: null
}
function metadataString(node: AnyNode, key: string): string | undefined {
const value = metadataRecord(node)?.[key]
return typeof value === 'string' ? value : undefined
}
function occupiedContentCount(counts: ContentCounts): number {
return (
counts.walls +
counts.zones +
counts.doors +
counts.windows +
counts.items +
counts.slabs +
counts.ceilings +
counts.stairs
)
}
function classifyLevel(level: AnyNode, counts: ContentCounts): LevelRole {
const metadataRole = metadataString(level, 'role')
if (metadataRole === 'roof') return 'roof'
if (metadataRole === 'support') return 'support'
if (counts.roofs > 0 && occupiedContentCount(counts) === 0) return 'roof'
return 'occupied'
}
function openingSummaries(bridge: SceneOperations, wallId: AnyNodeId) {
return bridge
.getChildren(wallId)
.filter((child) => child.type === 'door' || child.type === 'window')
.map((child) => ({
id: child.id,
type: child.type,
position: child.position,
width: child.width,
height: child.height,
}))
}
function wallSummary(bridge: SceneOperations, wall: AnyNode) {
if (wall.type !== 'wall') return null
const length = distance2D(wall.start, wall.end)
const resolvedHeight = resolveReportedWallHeight(bridge, wall)
return {
id: wall.id,
name: wall.name,
start: wall.start,
end: wall.end,
length: Math.round(length * 100) / 100,
height: wall.height,
resolvedHeight,
heightIsExplicit: wall.height !== undefined,
thickness: wall.thickness,
openings: openingSummaries(bridge, wall.id as AnyNodeId),
}
}
function zoneSummary(zone: AnyNode) {
if (zone.type !== 'zone') return null
const xs = zone.polygon.map((p) => p[0])
const zs = zone.polygon.map((p) => p[1])
return {
id: zone.id,
name: zone.name,
color: zone.color,
polygon: zone.polygon,
areaSqMeters: Math.round(polygonArea(zone.polygon) * 100) / 100,
bounds: {
width: Math.round((Math.max(...xs) - Math.min(...xs)) * 100) / 100,
depth: Math.round((Math.max(...zs) - Math.min(...zs)) * 100) / 100,
},
}
}
function itemSummary(item: AnyNode) {
if (item.type !== 'item') return null
return {
id: item.id,
name: item.name ?? item.asset.name,
parentId: item.parentId,
position: item.position,
rotation: item.rotation,
asset: {
id: item.asset.id,
name: item.asset.name,
category: item.asset.category,
dimensions: item.asset.dimensions,
attachTo: item.asset.attachTo ?? null,
},
}
}
type SegmentTransform = {
position: [number, number, number]
rotation: number
}
type StairSegmentLike = {
width: number
length: number
height: number
stepCount: number
attachmentSide: 'front' | 'left' | 'right'
}
function rotateXZ(x: number, z: number, angle: number): Vec2 {
const cos = Math.cos(angle)
const sin = Math.sin(angle)
return [x * cos + z * sin, -x * sin + z * cos]
}
function toWorldPlanPoint(
stair: AnyNode & { type: 'stair' },
localX: number,
localZ: number,
): Vec2 {
const [worldX, worldZ] = rotateXZ(localX, localZ, stair.rotation ?? 0)
return [stair.position[0] + worldX, stair.position[2] + worldZ]
}
function computeSegmentTransforms(segments: StairSegmentLike[]): SegmentTransform[] {
const transforms: SegmentTransform[] = []
let currentX = 0
let currentY = 0
let currentZ = 0
let currentRot = 0
for (let index = 0; index < segments.length; index++) {
const segment = segments[index]
if (!segment) continue
if (index === 0) {
transforms.push({ position: [currentX, currentY, currentZ], rotation: currentRot })
continue
}
const previous = segments[index - 1]
if (!previous) continue
let attachX = 0
let attachZ = 0
let rotationDelta = 0
switch (segment.attachmentSide) {
case 'front':
attachZ = previous.length
break
case 'left':
attachX = previous.width / 2
attachZ = previous.length / 2
rotationDelta = Math.PI / 2
break
case 'right':
attachX = -previous.width / 2
attachZ = previous.length / 2
rotationDelta = -Math.PI / 2
break
}
const [deltaX, deltaZ] = rotateXZ(attachX, attachZ, currentRot)
currentX += deltaX
currentY += previous.height
currentZ += deltaZ
currentRot += rotationDelta
transforms.push({ position: [currentX, currentY, currentZ], rotation: currentRot })
}
return transforms
}
function stairFootprintPolygons(
bridge: SceneOperations,
stair: AnyNode & { type: 'stair' },
): Vec2[][] {
if (stair.stairType === 'curved' || stair.stairType === 'spiral') {
const radius = Math.max(0.05, stair.innerRadius ?? 0.9) + Math.max(stair.width ?? 1, 0.4)
return [
Array.from({ length: 24 }).map((_, index) => {
const angle = (index / 24) * Math.PI * 2
return toWorldPlanPoint(stair, Math.cos(angle) * radius, Math.sin(angle) * radius)
}),
]
}
const nodes = bridge.getNodes()
const segments = (stair.children ?? [])
.map((childId) => nodes[childId as AnyNodeId])
.filter((node): node is AnyNode & { type: 'stair-segment' } => node?.type === 'stair-segment')
const usableSegments: StairSegmentLike[] =
segments.length > 0
? segments
: [
{
width: stair.width ?? 1,
length: 3,
height: resolveStairTotalRise(stair, nodes),
stepCount: stair.stepCount ?? 10,
attachmentSide: 'front' as const,
},
]
const transforms = computeSegmentTransforms(usableSegments)
return usableSegments.map((segment, index) => {
const transform = transforms[index] ?? {
position: [0, 0, 0] as [number, number, number],
rotation: 0,
}
const halfWidth = segment.width / 2
const corners: Vec2[] = [
[-halfWidth, 0],
[halfWidth, 0],
[halfWidth, segment.length],
[-halfWidth, segment.length],
]
return corners.map(([localX, localZ]) => {
const [rx, rz] = rotateXZ(localX, localZ, transform.rotation)
return toWorldPlanPoint(stair, transform.position[0] + rx, transform.position[2] + rz)
})
})
}
function wallSamplePoints(wall: AnyNode & { type: 'wall' }): Vec2[] {
return [0.25, 0.5, 0.75].map((t) => [
wall.start[0] + (wall.end[0] - wall.start[0]) * t,
wall.start[1] + (wall.end[1] - wall.start[1]) * t,
])
}
function getLevelNumber(
levelId: string | null | undefined,
nodes: Record<string, AnyNode>,
): number | undefined {
if (!levelId) return undefined
const node = nodes[levelId as AnyNodeId]
return node?.type === 'level' ? node.level : undefined
}
function targetLevelIdsForStair(
bridge: SceneOperations,
stair: AnyNode & { type: 'stair' },
): AnyNodeId[] {
const nodes = bridge.getNodes()
const parentLevelId = bridge.resolveLevelId(stair.id as AnyNodeId)
const fromLevelId = (stair.fromLevelId ?? parentLevelId) as string | null
const toLevelId = (stair.toLevelId ?? fromLevelId) as string | null
const fromLevel = getLevelNumber(fromLevelId, nodes)
const toLevel = getLevelNumber(toLevelId, nodes)
if (fromLevel === undefined || toLevel === undefined) {
return toLevelId ? [toLevelId as AnyNodeId] : []
}
const minLevel = Math.min(fromLevel, toLevel)
const maxLevel = Math.max(fromLevel, toLevel)
return getLevels(bridge)
.filter((level) => level.type === 'level' && level.level > minLevel && level.level <= maxLevel)
.map((level) => level.id as AnyNodeId)
}
function holeBelongsToStair(
surface: AnyNode & { type: 'slab' | 'ceiling' },
holeIndex: number,
stairId: string,
) {
const metadata = surface.holeMetadata?.[holeIndex]
return metadata?.source === 'stair' && metadata.stairId === stairId
}
function parentListsChild(parent: AnyNode, childId: string): boolean {
if (!('children' in parent && Array.isArray(parent.children))) return false
return parent.children.some((child) => {
if (typeof child === 'string') return child === childId
return (
child !== null &&
typeof child === 'object' &&
'id' in child &&
(child as { id?: unknown }).id === childId
)
})
}
function levelSummary(bridge: SceneOperations, levelId: AnyNodeId) {
const level = bridge.getNode(levelId)
if (level?.type !== 'level') {
throw new Error(`Level not found: ${levelId}`)
}
const nodes = nodesOnLevel(bridge, levelId)
const walls = nodes
.map((n) => wallSummary(bridge, n))
.filter((n): n is NonNullable<typeof n> => !!n)
const zones = nodes.map(zoneSummary).filter((n): n is NonNullable<typeof n> => !!n)
const items = nodes.map(itemSummary).filter((n): n is NonNullable<typeof n> => !!n)
const slabs = nodes
.filter((node) => node.type === 'slab')
.map((node) => ({
id: node.id,
polygon: node.polygon,
holes: node.holes ?? [],
holeMetadata: node.holeMetadata ?? [],
elevation: node.elevation,
}))
const ceilings = nodes
.filter((node) => node.type === 'ceiling')
.map((node) => ({
id: node.id,
polygon: node.polygon,
holes: node.holes ?? [],
holeMetadata: node.holeMetadata ?? [],
height: node.height,
}))
const doors = nodes.filter((node) => node.type === 'door')
const windows = nodes.filter((node) => node.type === 'window')
const roofs = nodes.filter((node) => node.type === 'roof')
const stairs = nodes.filter((node) => node.type === 'stair')
const counts: ContentCounts = {
walls: walls.length,
zones: zones.length,
doors: doors.length,
windows: windows.length,
items: items.length,
slabs: slabs.length,
ceilings: ceilings.length,
roofs: roofs.length,
stairs: stairs.length,
}
const role = classifyLevel(level, counts)
const metadataRole = metadataString(level, 'role') ?? null
const referenceLevelId = metadataString(level, 'referenceLevelId') ?? null
return {
levelId,
levelName: level.name,
floorIndex: level.level,
role,
metadataRole,
isOccupiedStory: role === 'occupied',
isSupportLevel: role !== 'occupied',
referenceLevelId,
counts,
walls,
zones,
items,
slabs,
ceilings,
}
}
export function registerListLevels(server: McpServer, bridge: SceneOperations): void {
server.registerTool(
'list_levels',
{
title: 'List levels',
description:
'List all levels in the current scene with ids, names, floor indices, and child counts.',
inputSchema: {},
outputSchema: listLevelsOutput,
},
async () => {
const activeScene = bridge.getActiveScene()
const levels = getLevels(bridge).map((level) => {
const summary = levelSummary(bridge, level.id as AnyNodeId)
return {
id: level.id,
name: level.name,
floorIndex: level.type === 'level' ? level.level : 0,
parentId: level.parentId,
role: summary.role,
metadataRole: summary.metadataRole,
isOccupiedStory: summary.isOccupiedStory,
isSupportLevel: summary.isSupportLevel,
referenceLevelId: summary.referenceLevelId,
childCount: bridge.getChildren(level.id as AnyNodeId).length,
}
})
const occupiedStoryCount = levels.filter((level) => level.isOccupiedStory).length
const roofLevelIds = levels
.filter((level) => level.role === 'roof')
.map((level) => level.id as string)
return textResult({
activeSceneId: activeScene?.id ?? null,
levelCount: levels.length,
occupiedStoryCount,
supportLevelCount: levels.length - occupiedStoryCount,
roofLevelIds,
levels,
})
},
)
}
export function registerGetLevelSummary(server: McpServer, bridge: SceneOperations): void {
server.registerTool(
'get_level_summary',
{
title: 'Get level summary',
description:
'Get a compact model-friendly summary of one level: counts plus walls, zones, slabs, ceilings, and items. Omit levelId to use the first level.',
inputSchema: levelScopedInput,
outputSchema: getLevelSummaryOutput,
},
async ({ levelId }) => {
const resolved = getDefaultLevelId(bridge, levelId)
if (!resolved) throw new Error('No level exists in the scene')
return textResult(levelSummary(bridge, resolved))
},
)
}
export function registerGetWalls(server: McpServer, bridge: SceneOperations): void {
server.registerTool(
'get_walls',
{
title: 'Get walls',
description:
'Get walls on a level with start/end coordinates, length, height, thickness, and child doors/windows. Omit levelId to use the first level.',
inputSchema: levelScopedInput,
outputSchema: getWallsOutput,
},
async ({ levelId }) => {
const resolved = getDefaultLevelId(bridge, levelId)
if (!resolved) throw new Error('No level exists in the scene')
return textResult({
levelId: resolved,
walls: levelSummary(bridge, resolved).walls,
})
},
)
}
export function registerGetZones(server: McpServer, bridge: SceneOperations): void {
server.registerTool(
'get_zones',
{
title: 'Get zones',
description:
'Get room/zone polygons on a level with names, colors, bounds, and approximate areas. Omit levelId to use the first level.',
inputSchema: levelScopedInput,
outputSchema: getZonesOutput,
},
async ({ levelId }) => {
const resolved = getDefaultLevelId(bridge, levelId)
if (!resolved) throw new Error('No level exists in the scene')
return textResult({
levelId: resolved,
zones: levelSummary(bridge, resolved).zones,
})
},
)
}
export function registerVerifyScene(server: McpServer, bridge: SceneOperations): void {
server.registerTool(
'verify_scene',
{
title: 'Verify scene',
description:
'High-level self-check after complex edits. Returns validation status, per-level room/content counts, empty levels, and practical layout issues.',
inputSchema: {},
outputSchema: verifySceneOutput,
},
async () => {
const validation = bridge.validateScene()
const levels = getLevels(bridge).map((level) => {
const summary = levelSummary(bridge, level.id as AnyNodeId)
const totalContent = Object.values(summary.counts).reduce(
(sum, count) => sum + (typeof count === 'number' ? count : 0),
0,
)
return {
levelId: level.id,
levelName: level.name ?? `Level ${summary.floorIndex}`,
floorIndex: summary.floorIndex,
role: summary.role,
metadataRole: summary.metadataRole,
isOccupiedStory: summary.isOccupiedStory,
isSupportLevel: summary.isSupportLevel,
referenceLevelId: summary.referenceLevelId,
isEmpty: totalContent === 0,
content: summary.counts,
}
})
const issues: string[] = []
const occupiedStoryCount = levels.filter((level) => level.isOccupiedStory).length
const roofLevelIds = levels
.filter((level) => level.role === 'roof')
.map((level) => level.levelId as string)
const emptyLevelIds = levels.filter((level) => level.isEmpty).map((level) => level.levelId)
if (emptyLevelIds.length > 0) {
issues.push(`Empty level(s): ${emptyLevelIds.join(', ')}`)
}
for (const level of levels) {
const occupiedContent = occupiedContentCount(level.content)
if (level.role === 'roof') {
if (level.content.roofs === 0) {
issues.push(
`${level.levelName} is a roof support level but has no roof geometry; add or move roof geometry there rather than deleting the support level to satisfy story count`,
)
}
if (occupiedContent > 0) {
issues.push(
`${level.levelName} is a roof support level but contains occupied-story content; move rooms, walls, stairs, slabs, ceilings, and items to an occupied story and keep the roof level for roof geometry only`,
)
}
if (level.referenceLevelId) {
const referenceLevel = bridge.getNode(level.referenceLevelId as AnyNodeId)
if (referenceLevel?.type === 'level' && level.floorIndex <= referenceLevel.level) {
issues.push(
`${level.levelName} roof support level should be above its reference occupied level ${referenceLevel.name ?? referenceLevel.id}`,
)
}
}
continue
}
if (level.content.walls > 0 && level.content.zones === 0) {
issues.push(`${level.levelName} has walls but no zones/rooms`)
}
if (level.content.zones > 0 && level.content.slabs === 0) {
issues.push(`${level.levelName} has zones but no slabs/floors`)
}
if (level.content.zones > 0 && level.content.ceilings === 0) {
issues.push(`${level.levelName} has zones but no ceilings`)
}
if (level.content.walls > 0 && level.content.doors === 0) {
issues.push(`${level.levelName} has walls but no doors`)
}
if (
level.content.roofs > 0 &&
(level.content.walls > 0 || level.content.zones > 0 || level.content.stairs > 0)
) {
issues.push(
`${level.levelName} mixes roof geometry with occupied-level content; place roofs on a dedicated roof level for solo/exploded level views`,
)
}
}
const hasMultipleOccupiedStories = occupiedStoryCount > 1
if (hasMultipleOccupiedStories) {
for (const level of getLevels(bridge)) {
if (level.type !== 'level') continue
const summary = levels.find((entry) => entry.levelId === level.id)
if (!summary?.isOccupiedStory) continue
const expectedHeight = getStoredLevelHeight(level)
for (const wall of nodesOnLevel(bridge, level.id as AnyNodeId).filter(
(node): node is AnyNode & { type: 'wall' } => node.type === 'wall',
)) {
const wallHeight = resolveReportedWallHeight(bridge, wall)
if (wallHeight > expectedHeight + 0.25) {
issues.push(
`Wall ${wall.name ?? wall.id} on ${level.name ?? level.id} is ${wallHeight}m high; multi-story exterior walls should be split into level-owned story walls`,
)
}
}
}
}
for (const node of Object.values(bridge.getNodes())) {
if (node.type === 'door' || node.type === 'window') {
const parent = node.parentId ? bridge.getNode(node.parentId as AnyNodeId) : null
if (parent?.type !== 'wall') {
issues.push(`${node.type} ${node.id} is not parented to a wall`)
continue
}
if (node.wallId !== parent.id) {
issues.push(
`${node.type} ${node.id} has wallId ${node.wallId ?? 'unset'} but is parented to wall ${parent.id}`,
)
}
if (!parentListsChild(parent, node.id)) {
issues.push(`${node.type} ${node.id} is not listed in wall ${parent.id} children`)
}
const length = wallLength(parent)
const width = node.width ?? (node.type === 'door' ? 0.9 : 1.5)
const height = node.height ?? (node.type === 'door' ? 2.1 : 1.5)
const localX = node.position[0]
if (localX - width / 2 < -0.01 || localX + width / 2 > length + 0.01) {
issues.push(`${node.type} ${node.id} extends outside wall ${parent.id}`)
}
const wallHeight = resolveReportedWallHeight(bridge, parent)
const bottom = node.position[1] - height / 2
const top = node.position[1] + height / 2
if (bottom < -0.01 || top > wallHeight + 0.01) {
issues.push(
`${node.type} ${node.id} vertical bounds [${bottom.toFixed(2)}, ${top.toFixed(2)}] exceed wall ${parent.id} height ${wallHeight.toFixed(2)}m`,
)
}
}
}
for (const stair of Object.values(bridge.getNodes()).filter(
(node): node is AnyNode & { type: 'stair' } => node.type === 'stair',
)) {
const sourceLevelId = bridge.resolveLevelId(stair.id as AnyNodeId)
if (sourceLevelId) {
const sourceLevel = bridge.getNode(sourceLevelId)
const footprints = stairFootprintPolygons(bridge, stair)
const sourceSlabs = nodesOnLevel(bridge, sourceLevelId).filter(
(node): node is AnyNode & { type: 'slab' } => node.type === 'slab',
)
if (sourceSlabs.length > 0) {
const outsideFootprints = footprints.filter(
(footprint) =>
!sourceSlabs.some((slab) =>
polygonContainsPolygon(slab.polygon as Vec2[], footprint),
),
)
if (outsideFootprints.length > 0) {
issues.push(
`Stair ${stair.name ?? stair.id} footprint extends outside source floor slab on ${
sourceLevel?.name ?? sourceLevelId
}`,
)
}
}
const obstructingWalls = nodesOnLevel(bridge, sourceLevelId)
.filter((node): node is AnyNode & { type: 'wall' } => node.type === 'wall')
.filter((wall) =>
footprints.some((footprint) =>
wallSamplePoints(wall).some((point) => pointInPolygon(point, footprint, false)),
),
)
for (const wall of obstructingWalls) {
issues.push(
`Wall ${wall.name ?? wall.id} obstructs stair ${stair.name ?? stair.id} on ${
sourceLevel?.name ?? sourceLevelId
}`,
)
}
}
if ((stair.slabOpeningMode ?? 'none') === 'destination') {
const targetLevelIds = targetLevelIdsForStair(bridge, stair)
if (targetLevelIds.length === 0) {
issues.push(
`Stair ${stair.name ?? stair.id} requests a slab opening but has no target level`,
)
}
for (const targetLevelId of targetLevelIds) {
const targetLevel = bridge.getNode(targetLevelId)
const targetSlabs = nodesOnLevel(bridge, targetLevelId).filter(
(node): node is AnyNode & { type: 'slab' } => node.type === 'slab',
)
if (targetSlabs.length === 0) {
issues.push(
`Stair ${stair.name ?? stair.id} targets ${targetLevel?.name ?? targetLevelId} but it has no slab`,
)
continue
}
const matchingHoles = targetSlabs.flatMap((slab) =>
(slab.holes ?? [])
.map((hole, index) => ({ slab, hole, index }))
.filter((entry) => holeBelongsToStair(entry.slab, entry.index, stair.id)),
)
if (matchingHoles.length === 0) {
issues.push(
`Stair ${stair.name ?? stair.id} has no destination slab opening on ${
targetLevel?.name ?? targetLevelId
}`,
)
continue
}
for (const { slab, hole } of matchingHoles) {
if (!polygonContainsPolygon(slab.polygon as Vec2[], hole as Vec2[])) {
issues.push(
`Stair ${stair.name ?? stair.id} opening extends outside slab ${slab.name ?? slab.id}`,
)
}
}
}
}
}
if (!validation.valid) {
for (const error of validation.errors.slice(0, 5)) {
issues.push(`Schema: ${error.nodeId}.${error.path} ${error.message}`)
}
if (validation.errors.length > 5) {
issues.push(`Schema: ${validation.errors.length - 5} additional validation errors`)
}
}
const payload = {
ok: true,
valid: validation.valid,
levelCount: levels.length,
occupiedStoryCount,
supportLevelCount: levels.length - occupiedStoryCount,
roofLevelIds,
activeSceneId: bridge.getActiveScene()?.id ?? null,
levels,
emptyLevelIds,
issues,
hasIssues: issues.length > 0,
}
return textResult(payload)
},
)
}
export function registerSceneQueryTools(server: McpServer, bridge: SceneOperations): void {
registerListLevels(server, bridge)
registerGetLevelSummary(server, bridge)
registerGetWalls(server, bridge)
registerGetZones(server, bridge)
registerVerifyScene(server, bridge)
}