feat(mcp): implement 19 scene query and mutation tools
Scene querying: get_scene, get_node, describe_node, find_nodes, measure. Scene mutation (undoable, atomic): apply_patch, create_level, create_wall, place_item, cut_opening, set_zone, duplicate_level, delete_node. Undo/redo: undo, redo. Export: export_json, export_glb (not_implemented stub). Validation: validate_scene, check_collisions. Each tool has: - Exported Zod input + output schemas - register<Tool>(server, bridge) wiring function - Bun test with happy-path + error-path coverage via InMemoryTransport 59 tool tests, all passing end-to-end through MCP protocol. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
07ed429d58
commit
58ad89e80b
@@ -0,0 +1,71 @@
|
||||
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
||||
import { cloneLevelSubtree } from '@pascal-app/core/clone-scene-graph'
|
||||
import type { AnyNode, AnyNodeId } from '@pascal-app/core/schema'
|
||||
import { z } from 'zod'
|
||||
import type { Patch as BridgePatch, SceneBridge } from '../bridge/scene-bridge'
|
||||
import { ErrorCode, throwMcpError } from './errors'
|
||||
import { NodeIdSchema } from './schemas'
|
||||
|
||||
export const duplicateLevelInput = {
|
||||
levelId: NodeIdSchema,
|
||||
}
|
||||
|
||||
export const duplicateLevelOutput = {
|
||||
newLevelId: z.string(),
|
||||
newNodeIds: z.array(z.string()),
|
||||
}
|
||||
|
||||
export function registerDuplicateLevel(server: McpServer, bridge: SceneBridge): void {
|
||||
server.registerTool(
|
||||
'duplicate_level',
|
||||
{
|
||||
title: 'Duplicate level',
|
||||
description:
|
||||
'Clone a level and all its descendants into a new subtree attached to the same building.',
|
||||
inputSchema: duplicateLevelInput,
|
||||
outputSchema: duplicateLevelOutput,
|
||||
},
|
||||
async ({ levelId }) => {
|
||||
const node = bridge.getNode(levelId as AnyNodeId)
|
||||
if (!node) {
|
||||
throwMcpError(ErrorCode.InvalidParams, `Level not found: ${levelId}`)
|
||||
}
|
||||
if (node.type !== 'level') {
|
||||
throwMcpError(ErrorCode.InvalidParams, `Node ${levelId} is a ${node.type}, expected level`)
|
||||
}
|
||||
|
||||
// cloneLevelSubtree(nodes, levelId) — returns { clonedNodes, newLevelId, idMap }.
|
||||
const { clonedNodes, newLevelId } = cloneLevelSubtree(bridge.getNodes(), levelId as AnyNodeId)
|
||||
|
||||
const buildingId = (node.parentId as AnyNodeId | null) ?? undefined
|
||||
|
||||
// Flatten cloned subtree into create patches. The level node itself
|
||||
// attaches to the original building; descendants attach to their
|
||||
// already-remapped parent (encoded in `parentId`).
|
||||
const patches: BridgePatch[] = clonedNodes.map((n) => {
|
||||
const isRoot = (n.id as AnyNodeId) === newLevelId
|
||||
const parentIdForBridge = isRoot
|
||||
? buildingId
|
||||
: ((n.parentId as AnyNodeId | null) ?? undefined)
|
||||
|
||||
const createOp: BridgePatch = {
|
||||
op: 'create',
|
||||
node: n as AnyNode,
|
||||
...(parentIdForBridge !== undefined ? { parentId: parentIdForBridge } : {}),
|
||||
}
|
||||
return createOp
|
||||
})
|
||||
|
||||
const result = bridge.applyPatch(patches)
|
||||
|
||||
const payload = {
|
||||
newLevelId: newLevelId as string,
|
||||
newNodeIds: result.createdIds as unknown as string[],
|
||||
}
|
||||
return {
|
||||
content: [{ type: 'text' as const, text: JSON.stringify(payload) }],
|
||||
structuredContent: payload,
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user