本插件实现了MCP(Model Context Protocol)协议规范,为开发者提供可扩展的二次开发接口,开发者可以编写自定义的Tool工具和Prompt提示模块,供Claude和Cursor等MCP客户端调用。
建议通过开发者菜单下的包管理器安装本插件。安装后MCP服务器会自动启动,并显示URL,将该URL填写到Cursor等IDE的MCP服务器配置里即可。
开发方式和普通的插件开发方式相同。在项目新建一个ts文件,使用本插件提供的装饰器就可以自定义tool和prompt。
通过@IEditor.mcp.tool/@IEditor.mcp.prompt在UI进程自定义tool和prompt。
通过@IEditorEnv.mcp.tool/@IEditorEnv.mcp.prompt在场景进程自定义tool和prompt。
回调函数的返回值可以为字符串,也可以为对象,请参考官方文档:https://modelcontextprotocol.io/docs
为了让vscode能够识别本插件提供的装饰器,需要在tsconfig里加上配置,如下示例,在include里加入./library/packages/**/*.d.ts
{
"compilerOptions": {
},
"include": [
"./assets",
"./src",
"./engine",
"./library/packages/**/*.d.ts"
]
}以下是简单的示例:
import fs from "fs";
import fpath from "path";
export class MCPTest {
@IEditorEnv.mcp.tool("create-cube-in-scene", 'create a cube in the opened scene')
static async createCube() {
let cube = new Laya.Sprite3D();
cube.name = "Cube";
cube.addComponent(Laya.MeshFilter).sharedMesh = <Laya.Mesh>await Laya.loader.load("~/internal/Box.lm");
cube.addComponent(Laya.MeshRenderer).sharedMaterial = <Laya.Material>await Laya.loader.load("~/internal/DefaultMaterial.lmat");
EditorEnv.scene.addNode(EditorEnv.scene.rootNode3D, cube);
EditorEnv.navigationManager.focusNode(cube);
return "finish";
}
@IEditorEnv.mcp.tool("import-model-to-scene", "import an external model to the scene", {
path: "an absolute file path"
})
static async importModelToScene(sourceFile: string) {
let asset = EditorEnv.assetMgr.createFileAsset(fpath.basename(sourceFile), true);
await fs.promises.copyFile(sourceFile, EditorEnv.assetMgr.getFullPath(asset));
//等待IDE监测到文件系统的改变
await IEditorEnv.utils.sleep(500);
await EditorEnv.assetMgr.flushChanges();
let node = await EditorEnv.scene.instantiatePrefab(asset.id, null, null);
EditorEnv.scene.addNode(EditorEnv.scene.rootNode3D, node);
EditorEnv.navigationManager.focusNode(node);
return "finish";
}
}