Cocos Creator MCP 服务器是一个全面的 Model Context Protocol (MCP) 服务器插件,专为 Cocos Creator 3.8+ 设计,通过标准化协议使 AI 助手能够与 Cocos Creator 编辑器进行交互。
本文档详细介绍了所有可用的 MCP 工具及其使用方法。
MCP 服务器提供了 158 个工具,按功能分为 13 个主要类别:
- 场景操作工具 (Scene Tools)
- 节点操作工具 (Node Tools)
- 组件管理工具 (Component Tools)
- 预制体操作工具 (Prefab Tools)
- 项目控制工具 (Project Tools)
- 调试工具 (Debug Tools)
- 偏好设置工具 (Preferences Tools)
- 服务器工具 (Server Tools)
- 广播工具 (Broadcast Tools)
- 高级资源工具 (Asset Advanced Tools)
- 参考图像工具 (Reference Image Tools)
- 高级场景工具 (Scene Advanced Tools)
- 场景视图工具 (Scene View Tools)
获取当前场景信息
参数: 无
返回: 当前场景的名称、UUID、类型、激活状态和节点数量
示例:
{
"tool": "scene_get_current_scene",
"arguments": {}
}获取项目中所有场景列表
参数: 无
返回: 项目中所有场景的列表,包括名称、路径和UUID
示例:
{
"tool": "scene_get_scene_list",
"arguments": {}
}通过路径打开场景
参数:
scenePath(string, 必需): 场景文件路径
示例:
{
"tool": "scene_open_scene",
"arguments": {
"scenePath": "db://assets/scenes/GameScene.scene"
}
}保存当前场景
参数: 无
示例:
{
"tool": "scene_save_scene",
"arguments": {}
}创建新场景资源
参数:
sceneName(string, 必需): 新场景的名称savePath(string, 必需): 保存场景的路径
示例:
{
"tool": "scene_create_scene",
"arguments": {
"sceneName": "NewLevel",
"savePath": "db://assets/scenes/NewLevel.scene"
}
}将场景另存为新文件
参数:
path(string, 必需): 保存场景的路径
示例:
{
"tool": "scene_save_scene_as",
"arguments": {
"path": "db://assets/scenes/GameScene_Copy.scene"
}
}关闭当前场景
参数: 无
示例:
{
"tool": "scene_close_scene",
"arguments": {}
}获取当前场景的完整层级结构
参数:
includeComponents(boolean, 可选): 是否包含组件信息,默认为 false
示例:
{
"tool": "scene_get_scene_hierarchy",
"arguments": {
"includeComponents": true
}
}在场景中创建新节点
参数:
name(string, 必需): 节点名称parentUuid(string, 强烈建议): 父节点UUID。重要:强烈建议始终提供此参数。使用get_current_scene或get_all_nodes查找父节点UUID。如果不提供,节点将在场景根节点创建。nodeType(string, 可选): 节点类型,可选值:Node、2DNode、3DNode,默认为NodesiblingIndex(number, 可选): 同级索引,-1 表示添加到末尾,默认为 -1
重要提示: 为了确保节点创建在预期位置,请始终提供 parentUuid 参数。您可以通过以下方式获取父节点UUID:
- 使用
scene_get_current_scene获取场景根节点UUID - 使用
node_get_all_nodes查看所有节点及其UUID - 使用
node_find_node_by_name查找特定节点的UUID
示例:
{
"tool": "node_create_node",
"arguments": {
"name": "PlayerNode",
"nodeType": "2DNode",
"parentUuid": "parent-uuid-here"
}
}通过UUID获取节点信息
参数:
uuid(string, 必需): 节点UUID
示例:
{
"tool": "node_get_node_info",
"arguments": {
"uuid": "node-uuid-here"
}
}按名称模式查找节点
参数:
pattern(string, 必需): 搜索的名称模式exactMatch(boolean, 可选): 是否精确匹配,默认为 false
示例:
{
"tool": "node_find_nodes",
"arguments": {
"pattern": "Enemy",
"exactMatch": false
}
}通过精确名称查找第一个节点
参数:
name(string, 必需): 要查找的节点名称
示例:
{
"tool": "node_find_node_by_name",
"arguments": {
"name": "Player"
}
}获取场景中所有节点及其UUID
参数: 无
示例:
{
"tool": "node_get_all_nodes",
"arguments": {}
}设置节点属性值
参数:
uuid(string, 必需): 节点UUIDproperty(string, 必需): 属性名称(如 position、rotation、scale、active)value(any, 必需): 属性值
示例:
{
"tool": "node_set_node_property",
"arguments": {
"uuid": "node-uuid-here",
"property": "position",
"value": {"x": 100, "y": 200, "z": 0}
}
}从场景中删除节点
参数:
uuid(string, 必需): 要删除的节点UUID
示例:
{
"tool": "node_delete_node",
"arguments": {
"uuid": "node-uuid-here"
}
}将节点移动到新的父节点
参数:
nodeUuid(string, 必需): 要移动的节点UUIDnewParentUuid(string, 必需): 新父节点UUIDsiblingIndex(number, 可选): 新父节点中的同级索引,默认为 -1
示例:
{
"tool": "node_move_node",
"arguments": {
"nodeUuid": "node-uuid-here",
"newParentUuid": "parent-uuid-here",
"siblingIndex": 0
}
}复制节点
参数:
uuid(string, 必需): 要复制的节点UUIDincludeChildren(boolean, 可选): 是否包含子节点,默认为 true
示例:
{
"tool": "node_duplicate_node",
"arguments": {
"uuid": "node-uuid-here",
"includeChildren": true
}
}向指定节点添加组件
参数:
nodeUuid(string, 必需): 目标节点UUID。重要:必须指定要添加组件的确切节点。使用get_all_nodes或find_node_by_name获取所需节点的UUID。componentType(string, 必需): 组件类型(如 cc.Sprite、cc.Label、cc.Button)
重要提示: 在添加组件之前,请确保:
- 先使用
node_get_all_nodes或node_find_node_by_name找到目标节点的UUID - 验证节点存在且UUID正确
- 选择合适的组件类型
示例:
{
"tool": "component_add_component",
"arguments": {
"nodeUuid": "node-uuid-here",
"componentType": "cc.Sprite"
}
}从节点移除组件
参数:
nodeUuid(string, 必需): 节点UUIDcomponentType(string, 必需): 要移除的组件类型
示例:
{
"tool": "component_remove_component",
"arguments": {
"nodeUuid": "node-uuid-here",
"componentType": "cc.Sprite"
}
}获取节点的所有组件
参数:
nodeUuid(string, 必需): 节点UUID
示例:
{
"tool": "component_get_components",
"arguments": {
"nodeUuid": "node-uuid-here"
}
}获取特定组件信息
参数:
nodeUuid(string, 必需): 节点UUIDcomponentType(string, 必需): 要获取信息的组件类型
示例:
{
"tool": "component_get_component_info",
"arguments": {
"nodeUuid": "node-uuid-here",
"componentType": "cc.Sprite"
}
}设置组件属性值
参数:
nodeUuid(string, 必需): 节点UUIDcomponentType(string, 必需): 组件类型property(string, 必需): 属性名称value(any, 必需): 属性值
示例:
{
"tool": "component_set_component_property",
"arguments": {
"nodeUuid": "node-uuid-here",
"componentType": "cc.Sprite",
"property": "spriteFrame",
"value": "sprite-frame-uuid"
}
}向节点附加脚本组件
参数:
nodeUuid(string, 必需): 节点UUIDscriptPath(string, 必需): 脚本资源路径
示例:
{
"tool": "component_attach_script",
"arguments": {
"nodeUuid": "node-uuid-here",
"scriptPath": "db://assets/scripts/PlayerController.ts"
}
}获取可用组件类型列表
参数:
category(string, 可选): 组件类别过滤器,可选值:all、renderer、ui、physics、animation、audio,默认为all
示例:
{
"tool": "component_get_available_components",
"arguments": {
"category": "ui"
}
}create-node 配合 assetUuid 进行的实例化过程存在限制,可能导致实例化的预制体中缺少子节点。
获取项目中所有预制体
参数:
folder(string, 可选): 搜索文件夹路径,默认为db://assets
示例:
{
"tool": "prefab_get_prefab_list",
"arguments": {
"folder": "db://assets/prefabs"
}
}通过路径加载预制体
参数:
prefabPath(string, 必需): 预制体资源路径
示例:
{
"tool": "prefab_load_prefab",
"arguments": {
"prefabPath": "db://assets/prefabs/Enemy.prefab"
}
}在场景中实例化预制体
参数:
prefabPath(string, 必需): 预制体资源路径parentUuid(string, 可选): 父节点UUIDposition(object, 可选): 初始位置,包含 x、y、z 属性
示例:
{
"tool": "prefab_instantiate_prefab",
"arguments": {
"prefabPath": "db://assets/prefabs/Enemy.prefab",
"parentUuid": "parent-uuid-here",
"position": {"x": 100, "y": 200, "z": 0}
}
}create-node 方法中使用 assetUuid 的限制,可能只创建根节点,子节点可能会丢失。这是当前实现的已知问题。
从节点创建预制体
参数:
nodeUuid(string, 必需): 源节点UUIDsavePath(string, 必需): 保存预制体的路径prefabName(string, 必需): 预制体名称
示例:
{
"tool": "prefab_create_prefab",
"arguments": {
"nodeUuid": "node-uuid-here",
"savePath": "db://assets/prefabs/",
"prefabName": "MyPrefab"
}
}从节点创建预制体(create_prefab 的别名)
参数:
nodeUuid(string, 必需): 源节点UUIDprefabPath(string, 必需): 保存预制体的路径
示例:
{
"tool": "prefab_create_prefab_from_node",
"arguments": {
"nodeUuid": "node-uuid-here",
"prefabPath": "db://assets/prefabs/MyPrefab.prefab"
}
}更新现有预制体
参数:
prefabPath(string, 必需): 预制体资源路径nodeUuid(string, 必需): 包含更改的节点UUID
示例:
{
"tool": "prefab_update_prefab",
"arguments": {
"prefabPath": "db://assets/prefabs/Enemy.prefab",
"nodeUuid": "node-uuid-here"
}
}将预制体实例恢复为原始状态
参数:
nodeUuid(string, 必需): 预制体实例节点UUID
示例:
{
"tool": "prefab_revert_prefab",
"arguments": {
"nodeUuid": "prefab-instance-uuid-here"
}
}获取详细的预制体信息
参数:
prefabPath(string, 必需): 预制体资源路径
示例:
{
"tool": "prefab_get_prefab_info",
"arguments": {
"prefabPath": "db://assets/prefabs/Enemy.prefab"
}
}在预览模式下运行项目
参数:
platform(string, 可选): 目标平台,可选值:browser、simulator、preview,默认为browser
示例:
{
"tool": "project_run_project",
"arguments": {
"platform": "browser"
}
}构建项目
参数:
platform(string, 必需): 构建平台,可选值:web-mobile、web-desktop、ios、android、windows、macdebug(boolean, 可选): 是否调试构建,默认为 true
示例:
{
"tool": "project_build_project",
"arguments": {
"platform": "web-mobile",
"debug": false
}
}获取项目信息
参数: 无
示例:
{
"tool": "project_get_project_info",
"arguments": {}
}获取项目设置
参数:
category(string, 可选): 设置类别,可选值:general、physics、render、assets,默认为general
示例:
{
"tool": "project_get_project_settings",
"arguments": {
"category": "physics"
}
}刷新资源数据库
参数:
folder(string, 可选): 要刷新的特定文件夹
示例:
{
"tool": "project_refresh_assets",
"arguments": {
"folder": "db://assets/textures"
}
}导入资源文件
参数:
sourcePath(string, 必需): 源文件路径targetFolder(string, 必需): 资源中的目标文件夹
示例:
{
"tool": "project_import_asset",
"arguments": {
"sourcePath": "/path/to/image.png",
"targetFolder": "db://assets/textures"
}
}获取资源信息
参数:
assetPath(string, 必需): 资源路径
示例:
{
"tool": "project_get_asset_info",
"arguments": {
"assetPath": "db://assets/textures/player.png"
}
}按类型获取资源
参数:
type(string, 可选): 资源类型过滤器,可选值:all、scene、prefab、script、texture、material、mesh、audio、animation,默认为allfolder(string, 可选): 搜索文件夹,默认为db://assets
示例:
{
"tool": "project_get_assets",
"arguments": {
"type": "texture",
"folder": "db://assets/textures"
}
}获取构建设置
参数: 无
示例:
{
"tool": "project_get_build_settings",
"arguments": {}
}在编辑器中打开构建面板
参数: 无
示例:
{
"tool": "project_open_build_panel",
"arguments": {}
}检查构建器工作进程是否就绪
参数: 无
示例:
{
"tool": "project_check_builder_status",
"arguments": {}
}启动预览服务器
参数:
port(number, 可选): 预览服务器端口,默认为 7456
示例:
{
"tool": "project_start_preview_server",
"arguments": {
"port": 8080
}
}停止预览服务器
参数: 无
示例:
{
"tool": "project_stop_preview_server",
"arguments": {}
}创建新的资源文件或文件夹
参数:
url(string, 必需): 资源URLcontent(string, 可选): 文件内容,null 表示创建文件夹overwrite(boolean, 可选): 是否覆盖现有文件,默认为 false
示例:
{
"tool": "project_create_asset",
"arguments": {
"url": "db://assets/scripts/NewScript.ts",
"content": "// New TypeScript script\n",
"overwrite": false
}
}复制资源到另一个位置
参数:
source(string, 必需): 源资源URLtarget(string, 必需): 目标位置URLoverwrite(boolean, 可选): 是否覆盖现有文件,默认为 false
示例:
{
"tool": "project_copy_asset",
"arguments": {
"source": "db://assets/textures/player.png",
"target": "db://assets/textures/backup/player.png",
"overwrite": false
}
}移动资源到另一个位置
参数:
source(string, 必需): 源资源URLtarget(string, 必需): 目标位置URLoverwrite(boolean, 可选): 是否覆盖现有文件,默认为 false
示例:
{
"tool": "project_move_asset",
"arguments": {
"source": "db://assets/textures/old_player.png",
"target": "db://assets/textures/player.png",
"overwrite": true
}
}删除资源
参数:
url(string, 必需): 要删除的资源URL
示例:
{
"tool": "project_delete_asset",
"arguments": {
"url": "db://assets/textures/unused.png"
}
}保存资源内容
参数:
url(string, 必需): 资源URLcontent(string, 必需): 资源内容
示例:
{
"tool": "project_save_asset",
"arguments": {
"url": "db://assets/scripts/GameManager.ts",
"content": "// Updated script content\n"
}
}重新导入资源
参数:
url(string, 必需): 要重新导入的资源URL
示例:
{
"tool": "project_reimport_asset",
"arguments": {
"url": "db://assets/textures/player.png"
}
}获取资源磁盘路径
参数:
url(string, 必需): 资源URL
示例:
{
"tool": "project_query_asset_path",
"arguments": {
"url": "db://assets/textures/player.png"
}
}从URL获取资源UUID
参数:
url(string, 必需): 资源URL
示例:
{
"tool": "project_query_asset_uuid",
"arguments": {
"url": "db://assets/textures/player.png"
}
}从UUID获取资源URL
参数:
uuid(string, 必需): 资源UUID
示例:
{
"tool": "project_query_asset_url",
"arguments": {
"uuid": "asset-uuid-here"
}
}获取编辑器控制台日志
参数:
limit(number, 可选): 要检索的最新日志数量,默认为 100filter(string, 可选): 按类型过滤日志,可选值:all、log、warn、error、info,默认为all
示例:
{
"tool": "debug_get_console_logs",
"arguments": {
"limit": 50,
"filter": "error"
}
}清空编辑器控制台
参数: 无
示例:
{
"tool": "debug_clear_console",
"arguments": {}
}在场景上下文中执行JavaScript代码
参数:
script(string, 必需): 要执行的JavaScript代码
示例:
{
"tool": "debug_execute_script",
"arguments": {
"script": "console.log('Hello from MCP!');"
}
}获取用于调试的详细节点树
参数:
rootUuid(string, 可选): 根节点UUID,如果不提供则使用场景根节点maxDepth(number, 可选): 最大树深度,默认为 10
示例:
{
"tool": "debug_get_node_tree",
"arguments": {
"rootUuid": "root-node-uuid",
"maxDepth": 5
}
}获取性能统计信息
参数: 无
示例:
{
"tool": "debug_get_performance_stats",
"arguments": {}
}验证当前场景是否有问题
参数:
checkMissingAssets(boolean, 可选): 检查缺失的资源引用,默认为 truecheckPerformance(boolean, 可选): 检查性能问题,默认为 true
示例:
{
"tool": "debug_validate_scene",
"arguments": {
"checkMissingAssets": true,
"checkPerformance": true
}
}获取编辑器和环境信息
参数: 无
示例:
{
"tool": "debug_get_editor_info",
"arguments": {}
}从 temp/logs/project.log 文件获取项目日志
参数:
lines(number, 可选): 从日志文件末尾读取的行数,默认值为100,范围:1-10000filterKeyword(string, 可选): 按指定关键词过滤日志logLevel(string, 可选): 按日志级别过滤,选项:ERROR,WARN,INFO,DEBUG,TRACE,ALL,默认为ALL
示例:
{
"tool": "debug_get_project_logs",
"arguments": {
"lines": 200,
"filterKeyword": "prefab",
"logLevel": "INFO"
}
}获取项目日志文件信息
参数: 无
返回: 文件大小、最后修改时间、行数和文件路径信息
示例:
{
"tool": "debug_get_log_file_info",
"arguments": {}
}在项目日志中搜索特定模式或错误
参数:
pattern(string, 必需): 搜索模式(支持正则表达式)maxResults(number, 可选): 最大匹配结果数量,默认为20,范围:1-100contextLines(number, 可选): 匹配结果周围显示的上下文行数,默认为2,范围:0-10
示例:
{
"tool": "debug_search_project_logs",
"arguments": {
"pattern": "error|failed|exception",
"maxResults": 10,
"contextLines": 3
}
}获取编辑器偏好设置
参数:
key(string, 可选): 要获取的特定偏好设置键
示例:
{
"tool": "preferences_get_preferences",
"arguments": {
"key": "editor.theme"
}
}设置编辑器偏好设置
参数:
key(string, 必需): 要设置的偏好设置键value(any, 必需): 要设置的偏好设置值
示例:
{
"tool": "preferences_set_preferences",
"arguments": {
"key": "editor.theme",
"value": "dark"
}
}获取全局编辑器偏好设置
参数:
key(string, 可选): 要获取的全局偏好设置键
示例:
{
"tool": "preferences_get_global_preferences",
"arguments": {
"key": "global.autoSave"
}
}设置全局编辑器偏好设置
参数:
key(string, 必需): 要设置的全局偏好设置键value(any, 必需): 要设置的全局偏好设置值
示例:
{
"tool": "preferences_set_global_preferences",
"arguments": {
"key": "global.autoSave",
"value": true
}
}获取最近打开的项目
参数: 无
示例:
{
"tool": "preferences_get_recent_projects",
"arguments": {}
}清除最近打开的项目列表
参数: 无
示例:
{
"tool": "preferences_clear_recent_projects",
"arguments": {}
}获取服务器信息
参数: 无
示例:
{
"tool": "server_get_server_info",
"arguments": {}
}广播自定义消息
参数:
message(string, 必需): 消息名称data(any, 可选): 消息数据
示例:
{
"tool": "server_broadcast_custom_message",
"arguments": {
"message": "custom_event",
"data": {"type": "test", "value": 123}
}
}获取编辑器版本信息
参数: 无
示例:
{
"tool": "server_get_editor_version",
"arguments": {}
}获取当前项目名称
参数: 无
示例:
{
"tool": "server_get_project_name",
"arguments": {}
}获取当前项目路径
参数: 无
示例:
{
"tool": "server_get_project_path",
"arguments": {}
}获取当前项目UUID
参数: 无
示例:
{
"tool": "server_get_project_uuid",
"arguments": {}
}请求重启编辑器
参数: 无
示例:
{
"tool": "server_restart_editor",
"arguments": {}
}请求退出编辑器
参数: 无
示例:
{
"tool": "server_quit_editor",
"arguments": {}
}获取最近的广播消息日志
参数:
limit(number, 可选): 要返回的最新消息数量,默认为 50messageType(string, 可选): 按消息类型过滤
示例:
{
"tool": "broadcast_get_broadcast_log",
"arguments": {
"limit": 100,
"messageType": "scene_change"
}
}开始监听特定广播消息
参数:
messageType(string, 必需): 要监听的消息类型
示例:
{
"tool": "broadcast_listen_broadcast",
"arguments": {
"messageType": "node_created"
}
}停止监听特定广播消息
参数:
messageType(string, 必需): 要停止监听的消息类型
示例:
{
"tool": "broadcast_stop_listening",
"arguments": {
"messageType": "node_created"
}
}清除广播消息日志
参数: 无
示例:
{
"tool": "broadcast_clear_broadcast_log",
"arguments": {}
}获取活动广播监听器列表
参数: 无
示例:
{
"tool": "broadcast_get_active_listeners",
"arguments": {}
}所有工具调用都使用 JSON-RPC 2.0 格式:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "tool_name",
"arguments": {
// 工具参数
}
},
"id": 1
}- 使用
node_get_all_nodes获取所有节点UUID - 使用
node_find_node_by_name按名称查找节点UUID - 使用
scene_get_current_scene获取场景UUID - 使用
prefab_get_prefab_list获取预制体信息
Cocos Creator 使用 db:// 前缀的资源URL格式:
- 场景:
db://assets/scenes/GameScene.scene - 预制体:
db://assets/prefabs/Player.prefab - 脚本:
db://assets/scripts/GameManager.ts - 纹理:
db://assets/textures/player.png
如果工具调用失败,会返回错误信息:
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32000,
"message": "Tool execution failed",
"data": {
"error": "详细错误信息"
}
}
}- 先查询再操作:在修改节点或组件之前,先使用查询工具获取当前状态
- 使用UUID:尽量使用UUID而不是名称来引用节点和资源
- 错误检查:始终检查工具调用的返回值,确保操作成功
- 资源管理:在删除或移动资源前,确保没有其他地方引用它们
- 性能考虑:避免在循环中频繁调用工具,考虑批量操作
如果您在使用过程中遇到问题,可以:
- 使用
debug_get_console_logs查看详细的错误日志 - 使用
debug_validate_scene检查场景是否有问题 - 使用
debug_get_editor_info获取环境信息 - 检查 MCP 服务器的运行状态和日志
此文档基于 Cocos Creator MCP 服务器 v1.3.0 编写,如有更新请参考最新版本文档。