Files
editor/packages/mcp/src/tools/create-level.ts
T
Aymeric RabotandClaude Opus 4.8 f66dcd438b feat(mcp): natural-language measurements for tool inputs via @pascal-app/lingo
Measurement/angle parameters on the scene tools now accept a bare number OR a
natural-language string ("6 in", "180cm", "45°", "1.57rad"), canonicalized to
the unit the handler already expects (meters for length, radians/degrees for
angles) with min/max bounds and model-readable errors. Makes LLM tool calls
safer — "6 in" no longer has to be pre-converted to 0.1524, and a bad value is
rejected with a message the model can self-correct from.

A shared `measurement(kind, unit)` zod field wraps lingo's parseQuantity as a
`number | string` union+transform; numbers pass through unchanged (backward
compatible), so no handler changes were needed. Covers create_wall, cut_opening,
place_item, create_level, create_story_shell, create_roof,
create_stair_between_levels, create_room, add_door, add_window, photo_to_scene.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 12:51:08 +02:00

68 lines
2.2 KiB
TypeScript

import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import type { AnyNodeId } from '@pascal-app/core/schema'
import { LevelNode } from '@pascal-app/core/schema'
import { z } from 'zod'
import type { SceneOperations } from '../operations'
import { ErrorCode, throwMcpError } from './errors'
import { publishLiveSceneSnapshot } from './live-sync'
import { measurement } from './measurement'
import { NodeIdSchema } from './schemas'
export const createLevelInput = {
buildingId: NodeIdSchema,
elevation: z.number().optional(),
height: measurement('length', 'm', {
min: 0,
description: 'Level height (stored in metadata).',
}).optional(),
label: z.string().optional(),
}
export const createLevelOutput = {
levelId: z.string(),
}
export function registerCreateLevel(server: McpServer, bridge: SceneOperations): void {
server.registerTool(
'create_level',
{
title: 'Create level',
description:
'Create a new level node attached to the given building. height and label are stored in metadata.',
inputSchema: createLevelInput,
outputSchema: createLevelOutput,
},
async ({ buildingId, elevation, height, label }) => {
const parent = bridge.getNode(buildingId as AnyNodeId)
if (!parent) {
throwMcpError(ErrorCode.InvalidParams, `Building not found: ${buildingId}`)
}
if (parent.type !== 'building') {
throwMcpError(
ErrorCode.InvalidParams,
`Node ${buildingId} is a ${parent.type}, expected building`,
)
}
const metadata: Record<string, unknown> = {}
if (height !== undefined) metadata.height = height
if (label !== undefined) metadata.label = label
const levelNode = LevelNode.parse({
level: elevation ?? 0,
children: [],
...(Object.keys(metadata).length > 0 ? { metadata } : {}),
...(label !== undefined ? { name: label } : {}),
})
const id = bridge.createNode(levelNode, buildingId as AnyNodeId)
await publishLiveSceneSnapshot(bridge, 'create_level')
const payload = { levelId: id as string }
return {
content: [{ type: 'text' as const, text: JSON.stringify(payload) }],
structuredContent: payload,
}
},
)
}