ai-chatbot/packages/mcp/src/tools/scene-query.ts
Sudhir Yadav bf25af6add
viewer: Fix Dutch roof trim artifacts (#452)
* Add roof surface placement support for items

Items (e.g. solar panels) can now be placed on sloped roof surfaces.
The placement system computes euler rotation from the roof surface
normal so items sit flush on the slope instead of going inside.

- Add roofStrategy to placement-strategies with enter/move/click/leave
- Wire roof:enter/move/click/leave events in the placement coordinator
- Add calculateRoofRotation in placement-math using surface normals
- Support full 3D cursor rotation for sloped surfaces
- Items on roofs are parented to the level with world-space rotation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fixed conflict

* Fix spiral stair openings and fence handle arrows

* Implement roof trim planes and ridge vent clipping

* Fix mansard roof and ridge vent placement

* Fix mansard merged roof cutouts

* Fix Dutch roof gable overhang

* Refactor roof segment, ridge vent, and surface geometry

Remove Dutch ridge axis abstraction and rework roof edit system,
ridge vent clipping geometry, and roof surface placement.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Simplify Dutch roof shape

* Add Dutch roof gable top geometry controls

* Fix Dutch roof slope material slots

* Render dutch roof tops as double-sided faces

* Add auto ridge vent toggle to roof segments

Track ridge vent auto-generation via an `autoRidgeVent` metadata flag so
geometry changes only regenerate default vents when enabled, treating
legacy segments with generated vents as auto-enabled for back-compat.
Expose a panel toggle to opt in/out per segment.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Snap new walls to the floor below

Feed the walls of the level directly beneath the active one into the
draft snap pipeline as extra references, so a new wall can align with
the floor below. They share the same local XZ origin, and the list is
kept separate from the current-level walls so the measurement HUD and
wall splitting only act on the active level.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Set Dutch roof shape defaults on type switch

Seed the Dutch shape parameters (waist width/height/length, top rake
thickness/length) with sensible defaults whenever a segment is created
as or switched to Dutch, so the gablet is well-formed regardless of
leftover values from the previous roof type.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Use green accent for corner and endpoint snap markers

Color the corner/endpoint snap markers and the vertical cursor pillar
green across the 2D floorplan beacon, the 3D alignment guide dots, and
the wall snap beacon so snap targets read as a consistent accent.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Add magnetic wall snapping to the roof tool

Snap roof draft corners onto wall corners, midpoints, crossings, and
bodies on the active level and the floor below, reusing the wall tool's
snap pipeline so the beacon and coloring match. The cursor's ground
dot/ring is hidden while a wall snap is active to avoid overlapping the
beacon glyph.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Update auto-generated Next.js route types path

Regenerated next-env.d.ts now references ./.next/dev/types/routes.d.ts.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Show cutaway outline while dragging roof trim

Slice an untrimmed segment volume generated from the live node instead of
the registry mesh, whose CSG rebuild lags a few frames behind the drag and
may still hold placeholder geometry — so the section outline now renders
deterministically. Use LineBasicNodeMaterial so the outline draws under the
WebGPU pipeline, and export generateRoofSegmentGeometry for the slice source.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Fill and clip roof trim cutaway, gate it to active drag

Add a violet silhouette fill behind the cutaway outline, extend the
section slicing to angled diagonal/corner trims via a generic vertical
cut plane, and clip each slice to its footprint span so the infinite
plane no longer sprouts stray lines across the rest of the roof. The
cutaway now renders only while a trim handle is being dragged.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Separate and extend Dutch roof end slopes

Pull the Dutch hip end slopes out of the watertight shingle shell into
their own slab wedge so they can be reshaped independently, and extend
each end slope inward up its own hip plane until the top edge meets the
gablet's inner triangle. Refactor roof-segment shape geometry into a
shared roof-segment-shape module.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Render roof trim cutaway as a material-only section cut

Replace the triangle-mesh slicer with a CSG intersection of a thin slab
against the untrimmed roof shell, so the cutaway shows red only on real
material (wall + deck bands) and leaves the hollow attic empty. Add an
analytic surface-edge outline, style both solid red like a SketchUp
section, and make the cutaway persist whenever a segment is trimmed.

Keep the merged roof shell visible during trim editing (re-trimmed live
from each segment's drag override) instead of swapping in the per-segment
meshes, whose abutting end-cap faces showed as stray white planes the
commit never had. Extend each slab past free cut-line ends only — trimmed
ends clamp to the cut line — so the red section stays inside the trim box.

Re-export INTERSECTION from the viewer CSG surface for the editor.

* Outline roof cutaway by fill silhouette, restyle to destructive red

Derive the section-cut outline from the fill geometry's edges
(EdgesGeometry) so it traces the real cut shape — wall/deck band
boundaries and the hollow-attic edge — instead of just the top surface
line. Drop the fill to 85% opacity and recolor both fill and outline to
the app's destructive red, matching the delete/destructive UI.

* Include roof accessories in trim clipping and red cutaway

Roof accessories (chimney, vents, skylight, dormer, gutter, downspout,
solar-panel, cupola) now slice at the trim plane like the roof shell and
appear in the red section-cut while dragging a trim handle:

- Export clipGeometryBySegmentTrim from the viewer as a reusable
  segment-local trim-clip primitive.
- Add a shared useSegmentTrimClippedGeometry hook + TrimClippedMesh
  wrapper (nodes) that slice accessory geometry by the host segment's
  live trim override, so the cut tracks the drag.
- Wire the clip into all 11 accessory renderers, including skylight
  glass panes and dormer window glass/frame/sill.
- Feed every hosted accessory mesh into the editor's red cutaway, welding
  triangle-soup geometry (e.g. ridge vent) so CSG INTERSECTION yields a
  cross-section.
- Register skylight in the scene-graph tree-node map so it shows in the
  outliner when placed on a roof.

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

* Add smooth spline fences with editable curve handles

Fences can now be drawn as one continuous Catmull-Rom/Bezier curve via an
optional `path` (+ per-point `tangents`), selectable in a Straight/Curved
mode toggle. Selected spline fences expose draggable control-point dots
(hexagon) and symmetric tangent handles (circle) joined by a violet line,
editable in both 2D plan and 3D. Side-move arrows are dropped for splines.

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

* Fix dutch roof ridge vent handling

* Fix Dutch ridge vent placement and support

* Fix Dutch roof trim artifacts

* Fix Dutch roof trim preview geometry

* Tag roof trim overlay meshes with EDITOR_LAYER

Child meshes relied on a parent group's layer, which three.js does not
propagate, so the trim section/rail/plane overlays rendered on the scene
layer — getting inked/SSGI-darkened and leaking into thumbnail exports.

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

* Apply Biome cleanup

* fix(core): address Dutch roof review feedback

* chore: apply biome check cleanup

* fix(core): relax Dutch roof surface helper input

* fix

* Fix biome checks and dev verification

* fixes

* Remove unsupported Biome noShadow override

* Improve roof interactions and fence editing

* Fix fence drag and ridge vent default handling

* editor: drop wall-snap debug log, gate curved-fence finish hint on draft start

Remove the leftover TEMP DIAGNOSTIC console.log in the wall tool's onMove
hot path.

Curved fences commit on a closing gesture (double-click / Enter) rather than
per-click, so surface a 'Finish curve' hint in the fence HUD — but only once a
point has been placed and a curve is actually in flight. The draft point count
is published from SplineFenceDraft into a small ephemeral editor store
(useFenceCurveDraft) that the contextual helper reads, mirroring the existing
useSegmentDraftChain pattern.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Wassim SAMAD <wass08@gmail.com>
2026-06-30 16:41:13 -04:00

833 lines
27 KiB
TypeScript

import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import type { AnyNode, AnyNodeId } from '@pascal-app/core/schema'
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,
)
}
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)
return {
id: wall.id,
name: wall.name,
start: wall.start,
end: wall.end,
length: Math.round(length * 100) / 100,
height: wall.height,
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: stair.totalRise ?? 2.5,
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 =
typeof level.metadata === 'object' &&
level.metadata !== null &&
'height' in level.metadata &&
typeof level.metadata.height === 'number'
? level.metadata.height
: 3.2
for (const wall of nodesOnLevel(bridge, level.id as AnyNodeId).filter(
(node): node is AnyNode & { type: 'wall' } => node.type === 'wall',
)) {
const wallHeight = wall.height ?? 2.5
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 = parent.height ?? 2.5
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)
}