fix: Windows spawn, prefab path, preview API, asset deps, error messages; add execute-context resource, tool descriptions, scene/script/asset tools

Bug fixes (verified against Cocos Creator 3.8.8):
- diagnostics: shell:true for .cmd/.bat on Windows (T125/T126)
- prefabs: duplicatePrefab target resolves under assets/ (T420)
- cocos-project: add preview.open candidate for 3.8.8 (T444)
- assets-advanced: detect directory assets in inspectAssetDependencies (T110)
- scene: improve component-not-found errors with compilation hint (T429)

Documentation improvements:
- tool-registry: add enum and injected vars to execute_javascript, path format examples, asset ref limitation note
- resources: add cocos://mcp/execute-context resource with variables, patterns, pitfalls

New tools (core 37->38, full 101->110):
- scene-management: create_scene, query_scene_state (core), copy_paste_node, rename_node, reparent_node
- scripts: create_script with component/plain templates
- prefabs: create_prefab
- assets-advanced: batch_asset_ops, find_unused_assets
This commit is contained in:
mingyuansi
2026-06-30 21:53:06 +08:00
parent a9b539ca9c
commit b65a4f22c3
21 changed files with 5273 additions and 53 deletions
+97
View File
@@ -70,6 +70,7 @@ class ResourceProvider {
createResource('cocos://logs/editor', `${projectName} Editor Logs`, 'Recent MCP runtime logs and tool interaction history.'),
createResource('cocos://logs/project', `${projectName} Project Logs`, 'Recent tails from common project log files.'),
createResource('cocos://mcp/interactions', `${projectName} MCP Interactions`, 'Recent MCP tool interaction summaries.'),
createResource('cocos://mcp/execute-context', `${projectName} Execute Context Guide`, 'Available variables and code patterns for execute_javascript scene and editor contexts.'),
];
}
@@ -131,6 +132,8 @@ class ResourceProvider {
return this.getProjectLogsText(projectPath);
case 'cocos://mcp/interactions':
return this.interactionLog.summary();
case 'cocos://mcp/execute-context':
return this.getExecuteContextGuide();
default:
break;
}
@@ -286,6 +289,100 @@ class ResourceProvider {
].join('\n'))
.join('\n\n---\n\n');
}
getExecuteContextGuide() {
return [
'Execute JavaScript Context Guide',
'',
'This document describes the variables and code patterns available in',
'execute_javascript for both scene and editor contexts.',
'',
'=== Scene Context (context="scene") ===',
'',
'Injected Variables:',
' cc - Cocos engine module. Use cc.Sprite, cc.Node, cc.Vec3, etc.',
' scene - Active scene root node (cc.Node). Same as director.getScene().',
' director - cc.director.',
' require - Node.js require (with Cocos module paths).',
' Editor - Editor global API (if running in editor).',
' args - User-passed args object.',
'',
'Return Patterns:',
' 1. Direct return: return { myResult: 42 };',
' 2. run function: async function run(env) { return result; }',
' 3. module.exports: module.exports = async (env) => result;',
'',
'Key Rules:',
' - Use cc.Sprite, cc.Node, cc.UITransform — NOT bare Sprite/Node.',
' - Do NOT redeclare scene, director, cc — they are already in scope.',
' - Find nodes by traversing scene.children recursively (see pattern below).',
' - Load assets with callback-style assetManager.loadAny (see pattern below).',
'',
'Pattern: Find a node by UUID',
' let target = null;',
' function search(n) {',
' if (n.uuid === "NODE_UUID") { target = n; return; }',
' for (const c of n.children) { search(c); if (target) return; }',
' }',
' search(scene);',
'',
'Pattern: Load and set an asset reference (SpriteFrame, Texture, etc.)',
' const { Sprite, assetManager, SpriteFrame } = cc;',
' const sf = await new Promise((resolve, reject) => {',
' assetManager.loadAny(',
' { uuid: "ASSET_UUID", type: SpriteFrame },',
' (err, asset) => { if (err) reject(err); else resolve(asset); }',
' );',
' });',
' sprite.spriteFrame = sf;',
'',
'Pattern: Create a node with components',
' const { Node, UITransform, Sprite } = cc;',
' const node = new Node("MyNode");',
' node.layer = parent.layer;',
' node.addComponent(UITransform);',
' node.addComponent(Sprite);',
' parent.addChild(node);',
'',
'=== Editor Context (context="editor") ===',
'',
'Injected Variables:',
' require - Node.js require.',
' Editor - Editor global API.',
' args - User-passed args object.',
' context - Runtime context (projectPath, projectName, cocosVersion, etc.).',
' helpers - { getStatus, listTools, readResource, callTool,',
' listClientTargets, getClientConfig, configureClient }.',
' fs - Node.js fs module.',
' path - Node.js path module.',
' os - Node.js os module.',
'',
'Pattern: Call asset-db from editor context',
' const info = await Editor.Message.request("asset-db", "query-asset-info", "ASSET_UUID");',
'',
'Pattern: Call a scene method from editor context',
' const result = await Editor.Message.request(',
' "scene", "execute-scene-script", { name: "funplay-cocos-mcp", method: "inspectNode", args: { uuid: "NODE_UUID" } }',
' );',
'',
'=== Common Pitfalls ===',
'',
'1. set_component_property cannot set asset references (SpriteFrame, Texture,',
' Material, Font, AudioClip). Use execute_javascript with loadAny instead.',
'',
'2. create_node only accepts parentPath (e.g. "Canvas/Player"), not parentUuid.',
' Use get_hierarchy first to find the full path.',
'',
'3. assetManager.loadAny(uuid) without callback returns null in scene context.',
' Always use callback style: loadAny({uuid, type}, (err, asset) => {}).',
'',
'4. Custom script components (e.g. TestComponent) require the script to be',
' compiled before add_component or findComponent can find them.',
'',
'5. Node hierarchy paths are slash-separated from scene root, e.g.',
' "should_hide_in_hierarchy/TestPrefab/Sprite". Use get_hierarchy to verify.',
].join('\n');
}
}
module.exports = {